POJ 2115 C Looooops(Exgcd)
【题目链接】 http://poj.org/problem?id=2115
【题目大意】
求for (variable = A; variable != B; variable += C)的循环次数,
其中变量为k比特无符号整数。
【题解】
题目等价于求解Cx=(B–A)(mod 2^k),利用扩展欧几里得算法可以求解该问题
【代码】
- #include <algorithm>
- #include <cstring>
- #include <cstdio>
- using namespace std;
- typedef long long ll;
- ll exgcd(ll a,ll b,ll&x,ll&y){
- if(!b)return x=1,y=0,a;
- ll d=exgcd(b,a%b,x,y),t=x;
- return x=y,y=t-a/b*y,d;
- }
- ll A,B,C,k;
- int main(){
- while(scanf("%lld%lld%lld%lld",&A,&B,&C,&k),A+B+C+k){
- ll a=C,b=B-A,n=1LL<<k,x,y,d=exgcd(a,n,x,y);
- if(b%d)puts("FOREVER");
- else{
- x=(x*(b/d))%n; //方程ax=b(mod n)的最小解
- x=(x%(n/d)+n/d)%(n/d); //方程ax=b(mod n)的最小整数解
- printf("%I64d\n",x);
- }
- }return 0;
- }
POJ 2115 C Looooops(Exgcd)的更多相关文章
- 【题解】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(扩展欧几里德求解模线性方程(线性同余方程))
d.对于这个循环, for (variable = A; variable != B; variable += C) statement; 给出A,B,C,求在k位存储系统下的循环次数. 例如k=4时 ...
- 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( 简单拓欧 + 快速幂 )
链接:传送门 题意:题目中给出一个循环 for (variable = A; variable != B; variable += C) ,这个东东还需要 mod 2^k 问至少多次能退出,如果进入死 ...
- POJ 2142 The Balance(exgcd)
嗯... 题目链接:http://poj.org/problem?id=2142 AC代码: #include<cstdio> #include<iostream> using ...
- POJ 3669 Meteor Shower(流星雨)
POJ 3669 Meteor Shower(流星雨) Time Limit: 1000MS Memory Limit: 65536K Description 题目描述 Bessie hears ...
- POJ 3253 Fence Repair (优先队列)
POJ 3253 Fence Repair (优先队列) Farmer John wants to repair a small length of the fence around the past ...
- 【POJ 3071】 Football(DP)
[POJ 3071] Football(DP) Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4350 Accepted ...
随机推荐
- 微信小程序base64编码解码以及utf-8解码
function base64_encode (str) { // 编码,配合encodeURIComponent使用 var c1, c2, c3; var base64EncodeChars = ...
- git使用笔记(八)团队协作
By francis_hao Nov 24,2016 本文由 刘英皓 创作,采用 知识共享 署名-非商业性使用-相同方式共享 3.0 中国大陆 许可协议进行许可.欢迎转载,请注明出处 ...
- github导入springboot maven项目
1.在GitHub里force喜欢的项目,获取GitHub项目地址,eclipse---import---project from git---clone uri---next---finish,项目 ...
- js用for of 遍历数组
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- AngularJs开发——控制器间的通信
AngularJs开发——控制器间的通信 指令与控制器之间通信,无非是以下几种方法: 基于scope继承的方式 基于event传播的方式 service的方式 基于scope继承的方式 最简单的让控制 ...
- ZooKeeper Watcher注意事项
zookeeper watch的定义如下:watch事件是一次性触发器,当watch监视的数据发生变化时,通知设置了该watch的client,即watcher. 需要注意三点: 1.一次性触发器 c ...
- (转)C/S 与 B/S 区别
感谢:http://www.cnblogs.com/xiaoshuai/archive/2010/05/25/1743741.html C/S结构,即Client/Server(客户机/服务器)结构, ...
- bzoj 2039 最小割模型
比较明显的网络流最小割模型,对于这种模型我们需要先求获利的和,然后减去代价即可. 我们对于第i个人来说, 如果选他,会耗费A[I]的代价,那么(source,i,a[i])代表选他之后的代价,如果不选 ...
- Raspberry Pi 3b+ 配置摄像头
1.开启摄像头硬件接口 raspi-config > Interfacing Options > Camera 2.测试 raspistill -v -o test.jpg
- python基础代码(猜年龄、从最内层跳出多层循环、简单的购物车程序)
1.猜年龄 , 可以让用户最多猜三次! age = 55 i=0 while i<3: user_guess = int (input ("input your guess:" ...