集训队专题(8)1009 C Looooopsz

来源:互联网 发布:5g网络的股票 编辑:程序博客网 时间:2024/06/15 10:58

C Looooops

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 26   Accepted Submission(s) : 15
Problem Description
A Compiler Mystery: We are given a C-language style for loop of type 
for (variable = A; variable != B; variable += C)  statement;

I.e., a loop which starts by setting variable to value A and while variable is not equal to B, repeats statement followed by increasing the variable by C. We want to know how many times does the statement get executed for particular values of A, B and C, assuming that all arithmetics is calculated in a k-bit unsigned integer type (with values 0 <= x < 2k) modulo 2k

 

Input
The input consists of several instances. Each instance is described by a single line with four integers A, B, C, k separated by a single space. The integer k (1 <= k <= 32) is the number of bits of the control variable of the loop and A, B, C (0 <= A, B, C < 2<sup>k</sup>) are the parameters of the loop. <br> <br>The input is finished by a line containing four zeros. <br>
 

Output
The output consists of several lines corresponding to the instances on the input. The i-th line contains either the number of executions of the statement in the i-th instance (a single integer number) or the word FOREVER if the loop does not terminate. <br>
 

Sample Input
3 3 2 163 7 2 167 3 2 163 4 2 160 0 0 0
 

Sample Output
0232766FOREVER
 

Source
PKU
 

这题小编做的时候看的是别人的题解做的,在看的时候发现了一篇写的相当细致的文章,小编这里就不再自己写题解,直接献上大神的作品

http://blog.csdn.net/lyy289065406/article/details/6648546

#include <cstdio>#define LL __int64LL exGcd(LL a,LL b,LL &x,LL &y){if(b == 0){x = 1;y = 0;return a;}LL d = exGcd(b,a%b,x,y);LL tmp = x;x = y;y = tmp-a/b*y;return d;}int main(){LL A,B,C,k;while(scanf("%I64d%I64d%I64d%I64d",&A,&B,&C,&k)&&(A||B||C||k)){LL a = C, b = B-A, n = (LL)1<<k;LL x,y;LL d = exGcd(a,n,x,y);if(b%d != 0) printf("FOREVER\n");else{x = (x*(b/d))%n;x = (x%(n/d)+n/d)%(n/d);printf("%I64d\n",x);}}return 0;}


0 0
原创粉丝点击