poj2115C Looooops(扩展欧几里德)
来源:互联网 发布:安卓6.0短信拦截源码 编辑:程序博客网 时间:2024/05/19 02:31
C Looooops
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 24011 Accepted: 6651
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<stdio.h>#define LL long longvoid exgcd(LL a,LL b,LL &d,LL &x,LL &y){if(b==0){d=a;x=1;y=0;}else{exgcd(b,a%b,d,y,x);y-=x*(a/b);}}int main(){LL a,b,c,k,n,x,y,d;while(scanf("%lld%lld%lld%lld",&a,&b,&c,&k),a||b||c||k){n=(LL)1<<k;exgcd(c,n,d,x,y);if((b-a)%d)printf("FOREVER\n");else{LL ans=(b-a)/d*x;LL s=n/d;x=(ans%s+s)%s;printf("%lld\n",x);}}return 0;}
0 0
- poj2115C Looooops(扩展欧几里德)
- poj2115C Looooops 拓展欧几里德
- poj2115C Looooops【扩展欧几里得】
- POJ2115C Looooops(2)
- POJ2115C Looooops(拓展欧几里得)
- POJ 2115 C Looooops(扩展欧几里德)
- POJ 2115 C Looooops(扩展欧几里德)
- poj2115 Looooops 扩展欧几里德
- poj2115 Looooops(扩展欧几里德模板)
- POJ 2115 C Looooops(扩展欧几里德 + 求解模线性方程)
- poj-2115-C Looooops-扩展欧几里德
- poj 2115 C Looooops 扩展欧几里德算法
- poj2115 Looooops 扩展欧几里德的应用
- poj-2115-C Looooops-扩展欧几里德
- poj-2115-C Looooops-扩展欧几里德
- POJ 2115-C Looooops(扩展欧几里德)
- POJ 2115- C Looooops(扩展欧几里德)
- 【POJ 2115】 C Looooops (扩展欧几里德)
- 第四周项目2
- Android开源框架ViewPagerIndicator的基本使用
- HDU:A计划
- 面试题38:数字在排序数组中出现的次数
- SCU2016-03 A题 trie树 + dp
- poj2115C Looooops(扩展欧几里德)
- MARKDOWN语法总结
- 常见ORA异常
- ThreadLocal
- 39. Combination Sum
- Unix网络编程 卷1 第11章:名字与地址转换(udp_client_connect_connect)
- HashMap与ConcurrentHashMap的区别
- 【JZOJ 4772】 运输妹子
- python自学笔记