Poj 2115 C Looooops(exgcd变式)
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变式)的更多相关文章
- poj 2115 C Looooops——exgcd模板
题目:http://poj.org/problem?id=2115 exgcd裸题.注意最后各种%b.注意打出正确的exgcd板子.就是别忘了/=g. #include<iostream> ...
- 【题解】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 ...
- 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( ...
- POJ 2115 C Looooops(Exgcd)
[题目链接] http://poj.org/problem?id=2115 [题目大意] 求for (variable = A; variable != B; variable += C)的循环次数, ...
- POJ 2115 C Looooops (扩展欧几里德 + 线性同余方程)
分析:这个题主要考察的是对线性同余方程的理解,根据题目中给出的a,b,c,d,不难的出这样的式子,(a+k*c) % (1<<d) = b; 题目要求我们在有解的情况下求出最小的解,我们转 ...
- POJ 2115 C Looooops(模线性方程)
http://poj.org/problem?id=2115 题意: 给你一个变量,变量初始值a,终止值b,每循环一遍加c,问一共循环几遍终止,结果mod2^k.如果无法终止则输出FOREVER. 思 ...
- POJ - 2115 C Looooops(扩展欧几里德求解模线性方程(线性同余方程))
d.对于这个循环, for (variable = A; variable != B; variable += C) statement; 给出A,B,C,求在k位存储系统下的循环次数. 例如k=4时 ...
- POJ 2115 C Looooops扩展欧几里得
题意不难理解,看了后就能得出下列式子: (A+C*x-B)mod(2^k)=0 即(C*x)mod(2^k)=(B-A)mod(2^k) 利用模线性方程(线性同余方程)即可求解 模板直达车 #incl ...
- POJ 2115 C Looooops
扩展GCD...一定要(1L<<k),不然k=31是会出错的 .... C Looooops Time Limit: 1000MS Mem ...
随机推荐
- Zookeeper系列(二)特征及应用场景
zookeeper类似一个分布式的文件系统,每个节点可以有和它自身或它的子节点相关联的数据,此外指向节点的路劲必须使用绝对路径(不能使用相对路劲): Znode 对应目录树中的的一个节点,并拥有一 ...
- Linux必学的命令
Linux必学的命令Linux提供了大量的命令,利用它可以有效地完成大量的工作,如磁盘操作.文件存取.目录操作.进程管理.文件权限设定等.所以,在Linux系统上工作离不开使用系统提供的命令.要想真正 ...
- jenkens构建脚本
Build Root POM Goals and options Command # consts SERVER="192.168.60.209" DEPLOY=" ...
- S2 结业考试前改错汇总
1. PS:正确答案是A:枚举是值类型,一个类的对象是引用类型. 2. 每张表最多包含1个聚集索引.并且聚集索引会决定记录储存的物理位置.聚集索引不一定要建立在主键字段上.一张表可以没有任何索引. 3 ...
- 如何在Windows中打开多个Windows Media Player
博客搬到了fresky.github.io - Dawei XU,请各位看官挪步.最新的一篇是:如何在Windows中打开多个Windows Media Player.
- Java实现文件的RSA和DES加密算法
根据密钥类型不同将现代密码技术分为两类:对称加密算法(秘密钥匙加密)和非对称加密算法(公开密钥加密) 对称钥匙加密系统是加密和解密均采用同一把秘密钥匙,而且通信双方都必须获得这把钥匙,并保持钥匙的秘密 ...
- 在TextView使用部分颜色文字
/** * change a part of string color. * * @param string * whole string. * @param subString * the sub ...
- js 获取浏览器版本号
1.在web开发中,会常常让你推断当前使用的是那个浏览器及浏览器的那个版本号,依据浏览器版本号来调整CSS的样式, 使在web界面在各个浏览器展现达到最佳的效果,以下是获取当前浏览器的代码: getB ...
- 系统数据文件和信息之附加组ID
4.2BSD引入了附加组ID(supplementary group ID)的概念.我们不仅可以属于口令文件记录项中组ID所对应的组,也可属于多达16个另外的组.文件访问权限检查相应被修改为:不仅将进 ...
- C# Attribute(中)——Attribute本质论
小序: 上篇里,我们把Attribute“粘”在类的成员方法上show了一把,让Attribute跟大家混了个脸儿熟.中篇里,我们将探讨“究竟什么是Attrib ...