Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 17752 | Accepted: 4608 |
Description
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 is finished by a line containing four zeros.
Output
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
欧几里德算法
欧几里德算法又称辗转相除法,用于计算两个整数a,b的最大公约数。
基本算法:设a=qb+r,其中a,b,q,r都是整数,则gcd(a,b)=gcd(b,r),即gcd(a,b)=gcd(b,a%b)。
第一种证明:
a可以表示成a = kb + r,则r = a mod b
假设d是a,b的一个公约数,则有
d|a, d|b,而r = a - kb,因此d|r
因此d是(b,a mod b)的公约数
假设d 是(b,a mod b)的公约数,则
d | b , d |r ,但是a = kb +r
因此d也是(a,b)的公约数
因此(a,b)和(b,a mod b)的公约数是一样的,其最大公约数也必然相等,得证
第二种证明:
要证欧几里德算法成立,即证: gcd(a,b)=gcd(b,r),其中 gcd是取最大公约数的意思,r=a mod b
下面证 gcd(a,b)=gcd(b,r)
设 c是a,b的最大公约数,即c=gcd(a,b),则有 a=mc,b=nc,其中m,n为正整数,且m,n互为质数
由 r= a mod b可知,r= a- qb 其中,q是正整数,
则 r=a-qb=mc-qnc=(m-qn)c
b=nc,r=(m-qn)c,且n,(m-qn)互质(假设n,m-qn不互质,则n=xd, m-qn=yd 其中x,y,d都是正整数,且d>1
则a=mc=(qx+y)dc, b=xdc,这时a,b 的最大公约数变成dc,与前提矛盾,
所以n ,m-qn一定互质)
则gcd(b,r)=c=gcd(a,b)
得证。
算法的实现:
最简单的方法就是应用递归算法,代码如下:
1 int gcd(int a,int b) 2 { 3 if(b==0) 4 return a; 5 return 6 gcd(b,a%b); 7 }
代码可优化如下:
1 int gcd(int a,int b) 2 { 3 return b ? gcd(b,a%b) : a; 4 }
当然你也可以用迭代形式:
1 int Gcd(int a, int b) 2 { 3 while(b != 0) 4 { 5 int r = b; 6 b = a % b; 7 a = r; 8 } 9 return a; 10 }
扩展欧几里德算法
基本算法:对于不完全为 0 的非负整数 a,b,gcd(a,b)表示 a,b 的最大公约数,必然存在整数对 x,y ,使得 gcd(a,b)=ax+by。
证明:设 a>b。
1,显然当 b=0,gcd(a,b)=a。此时 x=1,y=0;
2,ab!=0 时
设 ax1+by1=gcd(a,b);
bx2+(a mod b)y2=gcd(b,a mod b);
根据朴素的欧几里德原理有 gcd(a,b)=gcd(b,a mod b);
则:ax1+by1=bx2+(a mod b)y2;
即:ax1+by1=bx2+(a-(a/b)*b)y2=ay2+bx2-(a/b)*by2;
根据恒等定理得:x1=y2; y1=x2-(a/b)*y2;
这样我们就得到了求解 x1,y1 的方法:x1,y1 的值基于 x2,y2.
上面的思想是以递归定义的,因为 gcd 不断的递归求解一定会有个时候 b=0,所以递归可以结束。
扩展欧几里德的递归代码:
1 int exgcd(int a,int b,int &x,int &y) 2 { 3 if(b==0) 4 { 5 x=1; 6 y=0; 7 return a; 8 } 9 int r=exgcd(b,a%b,x,y); 10 int t=x; 11 x=y; 12 y=t-a/b*y; 13 return r; 14 }
扩展欧几里德非递归代码:
1 int exgcd(int m,int n,int &x,int &y) 2 { 3 int x1,y1,x0,y0; 4 x0=1; y0=0; 5 x1=0; y1=1; 6 x=0; y=1; 7 int r=m%n; 8 int q=(m-r)/n; 9 while(r) 10 { 11 x=x0-q*x1; y=y0-q*y1; 12 x0=x1; y0=y1; 13 x1=x; y1=y; 14 m=n; n=r; r=m%n; 15 q=(m-r)/n; 16 } 17 return n; 18 }
<span style="font-size:18px;">#include <iostream> #include<stdio.h> #include<math.h> #include<string.h> using namespace std; long long g; int extended_oujilide(long long n,long long m,long long &x,long long &y) { if(m==0) { x=1; y=0; return n; } g=extended_oujilide(m,n%m,x,y); long long t=x-n/m*y; x=y; y=t; return g; } int main() { long long a,b,c,k,n,m,x,y; while(scanf("%lld%lld%lld%lld",&a,&b,&c,&k)!=EOF) { if(a==0&&b==0&&c==0&&k==0) break; m=(long long)1<<k; //printf("%lld\n",m); extended_oujilide(c,m,x,y); if((b-a)%g!=0) printf("FOREVER\n"); else { x*=((b-a)/g); n=m/g; x=(x%n+n)%n; printf("%lld\n",x); } } } </span>