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) ...
随机推荐
- 高可用Kubernetes集群-12. 部署kubernetes-ingress
参考文档: Github:https://github.com/kubernetes/ingress-nginx Kubernetes ingress:https://kubernetes.io/do ...
- Ztree结合jbox实现弹窗树结构
点击添加分类,弹出事项选择框为jbox <a href="#" id="down{{row.id}}" style="display:none& ...
- 广东ACM省赛 E题
题意: 输入一个P 使得存在一个一个N大于等于P, 并且存在m 等于 m/n * (m-1)/(n-1)=1/2. 思路 此题可以利用佩尔方程求解, 也可以打表解决.本次我解决利用的是佩尔方程(其实也 ...
- JS中自定义事件的使用与触发
1. 事件的创建 JS中,最简单的创建事件方法,是使用Event构造器: var myEvent = new Event('event_name'); 但是为了能够传递数据,就需要使用 CustomE ...
- python3【基础】-and和or的短路逻辑
1. 表达式只有一个逻辑运算符 python中哪些对象会被当成False,哪些又是True呢? 基本数据类型中的None.任何数值类型中的0.空字符串"",空列表[],空元组()和 ...
- 04慕课网《vue.js2.5入门》——Vue-cli开发todolist
主要文件目录: 文件代码: 根实例,初始化vue: <!--index.html,网站入口页面,和main.jsp组成一套.vue文件,包含--> <!DOCTYPE html> ...
- Alpha 冲刺(8/10)
队名 火箭少男100 组长博客 林燊大哥 作业博客 Alpha 冲鸭鸭鸭鸭鸭鸭鸭鸭! 成员冲刺阶段情况 林燊(组长) 过去两天完成了哪些任务 协调各成员之间的工作 多次测试软件运行 学习OPENMP ...
- 福大软工1816:Beta(7/7)
Beta 冲刺 (7/7) 队名:第三视角 组长博客链接 本次作业链接 团队部分 团队燃尽图 工作情况汇报 张扬(组长) 过去两天完成了哪些任务 文字/口头描述 组织会议 wxpy中多个功能的开发 整 ...
- A10
今日内容: 完善界面.解决剩下的一些问题 明日计划: 无 困难: 无
- 经典面试题(一)附答案 算法+数据结构+代码 微软Microsoft、谷歌Google、百度、腾讯
1. 有一个整数数组,请求出两两之差绝对值最小的值.记住,只要得出最小值即可,不需要求出是哪两个数.(Microsoft) 方法1:两两作差求绝对值,并取最小,O( n2 ). 方法2 ...