POJ2115 C Looooops ——模线性方程(扩展gcd)
题目链接:http://poj.org/problem?id=2115
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 27838 | Accepted: 7930 |
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
题解:
A要跳到B的位置,那么它跳了n次后(绕了若干圈),可以跳到B的位置(这时可以理解为A在这一圈内,要从B的位置开始继续跳,当然循环已经结束)。
可知:( A + n*C ) % len = B。其中len = 1<<k;
那么:A + n*C = m*len + B,即: n*C - m*len = B - A。 由于m、n是任意常数,所以将其符号全部转为正。
即得出一个模线性方程: n*C + m*len = B - A。
标准形式为:a*x + b*y = c,其中 a = C, b = len, c = B-A。
然后就是扩展欧几里得的应用。
代码如下:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
#define ms(a,b) memset((a),(b),sizeof((a)))
//#define LOCAL
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int mod = 1e9+;
const int maxn = 1e5+; LL exgcd(LL a, LL b, LL &x, LL &y)
{
if(a== &&b==) return -;
if(b==) {x=; y=; return a;}
LL d = exgcd(b,a%b,y,x);
y -= a/b*x;
return d;
} int main()
{
#ifdef LOCAL
freopen("", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
LL a, b, c, k;
while(scanf("%lld%lld%lld%lld",&a,&b,&c,&k) && (a || b||c ||k))
{
LL len = (1LL<<k);
LL x, y;
LL d = exgcd(c, len, x,y);
LL C = b-a; //C为扩展gcd等式右边的常数项
if(C%d) //如果常数项不能被最大公约数除尽,则无解
{
printf("FOREVER\n");
continue;
} x = (x*(C/d))%len; //C/d为倍数
x = (x%(len/d)+(len/d))%(len/d); //(len/d)相当于x的通解的斜率k(必为正数),(x+k)%k即得到最小正整数解
printf("%lld\n",x);
}
}
POJ2115 C Looooops ——模线性方程(扩展gcd)的更多相关文章
- POJ2115 C Looooops 模线性方程(扩展欧几里得)
题意:很明显,我就不说了 分析:令n=2^k,因为A,B,C<n,所以取模以后不会变化,所以就是求(A+x*C)%n=B 转化一下就是求 C*x=B-A(%n),最小的x 令a=C,b=B-A ...
- poj_2115C Looooops(模线性方程)
题目链接:http://poj.org/problem?id=2115 C Looooops Time Limit: 1000MS Memory Limit: 65536K Total Submi ...
- POJ2115——C Looooops(扩展欧几里德+求解模线性方程)
C Looooops DescriptionA Compiler Mystery: We are given a C-language style for loop of type for (vari ...
- C Looooops(扩展欧几里得求模线性方程)
http://poj.org/problem?id=2115 题意:对于C的循环(for i = A; i != B; i+=C)问在k位存储系统内循环多少次结束: 若循环有限次能结束输出次数,否则输 ...
- POJ - 2115 C Looooops(扩展欧几里德求解模线性方程(线性同余方程))
d.对于这个循环, for (variable = A; variable != B; variable += C) statement; 给出A,B,C,求在k位存储系统下的循环次数. 例如k=4时 ...
- C Looooops(扩展欧几里得+模线性方程)
http://poj.org/problem?id=2115 题意:给出A,B,C和k(k表示变量是在k位机下的无符号整数),判断循环次数,不能终止输出"FOREVER". 即转化 ...
- POJ 1061 青蛙的约会(扩展GCD求模线性方程)
题目地址:POJ 1061 扩展GCD好难懂.. 看了半天.最终把证明什么的都看明确了. .推荐一篇博客吧(戳这里),讲的真心不错.. 直接上代码: #include <iostream> ...
- POJ 2115 C Looooops(模线性方程)
http://poj.org/problem?id=2115 题意: 给你一个变量,变量初始值a,终止值b,每循环一遍加c,问一共循环几遍终止,结果mod2^k.如果无法终止则输出FOREVER. 思 ...
- [ACM_其他] Modular Inverse [a关于模m的逆 模线性方程]
Description The modular modular multiplicative inverse of an integer a modulo m is an integer x such ...
随机推荐
- 老哥你真的知道ArrayList#sublist的正确用法么
我们有这么一个场景,给你一个列表,可以动态的新增,但是最终要求列表升序,要求长度小于20,可以怎么做? 这个还不简单,几行代码就可以了 public List<Integer> trimL ...
- 更新tensorflow支持GPU时出错
sudo pip install --upgrade tensorflow-gpu Operation not permitted: '/tmp/pip-Sx_vMg-uninstall/System ...
- pycharm的todo和fixme标记,标志为今后再做和bug点
使用方法,及查看方法: https://blog.csdn.net/xiemanR/article/details/73368440
- python 常见细节知识点
1. a[::-1]翻转 设有一个元组或者列表 a = (1,2,3,4) b = [1,2,3,4] 则a[::-1]和b[::-1]的含义是将元组或列表的内容翻转 a[::-1] # 结果为(4, ...
- cocos2d-x调用android内嵌浏览器打开网页
cocos2d-x调用android内嵌浏览器打开网页,能够从入口传入网址,C++调用android 的api就可以实现. 方法也非常easy 1. 改动"cocos2dx\platform ...
- 【转载】聊一聊C#的Equals()和GetHashCode()方法
首先先谈一下Equals()这个方法: Equals()方法,来自于Object,是我们经常需要重写的方法.此方法的默认实现大概是这样的: public virtual bool Equals(obj ...
- C#设计模式总结 C#设计模式(22)——访问者模式(Vistor Pattern) C#设计模式总结 .NET Core launch.json 简介 利用Bootstrap Paginator插件和knockout.js完成分页功能 图片在线裁剪和图片上传总结 循序渐进学.Net Core Web Api开发系列【2】:利用Swagger调试WebApi
C#设计模式总结 一. 设计原则 使用设计模式的根本原因是适应变化,提高代码复用率,使软件更具有可维护性和可扩展性.并且,在进行设计的时候,也需要遵循以下几个原则:单一职责原则.开放封闭原则.里氏代替 ...
- 工作总结 mvc 调页面传参数 参数值会一直保存 在这个页面上的
意思是 两个页面均可以 获取到id 和 goodsType 都可以获取 id goodsType post 的 还多带点属性值 form data 页面上带过去的 (新增 编辑)
- php错误封装类
1.创建MyErrorHandler.php文件 代码如下: <?php class MyErrorHandler { public $message; public $filename; pu ...
- Android摄像头採集的视频数据流怎样通过Socket实时发送到目标服务端
分两块: 1.取得摄像头採集的视频流 2.发送到server端 protected MediaRecorder mMediaRecorder; private LocalServerSocket mL ...