http://poj.org/problem?id=2115
有一些写程序的技巧
#include <iostream>
#include <algorithm>
#define pr pair<int,int>
#define x tmp.first
#define y tmp.second
#define int long long
using namespace std;
int Gcd;
pr exgcd(int a,int b)
{
if(b==0)
{
Gcd=a;
return pr(1,0);
}
pr tmp=exgcd(b,a%b);
return pr(y,x-a/b*y);
}
signed main()
{
int a1,b1,c1,k;
while(cin>>a1>>b1>>c1>>k)
{
if (a1 == 0 && b1 == 0 && c1 == 0 && k == 0) break;
int k2=1ll<<k;
int l=b1-a1;
if(l==0)
{
cout<<0<<endl;
continue;
}
pr tmp = exgcd(c1,k2);
int dm=k2/Gcd;
if(l%Gcd!=0)cout<<"FOREVER"<<endl;
else cout<<((x*l/Gcd)%dm+dm)%dm<<endl;
}
return 0;
}