POJ2115 ZOJ2305 C Looooops同余方程
Posted 海岛Blog
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ2115 ZOJ2305 C Looooops同余方程相关的知识,希望对你有一定的参考价值。
C Looooops
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 39685 Accepted: 11680
Description
A Compiler Mystery: We are given a C-language style for loop of type
for (variable = A; variable != B; variable += C)
statement;
I.e., a loop which starts by setting variable to value A and while variable is not equal to B, repeats statement followed by increasing the variable by C. We want to know how many times does the statement get executed for particular values of A, B and C, assuming that all arithmetics is calculated in a k-bit unsigned integer type (with values 0 <= x < 2k) modulo 2k.
Input
The input consists of several instances. Each instance is described by a single line with four integers A, B, C, k separated by a single space. The integer k (1 <= k <= 32) is the number of bits of the control variable of the loop and A, B, C (0 <= A, B, C < 2k) are the parameters of the loop.
The input is finished by a line containing four zeros.
Output
The output consists of several lines corresponding to the instances on the input. The i-th line contains either the number of executions of the statement in the i-th instance (a single integer number) or the word FOREVER if the loop does not terminate.
Sample Input
3 3 2 16
3 7 2 16
7 3 2 16
3 4 2 16
0 0 0 0
Sample Output
0
2
32766
FOREVER
Source
问题链接: POJ2115 ZOJ2305 C Looooops
问题简述:(略)
问题分析:用扩展欧几里得算法解同余方程。
程序说明:(略)
参考链接:(略)
题记:(略)
AC的C++语言程序如下:
/* POJ2115 ZOJ2305 C Looooops */
#include <iostream>
#include <cstdio>
using namespace std;
typedef long long LL;
void exgcd(LL a, LL b, LL &d, LL &x, LL &y)
{
if (b) {exgcd(b, a % b, d, x, y); LL t = x; x = y; y = t - a / b * y;}
else {x = 1; y = 0; d = a;}
}
int main()
{
LL a, b, c, k;
while (~scanf("%lld%lld%lld%lld", &a, &b, &c, &k) && (a || b || c || k)) {
LL a1 = c, b1 = b - a, d, x, y, n = 1LL << k; // 2^k
exgcd(a1, n, d, x, y);
if (b1 % d != 0)
printf("FOREVER\\n");
else {
x = (x * (b1 / d)) % n;
x = (x % (n / d) + n / d) % (n / d);
printf("%lld\\n", x);
}
}
return 0;
}
以上是关于POJ2115 ZOJ2305 C Looooops同余方程的主要内容,如果未能解决你的问题,请参考以下文章