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 ...
随机推荐
- KindEditor-编辑器配置参数属性
KindEditor-源码分析 通过使用KE.show(config)方法即可将编辑器添加到文档中.下面是一段源码: KE.show = function(args) { KE.init(ar ...
- 分布式系统介绍-PNUTS
PNUTS是Yahoo!的分布式数据库系统,支持地域上分布的大规模并发操作.它根据主键的范围区间或者其哈希值的范围区间将表拆分为表单元(Tablet),多个表单元存储在一个服务器上.一个表单元控制器根 ...
- 微信小程序 - loading(组件)
更新日期: 2019/3/8:首次发布 2019/3/12:增加loadOpacity透明度控制,默认0.5. 以及修改居中方式 Loading 参数: 1. type:loading(必需参数) 2 ...
- 升级华为s2016
Part 1 升级Bootrom 概述:启动启动菜单,用xmodem协议升级BootRom 升级的文件如下: wnm2.2.3-0004.zip :web网管zip压缩包文件. S2008_16-VR ...
- MySql安装完成后设置远程访问的角本
一.方法: 登陆安装Mysql的机器的Mysql, 执行: GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'password001!' ...
- 算法笔记_228:信用卡号校验(Java)
目录 1 问题描述 2 解决方案 1 问题描述 当你输入信用卡号码的时候,有没有担心输错了而造成损失呢?其实可以不必这么担心,因为并不是一个随便的信用卡号码都是合法的,它必须通过Luhn算法来验证 ...
- ES5学习笔记
转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/7234053.html 一:基础 1:语法 ECMAScript 中的变量无特定的类型,定义变量时只用 var ...
- ADAMS绳索仿真
在实际生活中有大量的绳索类传动运动形式.绳索类部件看似简单,但是用计算机软件对其模拟时却存在较大障碍.以钢丝绳为例,通常都是通过细长的钢丝螺旋缠绕在一起形成,在工作时当施加拉力载荷于其上,除了材料自身 ...
- 机器人运动规划中的构形空间(Configuration Space)
A key concept in motion planning is configuration space, or C-space for short. Every point in the C- ...
- Excel Vlookup 列查找函数
列查找函数语法:vlookup(lookup_value,table_array,col_index_num,[range_lookup]) lookup_value:要查找的值,数值.引用或文本字符 ...