POJ2115C Looooops(2)
来源:互联网 发布:mac 打开安卓模拟器 编辑:程序博客网 时间:2024/06/09 22:26
C Looooops
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 23060 Accepted: 6332
分类:扩展gcd;
Description
A Compiler Mystery: We are given a C-language style for loop of type
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.
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 < 2k) are the parameters of the loop.
The input is finished by a line containing four zeros.
The input is finished by a line containing four zeros.
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.
Sample Input
3 3 2 163 7 2 167 3 2 163 4 2 160 0 0 0
Sample Output
0232766FOREVER
Source
CTU Open 2004
#include <cstring>#include <cstdio>#include <algorithm>#include <cmath>#include <queue>#include <stack>#define LL long longusing namespace std;void exgcd(LL a,LL b,LL &x, LL &y,LL &d){ if(!b){d=a,x=1;y=0;} else {exgcd(b,a%b,y,x,d),y-=(a/b)*x;}}int main(){ LL A,B,C,D; LL a,b,c,d; LL x,y; while(scanf("%lld%lld%lld%lld",&A,&B,&C,&D),A+B+C+D) { b=1; b<<=D; a=C, c=B-A; exgcd(a,b,x,y,d); if(c%d) { printf("FOREVER\n"); } else { x*=c/d; b/=d; x=(x%b+b)%b; printf("%lld\n",x); } } return 0;}
0 0
- POJ2115C Looooops(2)
- poj2115C Looooops(扩展欧几里德)
- POJ2115C Looooops(拓展欧几里得)
- poj2115C Looooops【扩展欧几里得】
- poj2115C Looooops 拓展欧几里德
- Looooops
- Looooops
- C Looooops(欧几里德+poj2115)
- poj2115 C Looooops (欧几里德)
- [POJ2115]C Looooops(扩欧)
- POJ 2115 C Looooops(扩展欧几里德)
- poj-2115 C Looooops(扩展欧几里得)
- poj 2115 C Looooops (扩展欧几里得)
- POJ2115-C Looooops (错误分析)
- POJ - 2115 - C Looooops (扩展欧几里得)
- POJ 2115 C Looooops(扩展欧几里得)
- POJ 2115 C Looooops(扩展欧几里得)
- POJ-2115-C Looooops(扩展欧几里得)
- 转载:python使用urllib2抓取防爬取链接
- 剑指offer——和为s的两个数字且乘积最小
- 解决Android SDK Manager无法更新的问题[设置代理]
- google地图、高德地图基于基站定位位置纠偏
- java(九九乘法表输出1,3,5,7,9)
- POJ2115C Looooops(2)
- [编写高质量iOS代码的52个有效方法](二)对象
- HDU 5745 ( La Vie en rose )
- python使用urllib2抓取防爬取链接
- Android静态安全检测 -> AES/DES弱加密
- Android中如何查找内存泄露
- Android内存泄漏简介
- hdu 3592 World Exhibition(差分约束,最短路)
- HDOJ 1052 Tian Ji -- The Horse Racing