| ||||||||||
Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
欧几里德扩展算法,怎么错了?#include<iostream> using namespace std; long long gcd(long long,long long); long long lcm(long long,long long); long long m1,n1; int main() { int a,b,c,k; while(cin>>a>>b>>c>>k) { if(!a&&!b&&!c&&!k) break; if(a==b) { cout<<0<<endl; continue; } if(c==0) { cout<<"FOREVER"<<endl; continue; } long long t=1; for(int i=0;i<k;i++) t<<=1; long long m=gcd(c,t); if((b-a)%m) { cout<<"FOREVER"<<endl; continue; } int p=(b-a)/m*m1; int x=t/m; if(x) { if(p>0) { p-=(p/x)*x; } else if(p<0) { p+=(-p)/x*x; p+=x; } } if(p<0) { cout<<"FOREVER"<<endl; } else cout<<p<<endl; //cout<<t<<endl; } return 0; } long long gcd(long long m,long long n) { if(m==0) { m1=0; n1=1; return n; } else { long long tmp=gcd(n%m,m); long long p1=n1-n/m*m1; long long p2=m1; m1=p1; n1=p2; return tmp; } } long long lcm(long long a,long long b) { return (a*b)/gcd(a,b); } Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator