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 ...
随机推荐
- linux c 及 c++打印调用者函数caller function的方法,包括arm c平台
一般情况下,编译的时候可能需要加 -g 选项,对于android ndk的-g选项添加请参见android类目下的另一篇文章. 以下文章中的__builtin_return_address() 宏,若 ...
- HTML5初学者福利!11个在线学习网站推荐
HTML5初学者福利!11个在线学习网站推荐 HTML5的强大及流行趋势,让更多的人想要系统的对它进行学习.而大多数人获取HTML5知识的重要途径都是网络,不过面对五花八门的搜索结果,是不是觉得摸不着 ...
- oracle 日期字段的处理
a. oracle plsql 如何查询两个间隔日期之间的数据 1) 方法一:Select * from Tables where time >= to_date('2013-01-02 19: ...
- C++里面方便的打印日志到文件
ofstream write; //write只是个名字 你可以定义为任何其他的名字 write.open("D:\prj\Robot\dev\face\facerecog\facereco ...
- IOS 获取屏幕尺寸
CGRect frame = [[UIScreen mainScreen] bounds]; NSLog(@"frame :%@",frame); 这样输入是null NSL ...
- [OGRE]基础教程来七发:来谈一谈缓冲绑定
上一章我们处理监听的方案是,每一帧只处理一次. 这一次,当鼠标键盘的事件发生时,我们会立即处理它. 这里只是对缓冲输入的一个简单介绍,而不是完整的如何使用OIS的教程. 若想了解更多内容,请查阅相关的 ...
- 【DB】HBase的基本概念
一 Hbase是个啥东东? 在说Hase是个啥家伙之前,首先我们来看看两个概念.面向行存储和面向列存储.面向行存储.我相信大伙儿应该都清楚,我们熟悉的RDBMS就是此种类型的.面向行存储的数据库主要 ...
- TCP并发server,每个客户一个子进程
今天笔者带来的是server型号第一,这是最经常使用的模型的最基本的一个–TCP并发server,每个客户一个子进程. 首先简单介绍:TCP并发server,每个客户一个子进程,并发server调用f ...
- DHTMLX 前端框架 建立你的一个应用程序教程(三)--添加一个菜单
菜单的介绍 这篇我们介绍将菜单组建添加到上节中的布局中: 我们不对菜单做任何处理 只是在这里填充作为界面的一部分. 这里我们介绍的是dhtmlxMenu 组件. 这个组件的数据我们可以从XML或者J ...
- 【转】CCUserDefault类深入分析——2013-08-25 22
http://game.dapps.net/gamedev/game-engine/8792.html 另:本章所用Cocos2d-x版本为: 2.1.1 (2013-01-28) 大家好,今天我们来 ...