C Looooops
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 29262   Accepted: 8441

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

大致题意:求在mod 2^k下的一个for循环要运行多少次.
分析:列出同余式,扩展欧几里得解决.
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; typedef long long ll;
ll a,b,c,k,A,B,C,d; ll qpow(ll b)
{
ll res = ,x = ;
while (b)
{
if (b & )
res *= x;
x *= x;
b >>= ;
}
return res;
} ll gcd(ll a,ll b)
{
if (!b)
return a;
return gcd(b,a % b);
} void exgcd(ll a,ll b,ll &x,ll &y)
{
if (!b)
{
x = ;
y = ;
return;
}
exgcd(b,a % b,x,y);
ll t = x;
x = y;
y = t - (a / b) * y;
} int main()
{
while (scanf("%lld%lld%lld%lld",&a,&b,&c,&k) == )
{
if (!a && !b && !c && !k)
break;
C = b - a;
A = c;
B = qpow(k);
d = gcd(A,B);
if (C % d != || (b >= B || a >= B || c >= B))
printf("FOREVER\n");
else
{
ll x,y;
exgcd(A,B,x,y);
x = x * C / d;
B /= d;
x = (x % B + B) % B;
printf("%lld\n",x);
}
} return ;
}

poj2115 C Looooops的更多相关文章

  1. poj2115 C Looooops(exgcd)

    poj2115 C Looooops 题意: 对于C的for(i=A ; i!=B ;i +=C)循环语句,问在k位存储系统中循环几次才会结束. 若在有限次内结束,则输出循环次数. 否则输出死循环. ...

  2. POJ2115——C Looooops(扩展欧几里德+求解模线性方程)

    C Looooops DescriptionA Compiler Mystery: We are given a C-language style for loop of type for (vari ...

  3. POJ2115 C Looooops[扩展欧几里得]

    C Looooops Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 24355   Accepted: 6788 Descr ...

  4. POJ2115 C Looooops 扩展欧几里德

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - POJ2115 题意 对于C的for(i=A ; i!=B ;i +=C)循环语句,问在k位存储系统中循环几次 ...

  5. POJ2115 C Looooops ——模线性方程(扩展gcd)

    题目链接:http://poj.org/problem?id=2115 C Looooops Time Limit: 1000MS   Memory Limit: 65536K Total Submi ...

  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. POJ2115 C Looooops 模线性方程(扩展欧几里得)

    题意:很明显,我就不说了 分析:令n=2^k,因为A,B,C<n,所以取模以后不会变化,所以就是求(A+x*C)%n=B 转化一下就是求 C*x=B-A(%n),最小的x 令a=C,b=B-A ...

  8. POJ2115 - C Looooops(扩展欧几里得)

    题目大意 求同余方程Cx≡B-A(2^k)的最小正整数解 题解 可以转化为Cx-(2^k)y=B-A,然后用扩展欧几里得解出即可... 代码: #include <iostream> us ...

  9. POJ2115 C Looooops(数论)

    题目链接. 分析: 数论了解的还不算太多,解的时候,碰到了不小的麻烦. 设答案为x,n = (1<<k), 则 (A+C*x) % n == B 即 (A+C*x) ≡ B (mod n) ...

随机推荐

  1. thinkphp5框架生成二维码

    二话不说,先上代码: 第一中: 不用再本地保存文件,直接在前台页面显示: 这是控制器里面的内容,哦,对啦,首先要下载SDK:.phpqrcode类文件下载,下载地址:https://sourcefor ...

  2. Python异常(基础) except

    为什么要异常处理机制:在程序调用层数较深时,向主调函数传递错误信息需要层层return 返回比较麻烦,用异常处理机制可以较简单的传送错误信息 什么是错误 错误是指由于逻辑或语法等导致一个程序已无法正常 ...

  3. git blame 查看某行代码提交记录

    1. 在当前git项目目录下执行 git blame -L 38,38 <filename> 例子:  git blame -L 38,38 src/component/BarCode/i ...

  4. python3【基础】-and和or的短路逻辑

    1. 表达式只有一个逻辑运算符 python中哪些对象会被当成False,哪些又是True呢? 基本数据类型中的None.任何数值类型中的0.空字符串"",空列表[],空元组()和 ...

  5. Thunder团队第七周 - Scrum会议5

    Scrum会议5 小组名称:Thunder 项目名称:i阅app Scrum Master:邹双黛 工作照片: 宋雨沉迷于照相无法自拔,所以不在相片中. 参会成员: 王航:http://www.cnb ...

  6. 网页调用vlc并播放网络视频

    环境:windows/android/ios windows端保存以下内容为reg文件并运行 Windows Registry Editor Version 5.00 [HKEY_CLASSES_RO ...

  7. 在Asp.Net中使用Redis【本文摘自智车芯官网】

    Redis安装 在安装之前需要获取Redis安装包.在这里我们就不详细介绍安装包的获取了.这里Redis-x64-3.2.100.zip安装包为例通过dos命令取安装.通过dos命令找到安装目录. 在 ...

  8. 全栈https

    前言:在httpd中使用已经被私有CA签署的证书,对外提供一个https服务 配置httpd服务支持使用https 1:配置httpd支持使用ssl,及使用的证书 yum -y install mod ...

  9. 第七周PSP(10.27-11.03)

    psp   进度条 项目 细则 总计 代码行数   0 随笔字数   0 知识点   无 累计曲线 饼图

  10. PHP面向对象之final关键字

    最终类 最终类,其实就是一种特殊要求的类:要求该类不允许往下继承下去. 形式: final  class  类名{ //类的成员定义...跟一般类的定义一样! } 最终方法 最终方法,就是一个不允许下 ...