Day7 - F - C Looooops POJ - 2115
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 < 2 k) modulo 2 k.
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 思路:扩展欧几里德板子题,A+Cx=B(mod2^k), 化简有C*x - 2^k*y = B-A, 注意代入的时候带正的2^k,因为求的是x的最小正整数解,和青蛙不一样?(+1s?)
void ex_gcd(LL a, LL b, LL &x, LL &y, LL &d) {
if(!b) {
d = a, x = , y = ;
} else {
ex_gcd(b, a%b, y, x, d);
y -= x * (a / b);
}
}
int main() {
ios::sync_with_stdio(false), cin.tie(NULL);
LL a, b, c, k;
while(cin >> a >> b >> c >> k && a+b+c+k) {
LL x, y, d;
if(b-a == ) {
cout << "0\n";
continue;
}
LL MOD = 1LL << k;
ex_gcd(c, MOD, x, y, d);
if((b-a) % d != ) {
cout << "FOREVER\n";
continue;
}
x = x *(b-a) / d;
LL B = MOD / d;
x = (x % B + B) % B;
cout << x << "\n";
}
return ;
}
Day7 - F - C Looooops POJ - 2115的更多相关文章
- C Looooops POJ - 2115 (exgcd)
一个编译器之谜:我们被给了一段C++语言风格的循环 for(int i=A;i!=B;i+=C) 内容; 其中所有数都是k位二进制数,即所有数时膜2^k意义下的.我们的目标时球出 内容 被执行了多少次 ...
- C Looooops POJ - 2115 拓展gcd 有一个定理待补()
补算法导论P564 MODULAR-LINEAR-EQUATION-SOLVER算法(P564)
- D - C Looooops POJ - 2115 欧几里德拓展
题意:就是看看for(; ;)多久停止. 最让我蛋疼的是1L和1LL的区别!让我足足wa了12发! 1L 是long类型的, 1LL为long long类型的! 思路: 这就是欧几里德扩展的标准式子了 ...
- B - C Looooops POJ - 2115 (扩展欧几里得)
题目链接:https://cn.vjudge.net/contest/276376#problem/B 题目大意:for( int i= A ; i != B; i+ = c ),然后给你A,B,C ...
- R - C Looooops POJ - 2115 (exgcd)
题目大意:很好理解,一个for循环语句,从a开始到b结束,步长是c,模数是pow(2,k) 问,最少循环多少次,才能到达b,如果永远都到不了b,输出FOREVER 题解:其实就是求一个线性方程,cx= ...
- C Looooops POJ - 2115
数论好题.. 香! 首先我们看到这一题, 题意是 \[a + c * x \equiv b (mod \ \ 2 ^ k) \] 对此式移一下项, 得 \[c * x \equiv b - a (mo ...
- 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 (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(模线性方程)
http://poj.org/problem?id=2115 题意: 给你一个变量,变量初始值a,终止值b,每循环一遍加c,问一共循环几遍终止,结果mod2^k.如果无法终止则输出FOREVER. 思 ...
随机推荐
- ubuntu13.10安装增强功能
步骤: 1>cd /mnt 2> ./VBoxLinuxAdditions.run 3>设置共享文件夹share 4>访问共享文件夹cd /media/sf_share not ...
- P1135奇怪的电梯
P1135奇怪的电梯 #include <iostream> #include <cstdio> #include <cstring> #include <a ...
- 用IDEA创建项目时没有Spring类型的项目模板
使用的版本:2019.2.2 Community 解决方法:File=>Setting=>Plugins=>搜索Spring,安装Spring Assistant=>重启IDE ...
- centso7设置防火墙
CentOS 7默认使用的是firewall作为防火墙,使用iptables必须重新设置一下 1.直接关闭防火墙 1 2 3 systemctl stop firewalld.service #停止f ...
- 建设基于TensorFlow的深度学习环境
一.使用yum安装git 1.查看系统是否已经安装git git --version 2.yum 安装git yum install git 3.安装成功 git --version 4.进入指定目录 ...
- Codeforces Round #586 (Div. 1 + Div. 2)E(拓扑排序,思维)
#include<bits/stdc++.h>using namespace std;int n,m,s; vector<int>edge[200007];queue<i ...
- GsonUtils.getGson().fromJson() 转泛型集合用法
//计算其他收费 List<QiTaFree> qiTaFreeList = GsonUtils.getGson().fromJson(exhiMain.getQiTaFressJson( ...
- WebVR大潮来袭时,前端开发能做些什么
WebVR大潮来袭时,前端开发能做些什么? WebVR即web + VR的体验方式,我们可以戴着头显享受沉浸式的网页,新的API标准让我们可以使用js语言来开发.本文将介绍如何快速开发一个We ...
- bfc作用
作用 1.清浮动 2.不被浮动元素覆盖 3.阻止父子margin传递 触发条件: 1.float不为none 2.position不为static或relative 3.display:inline- ...
- 3 (mysql实战) 事务隔离
提到事务,你肯定不陌生,和数据库打交道的时候,我们总是会用到事务.最经典的例子就是转账,你要给朋友小王转 100 块钱,而此时你的银行卡只有 100 块钱. 转账过程具体到程序里会有一系列的操作,比如 ...