poj2115
构造出模线性方程c * x = b - a mod (2 ^ k)
很容易解。
利用LRJ书上的方法。
#include <iostream> using namespace std; #define LL long long int LL ext_gcd(LL a, LL b, LL& x, LL& y)
{
LL t, ret;
if (!b){
x = 1, y = 0;
return a;
}
ret = ext_gcd(b, a%b, x, y);
t = x, x = y, y = t - a / b*y;
return ret;
}
//ax = b (mod n)
void gcd(LL a, LL b, LL &d, LL &x, LL &y)
{
if (!b)
{
d = a, x = 1, y = 0;
}
else
{
gcd(b, a %b, d, y, x);
y -= x * (a / b);
}
}
LL modular_linear_equation(LL a, LL b, LL n)
{
long long x, y, e, d;
gcd(a, n, d, x, y);
if (b % d) return -1;
e = b / d * x % n + n;
return e % (n / d);
}
int main()
{
////c * x = b - a mod (2 ^ k)
int a, b, c, k;
while (cin >> a >> b >> c >> k && (a || b || c || k))
{
LL num = modular_linear_equation(c, b - a, 1LL << k);
if (num == -1)
{
cout << "FOREVER" << endl;
continue;
}
cout << num << endl;
}
}
poj2115的更多相关文章
- POJ2115——C Looooops(扩展欧几里德+求解模线性方程)
C Looooops DescriptionA Compiler Mystery: We are given a C-language style for loop of type for (vari ...
- poj2115 C Looooops(exgcd)
poj2115 C Looooops 题意: 对于C的for(i=A ; i!=B ;i +=C)循环语句,问在k位存储系统中循环几次才会结束. 若在有限次内结束,则输出循环次数. 否则输出死循环. ...
- poj2115(扩展欧基里德定理)
题目链接:https://vjudge.net/problem/POJ-2115 题意:模拟for循环for(int i=A;i!=B;i+=C),且数据范围为k位无符号数以内,即0~1<< ...
- POJ2115 C Looooops 扩展欧几里德
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - POJ2115 题意 对于C的for(i=A ; i!=B ;i +=C)循环语句,问在k位存储系统中循环几次 ...
- POJ2115 C Looooops[扩展欧几里得]
C Looooops Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 24355 Accepted: 6788 Descr ...
- POJ2115 C Looooops(线性同余方程)
无符号k位数溢出就相当于mod 2k,然后设循环x次A等于B,就可以列出方程: $$ Cx+A \equiv B \pmod {2^k} $$ $$ Cx \equiv B-A \pmod {2^k} ...
- 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 ...
- POJ2115 - C Looooops(扩展欧几里得)
题目大意 求同余方程Cx≡B-A(2^k)的最小正整数解 题解 可以转化为Cx-(2^k)y=B-A,然后用扩展欧几里得解出即可... 代码: #include <iostream> us ...
- POJ2115 C Looooops(数论)
题目链接. 分析: 数论了解的还不算太多,解的时候,碰到了不小的麻烦. 设答案为x,n = (1<<k), 则 (A+C*x) % n == B 即 (A+C*x) ≡ B (mod n) ...
- POJ2115(扩展欧几里得)
C Looooops Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 23700 Accepted: 6550 Descr ...
随机推荐
- MacOS 下提示APP 损坏 无法安装 解决方法
sudo spctl --master-disable
- su: cannot set user id: Resource temporarily unavailable【转】
今天R&D所在主机出现su: cannot set user id: Resource temporarily unavailable资源不可用报错,直接通过其他机器ssh huyuh@xxx ...
- sh-copy-id命令报错:-bash: ssh-copy-id: command not found
参考网址:http://www.bubuko.com/infodetail-1662159.html yum -y install openssh-clients
- Centos: -bash: unzip: command not found
安装命令:yum install -y unzip zip 同时安装unzip和zip
- python抓取内涵段子文章
# coding:utf-8 from urllib.request import urlretrieve import threading import requests from bs4 impo ...
- stylus项目知识点
1.在项目中,引入.sty文件的时候,用来下面方式 @import "~common/stylus/variable.styl" ~ 是stylus的写法,参考https://gi ...
- Java编程的逻辑 (15) - 初识继承和多态
本系列文章经补充和完善,已修订整理成书<Java编程的逻辑>,由机械工业出版社华章分社出版,于2018年1月上市热销,读者好评如潮!各大网店和书店有售,欢迎购买,京东自营链接:http:/ ...
- JS高级程序设计 表单部分
目录: 表单:1.引用表单 2.提交表单 3.重置表单 4.表单字段 在HTML中,表单是由<form>元素来表示的,而 ...
- vue1.0到2.0
vue1.0到2.0 vue2.0 新手教程(一) 想想自己写vue的项目也写了一年了,从vue1.0到2.0,走过不少路,填过不少坑, 下面记录一下新手从0到1的过程,本文“应该”会持续更新 首 ...
- P1006 传纸条 多维DP
题目描述 小渊和小轩是好朋友也是同班同学,他们在一起总有谈不完的话题.一次素质拓展活动中,班上同学安排做成一个mm行nn列的矩阵,而小渊和小轩被安排在矩阵对角线的两端,因此,他们就无法直接交谈了.幸运 ...