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 --------------------------------------------------------------------------
题意:在一个k位的机器里(大于2^k就回到0),进行每次增加c的循环,循环终止条件是!=b求循环何时终止。
分析:裸的扩欧。方程:c*x + 2^k*y = b-a 。
 #include <cstdio>
typedef long long LL;
LL exgcd(LL a,LL b,LL &x,LL &y)
{
int d;
if(b==)
{
x=;y=;return a;
}
else
{
d=exgcd(b,a%b,y,x);y-=x*(a/b);
}
return d;
}
int main()
{
LL a,b,c,k;
while(scanf("%lld%lld%lld%lld",&a,&b,&c,&k)&&(a||b||c||k))
{ LL i=b-a,x=,y=,d=,p=1LL<<k;//不加LL会爆
//方程:c*x + 2^k*y = b-a
d=exgcd(c,p,x,y);
if(i%d!=)
{
printf("FOREVER\n");
continue;
}
p/=d;
x%=p;
x*=(i/d)%p;//把倍数乘上
x=(x%p+p)%p;
printf("%lld\n",x);
}
return ;
}

【POJ】2115 C Looooops(扩欧)的更多相关文章

  1. 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( ...

  2. POJ 2115 C Looooops扩展欧几里得

    题意不难理解,看了后就能得出下列式子: (A+C*x-B)mod(2^k)=0 即(C*x)mod(2^k)=(B-A)mod(2^k) 利用模线性方程(线性同余方程)即可求解 模板直达车 #incl ...

  3. POJ 2115 C Looooops(扩展欧几里得)

    辗转相除法(欧几里得算法) 时间复杂度:在O(logmax(a, b))以内 int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a ...

  4. 【题解】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 ...

  5. POJ 2115 C Looooops( 简单拓欧 + 快速幂 )

    链接:传送门 题意:题目中给出一个循环 for (variable = A; variable != B; variable += C) ,这个东东还需要 mod 2^k 问至少多次能退出,如果进入死 ...

  6. poj 2115 C Looooops(推公式+扩展欧几里得模板)

    Description A Compiler Mystery: We are given a C-language style for loop of type for (variable = A; ...

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

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

  8. POJ 2115 C Looooops(Exgcd)

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

  9. poj 2115 C Looooops——exgcd模板

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

  10. POJ 2115 C Looooops

    扩展GCD...一定要(1L<<k),不然k=31是会出错的 ....                        C Looooops Time Limit: 1000MS   Mem ...

随机推荐

  1. Android-Error3:Error when loading the SDK

    解决方法: 用C:\android\sdk\tools中的devices.xml将出现错误的地方的devices.xml替换掉既可以了.

  2. linux bash 和 sh的区别

    详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt189 Linux 中的 shell 有很多类型,其中最常用的几种是: Bou ...

  3. CentOS 7 更改网卡名为eth0

    今天用VBOX安装了CentOS7, 但是安装完之后,发现没有ifconfig命令,在网上搜了一下解决办法,在可以连网状态下,输入下面命令即可 然后就可以用ifconfig命令了. 使用ifconfi ...

  4. Linux-grep 命令和find 命令 (6)

    grep与find区别: grep:查找指定目录下过滤文本文件中行数据 find:查找指定目录下文件 grep使用 格式:   grep "text"   *  [选项] 选项: ...

  5. POJ3228 并查集或二分最大流枚举答案

    忘记写题意了.这题题意:给出每个地点的金矿与金库的数量,再给出边的长度.求取最大可通过边长的最小权值使每个金矿都能运输到金库里. 这题和之前做的两道二分枚举最大流答案的问法很相识,但是这里用最大流速度 ...

  6. 201521123091 《Java程序设计》第4周学习总结

    Java 第二周总结 第四周的作业. 目录 1.本章学习总结 2.Java Q&A 3.使用码云管理Java代码 4.PTA实验 1.本章学习总结 1.1 尝试使用思维导图总结有关继承的知识点 ...

  7. 201521123100 《Java程序设计》第5周学习总结

    1. 本周学习总结 1.1 尝试使用思维导图总结有关多态与接口的知识点. 1.2 可选:使用常规方法总结其他上课内容. 1.接口不是类,不能使用new进行实例化 2.使用instanceof运算符,可 ...

  8. 201521123050 《Java程序设计》第13周学习总结

    1. 本周学习总结 2. 书面作业 1. 网络基础 1.1 比较ping www.baidu.com与ping cec.jmu.edu.cn,分析返回结果有何不同?为什么会有这样的不同? 对比可以看出 ...

  9. 201521123079《java程序设计》第11周学习总结

    1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结多线程相关内容. 2. 书面作业 本次PTA作业题集多线程 1.互斥访问与同步访问 完成题集4-4(互斥访问)与4-5(同步访问) ...

  10. 201521123108 《Java程序设计》第11周学习总结

    1. 本周学习总结 2. 书面作业 本次PTA作业题集多线程 Q1.互斥访问与同步访问 完成题集4-4(互斥访问)与4-5(同步访问) 1.1 除了使用synchronized修饰方法实现互斥同步访问 ...