POJ 2115 C Looooops(拓展欧几里德)
来源:互联网 发布:唐望 知乎 编辑:程序博客网 时间:2024/06/08 18:20
C Looooops
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 23914 Accepted: 6613
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
1<<k的时候要先转换成long long 不然wa呀!
#include <iostream>#include <cstring>#include <cstdio>typedef unsigned long long ull;using namespace std;void exgcd(ull a,ull b,ull &d,ull &x,ull &y){ if(!b) {y=0,x=1,d=a;} else { exgcd (b,a%b,d,y,x), y-=x*(a/b);}}int main(){ //freopen("/Users/zhangjiatao/Documents/暑期训练/input.txt","r",stdin); ull a,b,c,k,d,m; while(scanf("%lld%lld%lld%lld",&a,&b,&c,&k)) { if(a+b+c+k==0) break; ull x0,y0; m=(long long)1<<k; exgcd(c,m,d,x0,y0); if((b-a)%d!=0){ printf("FOREVER\n"); continue;} ull t=x0*(b-a)/d; ull temp=m/d; t=(t%temp+temp)%temp; printf("%lld\n",t); } return 0;}
0 0
- poj 2115 C Looooops 拓展欧几里德定理
- POJ 2115 C Looooops(拓展欧几里德)
- poj 2115 C Looooops 拓展欧几里德 解模线性方程模板
- PKU 2115 C Looooops(拓展欧几里德)
- POJ2115 C Looooops(拓展欧几里德)
- poj-2115-C Looooops-扩展欧几里德
- poj 2115 C Looooops 扩展欧几里德算法
- POJ 2115 C Looooops(扩展欧几里德)
- poj-2115-C Looooops-扩展欧几里德
- poj-2115-C Looooops-扩展欧几里德
- POJ 2115-C Looooops(扩展欧几里德)
- POJ 2115- C Looooops(扩展欧几里德)
- 【POJ 2115】 C Looooops (扩展欧几里德)
- POJ-2115-C Looooops-扩展欧几里德算法
- POJ 2115-C Looooops-扩展欧几里德算法
- POJ 2115 C Looooops(扩展欧几里德)
- poj 2115 C Looooops(扩展欧几里德)
- poj 2115 C Looooops (扩展欧几里德)
- 【Java并发编程】之七:使用synchronized获取互斥锁的几点说明 (r)
- Java版-九大排序算法
- POJ 1741 Tree(树的点分治、树形dp、男人八题)
- EditText的输入要求
- 面向对象的概述
- POJ 2115 C Looooops(拓展欧几里德)
- 条件变量用例--解锁与signal的顺序问题
- Android多个视频循环播放
- 知识管理的新模式
- HTML5 音频和视频(20160808-0008)
- 【Java并发编程】之八:多线程环境中安全使用集合API(含代码)(r)
- 中断程序
- 去除APP启动时的黑屏/白屏/等待时间/闪烁
- 使用清华镜像快速下载Android系统源代码