poj2115 C Looooops
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 29262 | Accepted: 8441 |
Description
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
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
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
#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的更多相关文章
- poj2115 C Looooops(exgcd)
poj2115 C Looooops 题意: 对于C的for(i=A ; i!=B ;i +=C)循环语句,问在k位存储系统中循环几次才会结束. 若在有限次内结束,则输出循环次数. 否则输出死循环. ...
- POJ2115——C Looooops(扩展欧几里德+求解模线性方程)
C Looooops DescriptionA Compiler Mystery: We are given a C-language style for loop of type for (vari ...
- POJ2115 C Looooops[扩展欧几里得]
C Looooops Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 24355 Accepted: 6788 Descr ...
- POJ2115 C Looooops 扩展欧几里德
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - POJ2115 题意 对于C的for(i=A ; i!=B ;i +=C)循环语句,问在k位存储系统中循环几次 ...
- POJ2115 C Looooops ——模线性方程(扩展gcd)
题目链接:http://poj.org/problem?id=2115 C Looooops Time Limit: 1000MS Memory Limit: 65536K Total Submi ...
- POJ2115 C Looooops(线性同余方程)
无符号k位数溢出就相当于mod 2k,然后设循环x次A等于B,就可以列出方程: $$ Cx+A \equiv B \pmod {2^k} $$ $$ Cx \equiv B-A \pmod {2^k} ...
- 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 ...
- POJ2115 - C Looooops(扩展欧几里得)
题目大意 求同余方程Cx≡B-A(2^k)的最小正整数解 题解 可以转化为Cx-(2^k)y=B-A,然后用扩展欧几里得解出即可... 代码: #include <iostream> us ...
- POJ2115 C Looooops(数论)
题目链接. 分析: 数论了解的还不算太多,解的时候,碰到了不小的麻烦. 设答案为x,n = (1<<k), 则 (A+C*x) % n == B 即 (A+C*x) ≡ B (mod n) ...
随机推荐
- New York Comic Con 2013 - 2013年纽约动漫展
New York Comic Con - 2013年纽约动漫展 New York Comic Con is the largest pop culture event on the East Coas ...
- c++ Dynamic Memory (part 2)
Don't use get to initialize or assign another smart pointer. The code that use the return from get c ...
- PHP中的闭包详解
PHP闭包(Closure)使用详解 作者: 字体:[增加 减小] 类型:转载 时间:2013-05-02我要评论 本篇文章介绍了,PHP闭包(Closure)的使用介绍,需要的朋友参考下 不知不 ...
- USACO 1.3.4 Prime Cryptarithm 牛式(模拟枚举)
Description 下面是一个乘法竖式,如果用我们给定的那n个数字来取代*,可以使式子成立的话,我们就叫这个式子牛式. * * * x * * ------- * * * * * * ------ ...
- 《IT小小鸟》的阅读心得
新年过后我们迎来大一下学期,想想刚迈入大学的我们,充满着好奇与兴奋,仿佛就在昨天.时光飞逝而今,虽经过一学期的学习,仍对计算机专业充满困惑,对未来充满迷茫. 在我感到迷茫的时候,老师给我们介绍了这样的 ...
- mnist测试
第一步:进入caffe目录 第二步:获取mnist数据集 ./data/mnist/get_mnist.sh 第三步:创建lmdb ./examples/mnist/create_mnist.sh 第 ...
- 第八章 Mysql运算符
算术运算符 符号 表达式形式 作用 + x1+x2 加法 - x1-x2 减法 * x1*x2 乘法 / x1/x2 除法 div x1 div x2 同上 % x1%x2 取余 mod mod(x1 ...
- TCP系列51—拥塞控制—14、TLP、ER与拥塞控制
一.概述 这里的重点是介绍TLP.ER与拥塞控制并不是介绍TLP和ER本身,因此TLP和ER的详细内容请翻前文. 在TLP与拥塞控制的交互中有几个点需要注意 1.TLP触发的重传后,TCP仍然处于Op ...
- 在原有的基础之上,启用NAT模型
# 给虚拟主机实例添加一个网关 route add default gw 192.168.23.1 # 在宿主机打开网卡间转发功能 echo 1 > /proc/sys/net/ipv4/i ...
- set集合,深浅拷贝以及部分知识点补充
目录: 1.基础数据类型补充 2.set集合 3.深浅拷贝 一,基础数据类型补充 字符串的基本操作 li = ["李李嘉诚", "麻花藤", "⻩黄海 ...