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 < 2 k) modulo 2 k.

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 < 2 k) 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 思路:扩展欧几里德板子题,A+Cx=B(mod2^k), 化简有C*x - 2^k*y = B-A, 注意代入的时候带正的2^k,因为求的是x的最小正整数解,和青蛙不一样?(+1s?)
void ex_gcd(LL a, LL b, LL &x, LL &y, LL &d) {
if(!b) {
d = a, x = , y = ;
} else {
ex_gcd(b, a%b, y, x, d);
y -= x * (a / b);
}
} int main() {
ios::sync_with_stdio(false), cin.tie(NULL);
LL a, b, c, k;
while(cin >> a >> b >> c >> k && a+b+c+k) {
LL x, y, d;
if(b-a == ) {
cout << "0\n";
continue;
}
LL MOD = 1LL << k;
ex_gcd(c, MOD, x, y, d);
if((b-a) % d != ) {
cout << "FOREVER\n";
continue;
}
x = x *(b-a) / d;
LL B = MOD / d;
x = (x % B + B) % B;
cout << x << "\n";
}
return ;
}
												

Day7 - F - C Looooops POJ - 2115的更多相关文章

  1. C Looooops POJ - 2115 (exgcd)

    一个编译器之谜:我们被给了一段C++语言风格的循环 for(int i=A;i!=B;i+=C) 内容; 其中所有数都是k位二进制数,即所有数时膜2^k意义下的.我们的目标时球出 内容 被执行了多少次 ...

  2. C Looooops POJ - 2115 拓展gcd 有一个定理待补()

    补算法导论P564 MODULAR-LINEAR-EQUATION-SOLVER算法(P564)

  3. D - C Looooops POJ - 2115 欧几里德拓展

    题意:就是看看for(; ;)多久停止. 最让我蛋疼的是1L和1LL的区别!让我足足wa了12发! 1L 是long类型的, 1LL为long long类型的! 思路: 这就是欧几里德扩展的标准式子了 ...

  4. B - C Looooops POJ - 2115 (扩展欧几里得)

    题目链接:https://cn.vjudge.net/contest/276376#problem/B 题目大意:for( int  i= A ; i != B; i+ = c ),然后给你A,B,C ...

  5. R - C Looooops POJ - 2115 (exgcd)

    题目大意:很好理解,一个for循环语句,从a开始到b结束,步长是c,模数是pow(2,k) 问,最少循环多少次,才能到达b,如果永远都到不了b,输出FOREVER 题解:其实就是求一个线性方程,cx= ...

  6. C Looooops POJ - 2115

    数论好题.. 香! 首先我们看到这一题, 题意是 \[a + c * x \equiv b (mod \ \ 2 ^ k) \] 对此式移一下项, 得 \[c * x \equiv b - a (mo ...

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

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

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

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

随机推荐

  1. c++对象初始化(翁恺c++公开课[10])

    c++对象初始化 就是去调用构造函数来完成初始化操作: 构造函数有无参数的构造函数.有参数构造函数.默认构造函数(编译器给我们实现的)...(拷贝构造函数之后说) 注意:默认构造函数只有在我们自己没有 ...

  2. 学习笔记(6)- pytext训练会话助手

    https://github.com/facebookresearch/pytext https://pytext.readthedocs.io/en/master/ https://pytext.r ...

  3. 各颜色LED压降

    一下是参考1.直插LED压降红:2.0-2.2V黄:1.8-2.0V绿:3.0-3.2V 额定电流约20mA.2.贴片LED压降红:1.82-1.88V,电流5-8mA绿:1.75-1.82V,3-5 ...

  4. 使用jquery select2实现下拉框搜索功能

    由于公司后台系统下拉框数据量太多了,用户操作起来要不方便所以增加了下拉框里面一个搜索功能 1从官网下载jquery select2 下来 地址https://select2.github.io/ 2: ...

  5. iframe结构的网站按F5刷新子页面的实现方式

    有的网站或者后台系统由于页面有公共的部分,比如菜单,会把公共的部分放在一个页面,这里称之为父页面,而把具体的内容放入一个iframe中,之后的请求改变iframe的内容.但是这样会有一个问题,因为浏览 ...

  6. 利用django打造自己的工作流平台(二):疫情统计系统

    相关文章: 利用django打造自己的工作流平台(一):从EXCEL到流程化运作 本文是“利用django打造自己的工作流平台”系列文章的第二篇,在自己开发的工作流平台中添加了一个用于排查统计可能受感 ...

  7. MYSQL--“Row size too large (> 8126)”

    将表的引擎改为MyISAM就可以,如下图. 因为新数据库mysql默认的引擎是InnoDB

  8. mysql 获取刚插入行id汇总

    mysql 获取刚插入行id汇总 我们在写数据库程序的时候,经常会需要获取某个表中的最大序号数, 一般情况下获取刚插入的数据的id,使用select max(id) from table 是可以的.但 ...

  9. Xcode下载途径

    Xcode除了能在Appstore直接下载外,还可以用开发者账号登陆开发者中心[https://developer.apple.com/download/]下载对应的资源. 在开发者中心下载的好处是, ...

  10. Codeforces 1249E By Elevator or Stairs? 题解

    这题其实和 NIKKEI 2019-2-D Shortest Path on a Line 差不多的啦,都是一种最短路的变形,把多个点和边关联了起来. 题面 你要从一楼到 \(n\) 楼去,每层楼可以 ...