poj2115--C Looooops(扩展gcd)
来源:互联网 发布:东方财富股票交易软件 编辑:程序博客网 时间:2024/05/24 04:37
C Looooops
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 17740 Accepted: 4600
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
题目大意初始值是a,每一次加c ,可以对得到的值取模2^k,在值为b时结束,问最少会执行多少次,如过一直不会停FOREVER
由题中大意的到等式,假设一定会停,那么在循环x次后会等于b 也就是 (a + x*c)% (2^k)= b 。很明显的扩展gcd的题目,得到等式, a + c*x - b = y*(2^k) 也就是
求解 c*x - (2^k)*y = b-a ,问有没有整数解。
对于a*x1 + b*y1 = c ,用扩展gcd,求解 a*x1 + b*y1 = gad(a,b) = gcd(b,a%b) = a*y2 +b* (x2-a/b*y2)得到 x1 = y2 , y1 = ( x2 +a/b*y2 ) , 利用这个等式,可以推到b = 0 时,那是x = 1 , y = 0 ,最大公约数为a,在逐层返回,最终得到 a*x1 + b*y1 = gad(a,b) 的解,如果c%( gcd(a,b) ) == 0 那么a*x1 + b*y1 = c有整数解,令d = gcd(a,b),d = b / d.最小的正整数解围 x = ( x%d +d )%d.
#include <cstdio>#include <cstring>#include <algorithm>#define LL __int64using namespace std;void gcd(LL a,LL b,LL &d,LL &x,LL &y){ if(b == 0) { d = a ; x = 1 ; y = 0 ; } else { gcd(b,a%b,d,y,x); y = -y ; x = -x ; y += a/b*x ; } return ;}int main(){ LL k , a , b , c , d , x , y ; while(scanf("%I64d %I64d %I64d %I64d", &a, &b, &c, &k)!=EOF) { if(a == 0 && b == 0 && c == 0 && k == 0) break; k = ( (LL)1 )<<k ; gcd(c,k,d,x,y); if( (b-a)%d ) printf("FOREVER\n"); else { x = (b-a)/d*x ; k = k/d ; x = (x%k+k)%k ; printf("%I64d\n", x); } } return 0;}
0 0
- poj2115--C Looooops(扩展gcd)
- POJ2115 C Looooops ——模线性方程(扩展gcd)
- POJ2115 C Looooops(扩展欧几里得)
- poj2115 C Looooops(扩展欧几里得)
- C Looooops poj2115 (扩展欧几里得+模线性方程)
- POJ2115 C Looooops(扩展欧几里得算法)
- POJ2115:C Looooops(扩展欧几里得)
- POJ2115 C Looooops(扩展欧几里得)
- poj2115 Looooops 扩展欧几里德
- 欧几里德 poj2115 C Looooops
- POJ2115 C Looooops
- POJ2115-C Looooops
- [POJ2115] C Looooops
- #POJ2115# C Looooops
- POJ2115-C Looooops
- POJ2115 C Looooops
- poj2115 Looooops(扩展欧几里德模板)
- C Looooops(欧几里德+poj2115)
- OC 中的数组方法(不可变数组NSArray 和 可变数组 NSMutableArray)
- linux高级技巧:集群之keepalived
- 什么是sharding与切分
- HalconVC异常处理
- 逻辑地址、线性地址、物理地址和虚拟地址理解
- poj2115--C Looooops(扩展gcd)
- android 删除目录(实例)
- linux高级技巧:heartbeat+lvs(三)
- Urall-1109(二分图的最大匹配 匈牙利算法)
- C语言之高级指针
- 【线段树】HDU 1754---I Hate It
- POJ1611_The Suspects
- maxsum(dp求最大子矩阵)
- Halcon入摄像机标定初始参数解释