【POJ】2115 C Looooops(扩欧)
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
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
- --------------------------------------------------------------------------
题意:在一个k位的机器里(大于2^k就回到0),进行每次增加c的循环,循环终止条件是!=b求循环何时终止。
分析:裸的扩欧。方程:c*x + 2^k*y = b-a 。
- #include <cstdio>
- typedef long long LL;
- LL exgcd(LL a,LL b,LL &x,LL &y)
- {
- int d;
- if(b==)
- {
- x=;y=;return a;
- }
- else
- {
- d=exgcd(b,a%b,y,x);y-=x*(a/b);
- }
- return d;
- }
- int main()
- {
- LL a,b,c,k;
- while(scanf("%lld%lld%lld%lld",&a,&b,&c,&k)&&(a||b||c||k))
- {
- LL i=b-a,x=,y=,d=,p=1LL<<k;//不加LL会爆
- //方程:c*x + 2^k*y = b-a
- d=exgcd(c,p,x,y);
- if(i%d!=)
- {
- printf("FOREVER\n");
- continue;
- }
- p/=d;
- x%=p;
- x*=(i/d)%p;//把倍数乘上
- x=(x%p+p)%p;
- printf("%lld\n",x);
- }
- return ;
- }
【POJ】2115 C Looooops(扩欧)的更多相关文章
- 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 2115 C Looooops扩展欧几里得
题意不难理解,看了后就能得出下列式子: (A+C*x-B)mod(2^k)=0 即(C*x)mod(2^k)=(B-A)mod(2^k) 利用模线性方程(线性同余方程)即可求解 模板直达车 #incl ...
- POJ 2115 C Looooops(扩展欧几里得)
辗转相除法(欧几里得算法) 时间复杂度:在O(logmax(a, b))以内 int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a ...
- 【题解】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( 简单拓欧 + 快速幂 )
链接:传送门 题意:题目中给出一个循环 for (variable = A; variable != B; variable += C) ,这个东东还需要 mod 2^k 问至少多次能退出,如果进入死 ...
- poj 2115 C Looooops(推公式+扩展欧几里得模板)
Description A Compiler Mystery: We are given a C-language style for loop of type for (variable = A; ...
- 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 [题目大意] 求for (variable = A; variable != B; variable += C)的循环次数, ...
- poj 2115 C Looooops——exgcd模板
题目:http://poj.org/problem?id=2115 exgcd裸题.注意最后各种%b.注意打出正确的exgcd板子.就是别忘了/=g. #include<iostream> ...
- POJ 2115 C Looooops
扩展GCD...一定要(1L<<k),不然k=31是会出错的 .... C Looooops Time Limit: 1000MS Mem ...
随机推荐
- grep Pocket Reference读记
1 简介 grep的基本命令格式如下: grep [options] [regexp] [filename] 如果regexp中含有空格,应该使用单引号或双引号括起来.单引号和 ...
- jmeter系列-------脚本编写格式
1.通常会将用户和服务器的一次交互(页面访问或者提交)请求放在一个简单控制器或者事务控制器,例如微课首页里面包含4个接口都放到简单控制器里 或者一个提交可能,会触发3个接口,那么这3个接口放到一个简单 ...
- java使用字节流和字符流实现文件复制
大家在Java开发中都会遇到文件复制的文件,众所周知,需要通过文件输入输出流实现. 那究竟该怎么做那,话不多说,直接上代码: 一,使用字节流复制文件 public class FileByteCopy ...
- CSS3学习手记
--------------------CSS3新增选择器--------------------#E:nth-child(n):匹配元素类型为E且是父元素的第n个子元素#E:nth-last-chi ...
- 【转】Java虚拟机的JVM垃圾回收机制
详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcytp43 1.JVM内存空间 JVM堆(Heap)= 新生代 ...
- JavaWeb程序连接SQLserver数据库
声明:一直以来都以为javaweb程序连接数据库是一个很高大上很难的问题,结果今天学习了一下,不到两个小时就解决了,所以总结一篇博客. JavaWeb程序连接SQLserver数据库分为一下步骤: 1 ...
- poj3723 MST好题 kruskal
#include<iostream> #include<cstring> #include<cstdio> #include<algorithm> us ...
- 自制STP配置实验
本图使用Gns模拟器 实验需求: 1.要求利用vtp实现vlan同步设置 2.要求vtp server实现备份冗余 3.创建vlan 1~10要求MLSW1 是奇数vlan主根 MLSW2 是偶数vl ...
- 团队作业八——第二次团队冲刺(Beta版本)第7天&项目汇总
项目汇总 第一天:http://www.cnblogs.com/newteam6/p/6879383.html 第二天:http://www.cnblogs.com/newteam6/p/688078 ...
- C语言中变量的作用域和生命周期
变量的类型: 1. 局部变量和全局变量 局部变量也称为内部变量. 局部变量是在函数内作定义说明的.其作用域仅限于函数内, 离开该函数后再 使用这种变量是非法的. 全局变量也称为外部变量,它是在函数外部 ...