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. 牛客网暑期ACM多校训练营(第一场):E-Removal(DP)

    链接:E-Removal 题意:给出序列 s1, s2, ..., sn ,1<=s[i]<=10.问删除m个数后,有多少种不同的序列. 题解:定义dp[i][j]代表长度为i,最末尾的数 ...

  2. 基于Mininet测量路径的损耗率

    基于Mininet测量路径的损耗率 控制器采用POX,基于OVS仿真 Mininet脚本 创建Node mininet.node Node 创建链路连接 mininet.link TCLink 设置i ...

  3. pyqt5实现SMTP邮件发送

    # -*- coding: utf-8 -*- # Form implementation generated from reading ui file 'SMTP.ui' # # Created b ...

  4. 互评Alpha版本——基于spec评论作品

    组名:可以低头,但没必要 组长:付佳 组员:张俊余  李文涛  孙赛佳  田良  于洋  刘欣  段晓睿 一.二次元梦之队----I DO 在测评该项目时,我们组索要了该组的apk程序,安装之后我就开 ...

  5. 细节--服务器mysql空密码

    在部署致服务器的时候 发现mysql密码为空的情况 如果采用 root账户的话 试过很多 比如不写下面这行 <property name="password" value=& ...

  6. ERROR----java.lang.NoClassDefFoundError: org/apache/commons/lang3/StringUtils

    2013-4-28 13:17:57 org.apache.catalina.core.StandardContext filterStart 严重: Exception starting filte ...

  7. 博弈---尼姆博奕(Nimm Game)(重点)

    尼姆博奕(Nimm Game):有三堆各若干个物品,两个人轮流从某一堆取任意多的 物品,规定每次至少取一个,多者不限,最后取光者得胜. 这种情况最有意思,它与二进制有密切关系,我们用(a,b,c)表示 ...

  8. HDU 5234 Happy birthday 01背包

    题目链接: hdu:http://acm.hdu.edu.cn/showproblem.php?pid=5234 bc:http://bestcoder.hdu.edu.cn/contests/con ...

  9. Throwable、Error、Exception、RuntimeException 区别 联系

    1.Throwable 类是 Java 语言中所有错误或异常的超类.它的两个子类是Error和Exception: 2.Error 是 Throwable 的子类,用于指示合理的应用程序不应该试图捕获 ...

  10. nginx 配置文件简介

    主配置文件说明(先将注释部分去掉:sed -ri ‘/^#|[[:space:]]+#/d’ /etc/nginx/nginx.conf) (1)全局配置段 1:指明运行worker进程的用户和组 u ...