标签:
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 23637 | Accepted: 6528 |
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
Source
#include<iostream> #include<stdio.h> using namespace std; long long pow(long long k) { long long ans=1; for(int i=0;i<k;i++) ans*=2; return ans; } long long ext_gcd(long long a,long long b,long long *x,long long *y) { if(b==0) { *x=1,*y=0; return a; } long long r = ext_gcd(b,a%b,x,y); long long t = *x; *x = *y; *y = t - a/b * *y; return r; } int main() { long long a,b,c,k; while(~scanf("%I64d%I64d%I64d%I64d",&a,&b,&c,&k)) { if((a+b+c+k)==0) break; long long x,y; long long _gcd_ = ext_gcd(c,pow(k),&x,&y); if((b-a)%_gcd_) { printf("FOREVER\n"); continue; } long long tmp_ans = x*(b-a)/_gcd_; long long T = pow(k)/_gcd_;/*总结一下: b/gcd是 ax+by = k*gcd中,x*k/gcd的周期*/ long long ans = (tmp_ans%T+T)%T; printf("%I64d\n",ans); } return 0; }
标签:
原文地址:http://www.cnblogs.com/superxuezhazha/p/5768465.html