C Looooops
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 29061   Accepted: 8360

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

大致题意:

对于C的for(i=A ; i!=B ;i +=C)循环语句,问在k位存储系统中循环几次才会结束。

若在有限次内结束,则输出循环次数。

否则输出死循环。

解题思路:

题意不难理解,只是利用了 k位存储系统 的数据特性进行循环。

例如int型是16位的,那么int能保存2^16个数据,即最大数为65535(本题默认为无符号),

当循环使得i超过65535时,则i会返回0重新开始计数

如i=65534,当i+=3时,i=1

其实就是 i=(65534+3)%(2^16)=1

由此我们可以得到一个方程  A+CX = B(modn)n = 1 << k;

即 CX = (B-A)% n;

b = B-A;

该方程有解的充要条件为 gcd(C,n) | b ,即 b% gcd(a,n)==0

所以当b% gcd(C,n)!=0方程无解输出FOREVER

然后再求b%gcd(C,n)为0时的最小x解

令d = gcd(C,n)

引入欧几里得扩展方程  d=Cx+by

(即最开始求CX+ny=1的方程解,最后再乘(b/d))  用欧几里德扩展定理求出x(最小解)与gcd(X,n)

注意x0可能为负,因此要先 + n/d 再模n/d。

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std; typedef long long ll;
int a,b,c,k; ll exgcd(ll a,ll b,ll &x,ll &y){
if(b == ){
x = ;
y = ;
return a;
}
ll d = exgcd(b,a%b,x,y);
ll tmp = x;
x = y;
y = tmp - a/b*y;
return d;
} ll quick_mod(ll a,ll b){
ll ans = ;
while(b){
if(b%!=){
ans *= a;
b --;
}
b /= ;
a *= a;
}
return ans;
}
int main(){
while(cin >> a >> b >> c >> k){
if(!a && !b && !c && !k){
break;
}
ll n = quick_mod(,k);
ll x,y;
ll d = exgcd(c,n,x,y); //求a,n的最大公约数d=gcd(c,n)和方程d=cx+by的系数x、y
b = b - a;
if( b%d != ){//方程 cx=b(mod n) 无解
cout << "FOREVER" << endl;
continue;
}
x = (x*(b/d))%n; //方程cx=b(mod n)的最小解
x = (x%(n/d)+n/d)%(n/d); //方程ax=b(mod n)的最小正整数解
cout << x << endl;
}
return ;
}

poj 2115 求线性同余方程 C Looooops(好理解欧几里德扩展定理怎么应用)的更多相关文章

  1. 初等数论-Base-2(扩展欧几里得算法,同余,线性同余方程,(附:裴蜀定理的证明))

    我们接着上面的欧几里得算法说 扩展欧几里得算法 扩展欧几里德算法是用来在已知a, b求解一组x,y,使它们满足贝祖等式\(^①\): ax+by = gcd(a, b) =d(解一定存在,根据数论中的 ...

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

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

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

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

  4. POJ2115:C Looooops(一元线性同余方程)

    题目: http://poj.org/problem?id=2115 要求: 会求最优解,会求这d个解,即(x+(i-1)*b/d)modm;(看最后那个博客的链接地址) 前两天用二元一次线性方程解过 ...

  5. POJ 1061 - 青蛙的约会 - [exgcd求解一元线性同余方程]

    先上干货: 定理1: 如果d = gcd(a,b),则必能找到正的或负的整数k和l,使ax + by = d. (参考exgcd:http://www.cnblogs.com/dilthey/p/68 ...

  6. POJ2115 C Looooops(线性同余方程)

    无符号k位数溢出就相当于mod 2k,然后设循环x次A等于B,就可以列出方程: $$ Cx+A \equiv B \pmod {2^k} $$ $$ Cx \equiv B-A \pmod {2^k} ...

  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扩展欧几里得

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

  9. poj2115-C Looooops -线性同余方程

    线性同余方程的模板题.和青蛙的约会一样. #include <cstdio> #include <cstring> #define LL long long using nam ...

随机推荐

  1. java基本数据类型和包装类之间的区别

    1.声明方式不同,基本类型不适用new关键字,而包装类型需要使用new关键字来在堆中分配存储空间: 2.存储方式及位置不同,基本类型是直接将变量值存储在堆栈中,而包装类型是将对象放在堆中,然后通过引用 ...

  2. iOS的录屏功能

    iOS的录屏功能其实没什么好说的,因为网上的教程很多,但是网上的Demo无一例外几乎都有一个bug,那就是iPad上会出现闪退,这也体现了国内的教程文档的一个特点,就是抄袭,教程几乎千篇一律,bug也 ...

  3. Apache 80端口可以访问,8080却不可访问

    RT, 记录一下,后面看是否有解决方案.

  4. 【0802 | Day 7】Python进阶(一)

    目 录  数字类型的内置方法 一.整型内置方法(int) 二.浮点型内置方法(float) 字符串类型内置方法 一.字符串类型内置方法(str) 二.常用操作和内置方法 优先掌握: 1.索引取值 2. ...

  5. 【0801 | Day 6】Python基础(四)

    Part 13 流程控制之while循环 一.语法 while 条件 code 1 code 2 code 3 ... ​ while True: print('*1'*100) print('*2' ...

  6. python 函数和函数名的应用

    一.函数 1.函数定义 def 关键字 -- 定义 func 函数名 -- 和变量定义规则一样 ()必须要写格式 : 声明语句结束 def my_len(): ​ 函数体 def func(a:int ...

  7. Unity进阶之:Shader渲染

    版权声明: 本文原创发布于博客园"优梦创客"的博客空间(网址:http://www.cnblogs.com/raymondking123/)以及微信公众号"优梦创客&qu ...

  8. Codeforces 343D Water Tree

    题意简述 维护一棵树,支持以下操作: 0 v:将以v为跟的子树赋值为1 1 v:将v到根节点的路径赋值为0 2 v:询问v的值 题解思路 树剖+珂朵莉树 代码 #include <set> ...

  9. c# 将dwg文件转化为pdf

    https://blog.csdn.net/mywaster/article/details/50220379 最近做一个项目,要求将dwg文件转化为pdf,开发工具VS2010 + AutoCad ...

  10. Linux与Unix到底有什么不同?

    来自:开源中国 原文:Linux vs. Unix: What's the difference? 链接: https://opensource.com/article/18/5/difference ...