C Looooops
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 17556 | Accepted: 4540 |
Description
A Compiler Mystery: We are given a C-language style for loop of type
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 < 2 k) modulo 2 k.
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 < 2 k) modulo 2 k.
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 < 2
k) are the parameters of the loop.
The input is finished by a line containing four zeros.
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
题目大意:
给你ABCk四个数字,i=A开始 i+=C为每轮加的值,i==B为停止的条件,以2^k作为存储单位。
即方程: x=[(B-A+2^k)%2^k] /C
求x的值,讲A-B化为b 2 ^k化为n C化为a,则原方程化为 ax=b(mod n)
也就是求解模线性方程了,用的是扩展欧几里得算法........
一点点知识点花了好久的时间,总算是有所理解了。
下面是扩展欧几里得算法求不定方程ax+by=c的值:
bool linear_equation(LL a,LL b,LL c,LL &x,LL &y)
{
LL d=ex_gcd(a,b,x,y);
if(c%d!=0) //即不整除
return false;
LL k=c/d;
x*=k; y*=k; //求得的只是其中一组解
return true;
}
扩展欧几里得算法可以求出ax+by=gcd(a,b) 的值,这个值记为d,如果说c的值不是d的值的倍数,则一定无解,否则乘起来就得到答案了
注意:求得的x0,y0只是一组特解,另外的解按照这个规则: x=x0+b/gcd(a,b) y=y0-a/gcd(a,b)来算得
而模线性方程则有点麻烦:求解 ax=b(mod n)
化为ax+ny=b的x的解,首先我们先把a和n的最大公约数d求出来,然后一样的道理如果n不是d的倍数则一定无解
下面就是求出一组特解x0=x*(b/d)%n; 为什么与之前的x的特解不一样要在后面mod一个n是因为这是一个线性同余方程
bool modular_linear_equation(LL a,LL b,LL n)
{
LL x,y,x0,i;
LL d=ex_gcd(a,n,x,y);
if(b%d!=0)
return false;
x0=x*(b/d)%n; //特解
for(i=1;i<d;i++) //解的个数是d
printf("%d\n",(x0+i*(n/d))%n);
return true;
}
下面附:欧几里得算法的模板......
#include<iostream>
#include<stdio.h>
using namespace std;
#define LL long long
LL gcd(LL a,LL b)
{
if(b==0) return a;
else return gcd(b,a%b);
}
LL ex_gcd(LL a,LL b,LL &x,LL &y)
{
if(b==0)
{
x=1;y=0;
return a;
}
LL r=ex_gcd(b,a%b,x,y);
LL t=x;
x=y;
y=t-a/b*y;
return r;
}
//求解ax+by=c的x,y的解
bool linear_equation(LL a,LL b,LL c,LL &x,LL &y)
{
LL d=ex_gcd(a,b,x,y);
if(c%d!=0) //即不整除
return false;
LL k=c/d;
x*=k; y*=k; //求得的只是其中一组解
return true;
}
bool modular_linear_equation(LL a,LL b,LL n)
{
LL x,y,x0,i;
LL d=ex_gcd(a,n,x,y);
if(b%d!=0)
return false;
x0=x*(b/d)%n; //特解
for(i=1;i<d;i++) //解的个数是d
printf("%d\n",(x0+i*(n/d))%n);
return true;
}
下面附这道题目的主函数:
int main ()
{
LL A,B,C,k;
while(~scanf("%I64d%I64d%I64d%I64d",&A,&B,&C,&k)&&(A||B||C||k))
{
LL a=C;
LL b=B-A;
LL n=1LL<<k;
LL x,y;
LL d=ex_gcd(a,n,x,y);
if(b%d!=0) cout<<"FOREVER"<<endl;
else
{
x=(x*(b/d))%n; //特解
x=(x%(n/d)+n/d)%(n/d); //方程ax=b(mod n)的最整数小解
printf("%I64d\n",x);
}
}
}
注意点就是定义1<<k的时候要这样定义 1LL<<k 这样定义出来才是long long 的类型