POJ 2891 Strange Way to Express Integers 中国剩余定理MOD不互质数字方法
http://poj.org/problem?id=2891
7
11323 9793
5537 4754
21538 10901
16118 203
2082 1209
22929 9510
16541 15898
4
18373 16147
8614 14948
8440 17480
22751 21618
6
19576 8109
18992 24177
9667 17726
16743 19533
16358 12524
8280 22731
4
9397 38490
22001 25094
33771 38852
19405 35943
ans
32439
13052907343337200
-1
78383942913636233
题目mod的数字并不互质,那怎么办呢?
记mod[i]表示第i个mod的数字,r[i]表示余数,那么观察前两项
总有X = k1 * mod[1] + r[1];
X = k2 * mod[2] + r[2];
那么k1 * mod[1] - k2 * mod[2] = r[2] - r[1]
这是可以用扩展欧几里德算法求得最小的解k1 (k1可以等于0,不一定要>0,我就是规定满足k1>0后不断溢出,所以wa)
那么得到满足前两个数字的解是(就是满足%mod[1] = r[1] , %mod[2] = r[2])的解。ansx = k1 * mod[1] + r[1]
然后往下合并
怎么往下合并呢?
就是把前面两条等式,变成了 % lcm(mod[1], mod[2]) = rr了,这个rr可以自己解出来,ansx % lcm(mod[1], mod[2])就是了
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string> const int maxn = + ;
LL mod[maxn];
LL r[maxn];
LL gcd(LL n, LL m) {
if (n % m == ) return m;
else return gcd(m, n % m);
}
LL lcm(LL n, LL m) {
return n / gcd(n, m) * m;
}
LL exgcd (LL a,LL mod,LL &x,LL &y) {
//求解a关于mod的逆元 ★:当且仅当a和mod互质才有用
if (mod==) {
x=;
y=;
return a;//保留基本功能,返回最大公约数
}
LL g=exgcd(mod,a%mod,x,y);
LL t=x; //这里根据mod==0 return回来后,
x=y; //x,y是最新的值x2,y2,改变一下,这样赋值就是为了x1=y2
y=t-(a/mod)*y; // y1=x2(变成了t)-[a/mod]y2;
return g; //保留基本功能,返回最大公约数
}
bool get_min_number (LL a,LL b,LL c,LL &x,LL &y) { //得到a*x+b*y=c的最小整数解
LL abGCD = gcd(a,b);
if (c % abGCD != ) return ;//不能整除GCD的话,此方程无解
a /= abGCD;
b /= abGCD;
c /= abGCD;
LL tx,ty;
exgcd(a,b,tx,ty); //先得到a*x+b*y=1的解,注意这个时候gcd(a,b)=1
x = tx * c;
y = ty * c; //同时乘上c,c是约简了的。得到了一组a*x + b*y = c的解。
LL haveSignB = b;
if (b < ) b = -b; //避免mod负数啊
x = (x % b + b) % b; //最小解
// if (x == 0) x = b; //避免x = 0不是"正"整数 不要用这个,溢出
y = (c - a * x) / haveSignB;
return ;//1代表可以
}
int n;
void work () {
for (int i = ; i <= n; ++i) {
scanf("%lld%lld", &mod[i], &r[i]);
}
LL mm = mod[];
LL rr = r[];
LL ansx = ;
for (int i = ; i <= n; ++i) {
LL x, y;
int ret = get_min_number(mm, -mod[i], r[i] - rr, x, y);
if (ret == ) {
printf("-1\n");
return;
}
ansx = x * mm + rr;
mm = lcm(mm, mod[i]);
rr = ansx % mm;
}
printf("%lld\n", rr);
return;
} int main() {
#ifdef local
freopen("data.txt","r",stdin);
#endif
while(scanf("%d", &n) != EOF && n) {
work();
}
return ;
}
为什么rr就是最小解呢?余数当然是最小的解啊。。。
POJ 2891 Strange Way to Express Integers 中国剩余定理MOD不互质数字方法的更多相关文章
- poj Strange Way to Express Integers 中国剩余定理
Strange Way to Express Integers Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 8193 ...
- poj 2891 Strange Way to Express Integers (非互质的中国剩余定理)
Strange Way to Express Integers Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 9472 ...
- poj——2891 Strange Way to Express Integers
Strange Way to Express Integers Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 16839 ...
- [POJ 2891] Strange Way to Express Integers
Strange Way to Express Integers Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 10907 ...
- poj 2891 Strange Way to Express Integers (扩展gcd)
题目链接 题意:给k对数,每对ai, ri.求一个最小的m值,令m%ai = ri; 分析:由于ai并不是两两互质的, 所以不能用中国剩余定理. 只能两个两个的求. a1*x+r1=m=a2*y+r2 ...
- POJ.2891.Strange Way to Express Integers(扩展CRT)
题目链接 扩展中国剩余定理:1(直观的).2(详细证明). [Upd:]https://www.luogu.org/problemnew/solution/P4774 #include <cst ...
- POJ 2891 Strange Way to Express Integers 中国剩余定理解法
一种不断迭代,求新的求余方程的方法运用中国剩余定理. 总的来说,假设对方程操作.和这个定理的数学思想运用的不多的话.是非常困难的. 參照了这个博客的程序写的: http://scturtle.is-p ...
- poj 2891 模数不互质的中国剩余定理
Strange Way to Express Integers Description Elina is reading a book written by Rujia Liu, which intr ...
- POJ 2891 Strange Way to Express Integers(拓展欧几里得)
Description Elina is reading a book written by Rujia Liu, which introduces a strange way to express ...
随机推荐
- 洛谷【P1601】A+B Problem(高精)
题目传送门:https://www.luogu.org/problemnew/show/P1601 高精度加法板子.我们灵性地回忆一波小学学加法列竖式的场景(从\(6\)岁开始口算从未打过草稿的大佬请 ...
- POJ2420:A Star not A Tree?
我对模拟退火的理解:https://www.cnblogs.com/AKMer/p/9580982.html 我对爬山的理解:https://www.cnblogs.com/AKMer/p/95552 ...
- 通过gitweb管理Puppet配置(nginx版本+lighttpd版)
Puppet路径为:/etc/puppet 软件版本:gitweb-1.7.1-3.el6_4.1.noarch git-1.7.1-3.el6_4.1.x86_64 fcgi-2.4.0-12.el ...
- Linux负载均衡软件之LVS
一. LVS简介 LVS是Linux Virtual Server的简称,也就是Linux虚拟服务器, 是一个由章文嵩博士发起的自由软件项目,它的官方站点是linuxvirtualserver.org ...
- web攻击之三:SQL注入攻击的种类和防范手段
观察近来的一些安全事件及其后果,安全专家们已经得到一个结论,这些威胁主要是通过SQL注入造成的.虽然前面有许多文章讨论了SQL注入,但今天所讨论的内容也许可帮助你检查自己的服务器,并采取相应防范措施. ...
- 最近一直是web前段,没什么意思,所以就不发资料了
最近一直是web前段,没什么意思,所以就不发资料了 版权声明:本文为博主原创文章,未经博主允许不得转载.
- TCP/IP以及Socket对象基本
1 OSI七层模型概念介绍 物理层:数据以比特的方式进行传递,典型的设备是集线器.该层主要规定了设备的电压或者端口等等一些列物理层面上的规定 数据链路层:该层数据以帧的方式进行传递,主要是两个 ...
- 一款Regular expression在线检测工具
记录下我自己使用的一款正则表达式使用工具 https://regex101.com/ 输入正则表达式后,可以在下面的“TEST STRING”中来测试对应的字符串是否满足该正则表达式 个人觉得非常好用
- redis学习总结2
redis配置文件说明:以下这篇文章已经说明的很明白了.感谢~ http://blog.sina.com.cn/s/blog_636415010101970j.html
- 在Android中使用FlatBuffers(上篇)
本文来自网易云社区. 总览 先来看一下 FlatBuffers 项目已经为我们提供了什么,而我们在将 FlatBuffers 用到我们的项目中时又需要做什么的整体流程.如下图: 在使用 FlatBuf ...