Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 32062   Accepted: 9337

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 < 2k) are the parameters of the loop.

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 16
3 7 2 16
7 3 2 16
3 4 2 16
0 0 0 0

Sample Output

0
2
32766
FOREVER

Source

 
  在模2^k的意义下,A每次加C,最少加多少次能达到B,输出这个次数。
  易得 A+i*C= B  (mod 2^k)   -->   i*C+j*(2^k) = B - A ,问题转化为求这个方程成立的最小的i值,直接拓欧,坑点是K最大32,不强制LL
的话会爆。
  

 #include<iostream>
#include<cstdio>
using namespace std;
#define LL long long
#define mp make_pair
#define pb push_back
#define inf 0x3f3f3f3f
void exgcd(LL a,LL b,LL &d,LL &x,LL &y){
if(!b){d=a;x=;y=;}
else{exgcd(b,a%b,d,y,x);y-=(a/b)*x;}
}
int main(){
LL A,B,C,K;
while(scanf("%lld%lld%lld%lld",&A,&B,&C,&K)!=EOF){
if(A==&&B==&&C==&&K==) break;
LL x,y,d;
LL M=;
M<<=K;
exgcd(C,M,d,x,y);
if((B-A)%d!=){
puts("FOREVER");
}
else{
M/=d;
printf("%lld\n",((x*(B-A)/d)%M+M)%M);
}
}
return ;
}

poj-2115-exgcd的更多相关文章

  1. R - C Looooops POJ - 2115 (exgcd)

    题目大意:很好理解,一个for循环语句,从a开始到b结束,步长是c,模数是pow(2,k) 问,最少循环多少次,才能到达b,如果永远都到不了b,输出FOREVER 题解:其实就是求一个线性方程,cx= ...

  2. 【题解】POJ 2115 C Looooops (Exgcd)

    POJ 2115:http://poj.org/problem?id=2115 思路 设循环T次 则要满足A≡(B+CT)(mod 2k) 可得 A=B+CT+m*2k 移项得C*T+2k*m=B-A ...

  3. poj 2115 C Looooops——exgcd模板

    题目:http://poj.org/problem?id=2115 exgcd裸题.注意最后各种%b.注意打出正确的exgcd板子.就是别忘了/=g. #include<iostream> ...

  4. POJ 2115 C Looooops(扩展欧几里得应用)

    题目地址:POJ 2115 水题. . 公式非常好推.最直接的公式就是a+n*c==b+m*2^k.然后能够变形为模线性方程的样子,就是 n*c+m*2^k==b-a.即求n*c==(b-a)mod( ...

  5. POJ 2115 C Looooops(Exgcd)

    [题目链接] http://poj.org/problem?id=2115 [题目大意] 求for (variable = A; variable != B; variable += C)的循环次数, ...

  6. Poj 2115 C Looooops(exgcd变式)

    C Looooops Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 22704 Accepted: 6251 Descripti ...

  7. C Looooops POJ - 2115 (exgcd)

    一个编译器之谜:我们被给了一段C++语言风格的循环 for(int i=A;i!=B;i+=C) 内容; 其中所有数都是k位二进制数,即所有数时膜2^k意义下的.我们的目标时球出 内容 被执行了多少次 ...

  8. POJ 2115 C-Looooops | exgcd

    题目 给出一个循环for(int i=A;i!=B;i+=C) 在mod (1<<k) 下是否可以退出循环 是,输出时间,否输出FORVEER 题解: 题意可以变换成 A+Cx=B (mo ...

  9. POJ 2115 C Looooops(模线性方程)

    http://poj.org/problem?id=2115 题意: 给你一个变量,变量初始值a,终止值b,每循环一遍加c,问一共循环几遍终止,结果mod2^k.如果无法终止则输出FOREVER. 思 ...

  10. poj 2115 扩展欧几里得

    题目链接:http://poj.org/problem?id=2115 题意: 给出一段循环程序,循环体变量初始值为 a,结束不等于 b ,步长为 c,看要循环多少次,其中运算限制在 k位:死循环输出 ...

随机推荐

  1. PTA 7-3 jmu-ds-单链表的基本运算(15 分)

    jmu-ds-单链表的基本运算(15 分) 实现单链表的基本运算:初始化.插入.删除.求表的长度.判空.释放.(1)初始化单链表L,输出L->next的值:(2)依次采用尾插法插入元素:输入分两 ...

  2. Selenium Webdriver 自动化测试开发常见问题(C#版)

    转一篇文章,有修改,出处http://www.7dtest.com/site/blog-2880-203.html 1:Selenium中对浏览器的操作 首先生成一个Web对象 IWebDriver ...

  3. HDU 1403 Longest Common Substring(最长公共子串)

    http://acm.hdu.edu.cn/showproblem.php?pid=1403 题意:给出两个字符串,求最长公共子串的长度. 思路: 刚开始学后缀数组,确实感觉很难,但是这东西很强大,所 ...

  4. Ajax - 汇总

    1,什么是ajax? 为什么要使用ajax? 1.ajax是"asynchornous javascript and xml "的缩写,指一种创建交互式网页应用的网页开发技术. 2 ...

  5. React创建组件的三种方式及其区别

    内容转载于http://www.cnblogs.com/wonyun/p/5930333.html React推出后,出于不同的原因先后出现三种定义react组件的方式,殊途同归; 具体的三种方式: ...

  6. xxx did not match any file(s) known to git

    切换分支的时候,报了标题这么个错误,error: pathspec ''xxx did not match any file(s) known to git. 看见不能切换分支,我首先 git sta ...

  7. JaveWeb 公司项目(5)----- Java获取当前时间的年月日以及同Thrift格式的转化

    随着项目进度的逐步完成,数据传输和界面基本上已经搭建完成,下面就是一些细节部分的修改 今天博文的主要内容说的是获取当前的时间和同Thrift类型的转化 和C#类似,java也有一个时间类Date,加载 ...

  8. P4391 [BOI2009]Radio Transmission

    描述: 给你一个字符串,它是由某个字符串不断自我连接形成的. 但是这个字符串是不确定的,现在只想知道它的最短长度是多少. 输入格式: 第一行给出字符串的长度,1 < L ≤ 1,000,000. ...

  9. Java面试宝典(说说&和&&的区别)

    &和&&都可以用作逻辑与的运算符,表示逻辑与(and),当运算符两边的表达式的结果都为true时,整个运算结果才为true,否则,只要有一方为false,则结果为false. ...

  10. 通过IP地址定位准确的地理位置

    事情的经过时这样的: 朋友发来一封QQ邮件原文,询问里面显示的IP地址是不是真是的IP地址.然后,我就解锁了一项新技能:通过IP地址定位准确的地理位置 在这里收藏一下这个网址:http://www.8 ...