C Looooops
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 22704 Accepted: 6251
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
/*
exgcd变式.
要求:(a+c*x)mod2^k=b.
变形得到:c*xmod2^k=b-a.
即 c*x=(b-a)mod2^k.
用同余方程求解.
(
mod运算是最"自由"的运算
符合常见的运算律.
)
*/
#include<iostream>
#include<cstdio>
#define LL long long
using namespace std;
LL a,b,c,k,x,y;
LL mi(int x)
{
LL tot=1;
for(int i=1;i<=x;i++) tot<<=1;
return tot;
}
LL exgcd(LL a,LL b)
{
if(!b)
{
x=1;y=0;return a;
}
LL d=exgcd(b,a%b);
LL tot=x;
x=y;
y=tot-a/b*y;
return d;
}
int main()
{
int k;
while(scanf("%I64d%I64d%I64d%d",&a,&b,&c,&k)&&a&&b&&c&&k)
{
x=0;y=0;
LL d=exgcd(c,mi(k));
if((b-a)%d) printf("FOREVER\n");
else
{
x=x*(b-a)/d;
LL r=mi(k)/d;
x=(x%r+r)%r;
printf("%I64d\n",x);
}
}
return 0;
}

Poj 2115 C Looooops(exgcd变式)的更多相关文章

  1. poj 2115 C Looooops——exgcd模板

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

  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(扩展欧几里得应用)

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

  4. POJ 2115 C Looooops(Exgcd)

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

  5. POJ 2115 C Looooops (扩展欧几里德 + 线性同余方程)

    分析:这个题主要考察的是对线性同余方程的理解,根据题目中给出的a,b,c,d,不难的出这样的式子,(a+k*c) % (1<<d) = b; 题目要求我们在有解的情况下求出最小的解,我们转 ...

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

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

  7. POJ - 2115 C Looooops(扩展欧几里德求解模线性方程(线性同余方程))

    d.对于这个循环, for (variable = A; variable != B; variable += C) statement; 给出A,B,C,求在k位存储系统下的循环次数. 例如k=4时 ...

  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...一定要(1L<<k),不然k=31是会出错的 ....                        C Looooops Time Limit: 1000MS   Mem ...

随机推荐

  1. C# WinForm修改Panel边框颜色

    private void panel1_Paint(object sender, PaintEventArgs e) { ControlPaint.DrawBorder(e.Graphics, thi ...

  2. [二]poi实践一

    1.创建时间格式的cell 2.创建不同格式的cell(字符串.布尔.数值) 3.读取遍历xls文件 4.抽取excel的内容

  3. 用document.title=“xxx”动态修改title,在ios的微信下面不生效

    单页应用里整个页面只会在第一次完全刷新,后面只会局部刷新(一般不包括head及里面的title),所以无法在服务器端控制title,只能在页面刷新的时候通过js修改title.常规做法如下,可惜在iO ...

  4. Yii2 富头像上传

    前台html: <div class="xz_img2 text-center"> <div class="images_flow" id=& ...

  5. jrae源代码解析(二)

    本文细述上文引出的RAECost和SoftmaxCost两个类. SoftmaxCost 我们已经知道.SoftmaxCost类在给定features和label的情况下(超參数给定),衡量给定权重( ...

  6. Fatal error: Call to undefined function mysql_connect()

    我在进行PHP环境搭建:Windows 7下安装配置PHP+Mysql+apache环境时,之前都没有什么问题,只是在验证PHP是否能连接Mysql时出现如下错误:Fatal error: Call ...

  7. HDU 4849-Wow! Such City!(最短路)

    Wow! Such City! Time Limit: 15000/8000 MS (Java/Others)    Memory Limit: 102400/102400 K (Java/Other ...

  8. Cocos2d-x滚动列表具体解释(CCScrollView的使用)

    今天要写一个滚动列表功能,类似以下这样.(图片资源都是自己从天天酷跑里面抠的,仅用于学习方便) 首先,这样一个列表就和iOS里面的UITableView没什么两样,当然,Android中肯定也存在类似 ...

  9. 解决Eclipse自己主动补充问题空间

    假设我们使用增强的战斗有时当代码完成功能 String ss时却发挥String ssString; Integer i 但打了 Integer integer; 很麻烦. 为这包的关系 org.ec ...

  10. 1.关于UltraEdit中的FTP和Tenent配置,UE远程连接Linux进行文件操作

     1  安装UltraEdit 2  配置FTP相关的配置 文件àFTP/Tenet(T)à watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdG90b3 ...