扩展GCD。。。一定要(1L<<k),不然k=31是会出错的 。。。。

                       C Looooops
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 15444   Accepted: 3941

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

CTU Open 2004

 #include <iostream>
#include <cstdio>
#include <cstring> using namespace std; typedef long long int LL; inline LL P(LL k){return (1LL<<k);} LL GCD(LL a,LL b)
{
return b==?a:GCD(b,a%b);
} LL EX_GCD(LL a,LL b,LL& x,LL& y)
{
if(b==)
{
x=;y=;
return a;
}
else
{
int ret=EX_GCD(b,a%b,x,y);
int t=x;
x=y; y=t-a/b*y;
return ret;
}
} int main()
{
LL a,b,c,k;
while(scanf("%I64d%I64d%I64d%I64d",&a,&b,&c,&k)!=EOF)
{
if(a==&&b==&&c==&&k==) break;
LL Aa=P(k),Bb=-c,Cc=a-b;
int d=GCD(Aa,Bb);
if(Cc%d!=)
{
puts("FOREVER");
continue;
}
LL A=Aa/d,B=Bb/d,C=Cc/d,X,Y;
EX_GCD(A,B,X,Y);
X=X*C;Y=Y*C;
if(A<) A=-A;
printf("%I64d\n",(Y%A+A)%A);
}
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 (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(模线性方程)

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

  4. POJ 2115 C Looooops(Exgcd)

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

  5. poj 2115 C Looooops——exgcd模板

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

  6. Poj 2115 C Looooops(exgcd变式)

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

  7. poj 2115 C Looooops 扩展欧几里德

    C Looooops Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 23616   Accepted: 6517 Descr ...

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

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

  9. poj 2115 C Looooops(扩展gcd)

    题目链接 这个题犯了两个小错误,感觉没错,结果怒交了20+遍,各种改看别人题解,感觉思路没有错误,就是wa. 后来看diccuss和自己查错,发现自己的ecgcd里的x*(a/b)写成了x*a/b.还 ...

随机推荐

  1. C#的参数修饰符out,params,ref

    using System; namespace ParamsProgram { class TestParams { public static void Main(string[] args)//s ...

  2. Android中运行的错误:java.lang.UnsatisfiedLinkError: Couldn't load locSDK3: findLibrary returned null.

    ---恢复内容开始--- 明明已经加入了liblocSDK3.so,但总是无法定位.提示错误java.lang.UnsatisfiedLinkError: Couldn't load locSDK3: ...

  3. Java内存回收机制

    在Java中,它的内存管理包括两方面:内存分配(创建Java对象的时候)和内存回收,这两方面工作都是由JVM自动完成的,降低了Java程序员的学习难度,避免了像C/C++直接操作内存的危险.但是,也正 ...

  4. 系统安装之:虚拟机VMware V12.0.1 专业版 + 永久密钥

    撰写日期:2016-6-30 10:30:26 转自:http://blog.sina.com.cn/s/blog_4549d6770102vxue.html    VMware V12.0.1 专业 ...

  5. Input Leakage Current

    输入脚一般是的Cmos 的G脚,理论上期望G-S是无限大高阻,,再大的电阻加了电压就会有电流,因为半导体电子迁移,存在微弱电流从G流到S脚.称做漏电流.而其与特定的加载的电压和环境温度有关系. 当然, ...

  6. redshift编译遇到的错误(ubuntu14.04)

    1. ./bootstrap: 6: ./bootstrap: autopoint: not found 解决方法: $ sudo apt-get install autopoint 2. autor ...

  7. 关键字static(1)

    static表示"全局"或者"静态"的意思,用来修饰成员变量和成员方法,也可以形成静态static代码块,但是Java语言中没有全局变量的概念.被static修 ...

  8. 谁都能看懂的单点登录(SSO)实现方式(附源码)

    SSO的基本概念 SSO英文全称Single Sign On(单点登录).SSO是在多个应用系统中,用户只需要登录一次就可以访问所有相互信任的应用系统.它包括可以将这次主要的登录映射到其他应用中用于同 ...

  9. re正则表达式13_review of regex symbols

    Review of Regex Symbols This chapter covered a lot of notation, so here’s a quick review of what you ...

  10. Java——文本组件:JTextComponent

    import java.awt.GridLayout; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing ...