POJ 2115:C Looooops
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 19536 | Accepted: 5204 |
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
< 2k) are the parameters of the loop.
The input is finished by a line containing four zeros.
Output
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
题意是问在
for (variable = A; variable != B; variable += C)
这样的情况下,循环多少次。
当中全部的数要mod 2的k次方。所以方程就是(A+C*x)%(2^k)=B,变换一下就是-C*x+(2^k)*y=A-B。解这个方程的最小正数x就可以。
又是扩展欧几里德。
代码:
#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std; long long yue; void ex_gcd(long long a,long long b, long long &xx,long long &yy)
{
if(b==0)
{
xx=1;
yy=0;
yue=a;
}
else
{
ex_gcd(b,a%b,xx,yy); long long t=xx;
xx=yy;
yy=t-(a/b)*yy;
}
} int main()
{
long long A,B,C,k,k2,xx,yy; while(scanf_s("%lld%lld%lld%lld",&A,&B,&C,&k))
{
if(!A&&!B&&!C&&!k)
break; k2=(1LL<<k);
ex_gcd(-C,k2,xx,yy); if((A-B)%yue)
{
cout<<"FOREVER"<<endl;
}
else
{
xx=xx*((A-B)/yue);
long long r=k2/yue;
if(r<0)
xx=(xx%r-r)%r;
else
xx=(xx%r+r)%r;
printf("%lld\n",xx);
}
}
return 0;
}
POJ 2115:C Looooops的更多相关文章
- 【poj 2115】C Looooops(数论--拓展欧几里德 求解同余方程 模版题)
题意:有一个在k位无符号整数下的模型:for (variable = A; variable != B; variable += C) statement; 问循环的次数,若"永不停息&q ...
- 【题解】POJ 2115 C Looooops (Exgcd)
POJ 2115:http://poj.org/problem?id=2115 思路 设循环T次 则要满足A≡(B+CT)(mod 2k) 可得 A=B+CT+m*2k 移项得C*T+2k*m=B-A ...
- 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( ...
- POJ 3321:Apple Tree + HDU 3887:Counting Offspring(DFS序+树状数组)
http://poj.org/problem?id=3321 http://acm.hdu.edu.cn/showproblem.php?pid=3887 POJ 3321: 题意:给出一棵根节点为1 ...
- POJ 3252:Round Numbers
POJ 3252:Round Numbers Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10099 Accepted: 36 ...
- POJ 2115 C Looooops(模线性方程)
http://poj.org/problem?id=2115 题意: 给你一个变量,变量初始值a,终止值b,每循环一遍加c,问一共循环几遍终止,结果mod2^k.如果无法终止则输出FOREVER. 思 ...
- poj 2115 C Looooops——exgcd模板
题目:http://poj.org/problem?id=2115 exgcd裸题.注意最后各种%b.注意打出正确的exgcd板子.就是别忘了/=g. #include<iostream> ...
- poj 2115 Looooops
C Looooops Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 23637 Accepted: 6528 Descr ...
- Poj 2115 C Looooops(exgcd变式)
C Looooops Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 22704 Accepted: 6251 Descripti ...
随机推荐
- asp.net 定时执行任务代码 定时采集数据
using System; using System.Data; using System.Configuration; using System.Collections; using System. ...
- C# •MouseDown •MouseDown •MouseUp 的先后顺序
鼠标按下事件发生的顺序 MouseDown event. Click event. MouseDownevent. MouseUp event. 可以写个测试,放一个picture控件 private ...
- Table折叠小技巧html-demo
1.要做一个table折叠的展示文本框直接上代码 html: <!DOCTYPE html> <html> <head> <title>table-折叠 ...
- http协议版本历史
1.http 0.9 2.http 1.0 3. http 1.1 4.http 2.0 推送:主动发送js.css推送到浏览器. 二进制流:可以并行发送数据. 2019.3.18补充: (1)htt ...
- Hibernate学习笔记一:项目创建与基本配置文件
转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6760773.html 一:ORM ORM:对象-关系 映射. 即:把Java中有关联关系的对象,转换成关系型 ...
- anagrams 查找序列里具有相同字符但顺序不同的单词
[LeetCode] Anagrams Given an array of strings, return all groups of strings that are anagrams. Not ...
- V-rep学习笔记:Reflexxes Motion Library 3
路径规划 VS 轨迹规划 轨迹规划的目的是将输入的简单任务描述变为详细的运动轨迹描述.注意轨迹和路径的区别:Trajectory refers to a time history of positio ...
- 改变mysql客户端输出的字符串编码
在客户端改变中文输出的编码,通常以gbk输出,因为电脑常见的是gbk编号形式 目的:不改变编码,输出中文的时候,可能会出现乱码的情况, set names gbk 在客户端以gbk编码显示需要输出的内 ...
- adb forward交互流程
命令:adb forward tcp:6100 tcp:7100 // PC上所有6100端口通信数据将被重定向到手机端7100端口server上 或者adb forward tcp:6100 loc ...
- Retrofit三步理解之中的一个 ------------------ Retrofit的简单使用总结
概念: Retrofit一開始看起来使用比較麻烦是由于它和其它网络请求框架不同的是它是通过注解和interface来进行网络请求,而且须要对返回数据进行特殊处理才干使用. 1. 简单使用,请求返回St ...