http://poj.org/problem?id=2115
题目大意:for(i=A;i!=B;i+=C),i的类型的范围为0<=a<1<<k
exgcd裸题目。
设a=C,b=(1<<k),c=(B-A).
则ax+by=c.
#include<cstdio> #include<cctype> #include<iostream> using namespace std; typedef long long ll; ll gcd(ll a,ll b){ return b?gcd(b,a%b):a; } void exgcd(ll a,ll b,ll &x,ll &y){ if(b==0){ x=1;y=0; return; } exgcd(b,a%b,x,y); ll temp; temp=x; x=y; y=temp-(a/b)*y; return; } int main(){ ll A,B,C,k; while(cin>>A>>B>>C>>k){ if(A==B&&B==C&&C==k&&A==0)return 0; ll x,y; ll a=C,b=(ll)1<<k,c=B-A; if(!c){ printf("0 "); continue; } ll g=gcd(a,b); if(c%g){ printf("FOREVER "); continue; } a/=g;b/=g;c/=g; exgcd(a,b,x,y); x=(x%b*c%b+b)%b; printf("%lld ",x); } return 0; }
#include<cstdio>
#include<cctype>
#include<iostream>
using namespace std;
typedef long long ll;
ll gcd(ll a,ll b){
return b?gcd(b,a%b):a;
}
void exgcd(ll a,ll b,ll &x,ll &y){
if(b==0){
x=1;y=0;
return;
}
exgcd(b,a%b,x,y);
ll temp;
temp=x;
x=y;
y=temp-(a/b)*y;
return;
}
int main(){
ll A,B,C,k;
while(cin>>A>>B>>C>>k){
if(A==B&&B==C&&C==k&&A==0)return 0;
ll x,y;
ll a=C,b=(ll)1<<k,c=B-A;
if(!c){
printf("0
");
continue;
}
ll g=gcd(a,b);
if(c%g){
printf("FOREVER
");
continue;
}
a/=g;b/=g;c/=g;
exgcd(a,b,x,y);
x=(x%b*c%b+b)%b;
printf("%lld
",x);
}
return 0;
}