Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 21174 | Accepted: 5753 |
Description
for (variable = A; variable != B; variable += C) statement;
Input
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 转换为:cx+2^ky=b-a ac代码:#include<stdio.h> #include<math.h> #include<string.h> #include<stack> #include<set> #include<queue> #include<vector> #include<iostream> #include<algorithm> #define MAXN 1010000 #define LL long long #define ll __int64 #define INF 0xfffffff #define mem(x) memset(x,0,sizeof(x)) #define PI acos(-1) using namespace std; int gcd(int a,int b){return b?gcd(b,a%b):a;} LL powmod(LL a,LL b,LL MOD){LL ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;} //head ll x,y; ll exgcd(ll a,ll b) { if(b==0) { x=1;y=0; return a; } ll ans=exgcd(b,a%b); ll t=x; x=y; y=t-a/b*y; return ans; } int main() { ll a,b,c,k; while(scanf("%I64d%I64d%I64d%I64d",&a,&b,&c,&k)!=EOF) { if(a==0&&b==0&&c==0&&k==0) break; ll num=pow(2,k+0.0); //printf("k=%I64d\n",k); ll kk=exgcd(c,num); //printf("kk=%I64d\n",kk); if((b-a)%kk) printf("FOREVER\n"); else { //printf("x=%I64d\n",x); x=(b-a)/kk*x%num+num; //printf("x=%I64d\n",x); printf("%I64d\n",x%(num/kk)); } } return 0; }