0.引子

每一个讲中国剩余定理的人,都会从孙子的一道例题讲起

有物不知其数,三三数之剩二,五五数之剩三,七七数之剩二。问物几何?

1.中国剩余定理

引子里的例题实际上是求一个最小的x满足

关键是,其中r1,r2,……,rk互质

这种问题都有多解,每一个解都为最小的解加上若干个lcm(r1,r2,...,rk),这个不用我证了吧(-_-||)

解决这个问题的方法是构造法,

先构造k个数

满足

这样就保证 ,但是由于 bi 乘了除 ri 以外所有 r,所以bi模其它的 r 都为 0,

再把所有 bi 加起来,得到的数就满足方程了。

例题

UVA756 Biorhythms

HDU1370 Biorhythms

非常裸的一道剩余定理的题,但是某些OJ题面出了问题,以至于让同学们白白wa了很多遍

首先是luogu的翻译,并不是“保证 x 不超过 21252”,而是“保证 x-d 不超过 21252”

然后是(屑)HDU的数据,一开始得输入一个数后才能开始输入,看样例应该就知道了

题目把r1~r3都给出来了,相当于可以直接手算得出

直接代入算b

最后判断x是否<=d,是就+=21252

CODE

#include<cstdio>
#include<cstring>
#include<iostream>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<algorithm>
#define ENDL putchar('\n')
#define LL long long
#define DB double
#define lowbit(x) ((-x)&(x))
//#define int LL
using namespace std;
inline LL read() {
LL f = 1,x = 0;char s = getchar();
while(s < '0' || s > '9') {if(s == '-')f = -1;s = getchar();}
while(s >= '0' && s <= '9') {x = x * 10 + (s - '0');s = getchar();}
return x * f;
}
int main() {
// read(); //HDU的把这句加上
int p,e,i,d,lc = 21252,ad1 = 5544,ad2 = 14421,ad3 = 1288;
int Case = 0;
while(scanf("%d%d%d%d",&p,&e,&i,&d) == 4) {
if(p == -1 || e == -1 || i == -1 || d == -1) break;
int x = (p *1ll* ad1 + e *1ll* ad2 + i *1ll* ad3) % lc;
if(x <= d) x += lc;
printf("Case %d: the next triple peak occurs in %d days.\n",++ Case,x - d);
}
return 0;
}

2.扩展中国剩余定理

这个就比普通中国剩余定理好用多了

还是求这个方程

但是不保证互质了

既然不保证互质,他们就有最大公约数

我们依次合并两个方程

把它变一下,设k、p,满足

即 

移个项:

于是它就变成了“ax+by=c”的形式,可以用扩展欧几里得求出特解k(若无解就整个方程无解了)

它的任意解都满足 

由于x等于通解中的一个

所以

成功合并成一个方程!

最后剩下一个方程时,最小的解就为式子右边的值

例题

POJ2891 Strange Way to Express Integers

这题是扩展中国剩余定理的板题,不用我讲了吧(众所周知,板题≠水题)

CODE

#include<cstdio>
#include<cstring>
#include<iostream>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<algorithm>
#define MAXN 2000005
#define MAXM 3000005
#define ENDL putchar('\n')
#define LL long long
#define DB double
#define lowbit(x) ((-x)&(x))
#define int LL
using namespace std;
inline LL read() {
LL f = 1,x = 0;char s = getchar();
while(s < '0' || s > '9') {if(s == '-')f = -1;s = getchar();}
while(s >= '0' && s <= '9') {x = x * 10 + (s - '0');s = getchar();}
return x * f;
}
const int jzm = 1000000007;
int n,m,i,j,s,o,k;
LL exgcd(LL a,LL b,LL &x,LL &y) {
if(b == 0) {
x = 1;y = 0;
return a;
}
LL r = exgcd(b,a%b,y,x);
y -= x*(a/b);
return r;
}
signed main() {
while(scanf("%lld",&n) == 1) {
LL r1 = read(),a1 = read();
bool flag = 1;
for(int i = 2;i <= n;i ++) {
LL r2 = read(),a2 = read(),k,p;
if(a2 > a1) swap(a1,a2),swap(r1,r2);
if(!flag) continue;
LL gc = exgcd(r1,r2,k,p),lc = r1 / gc * r2;
if((a1-a2) % gc) {
flag = 0;continue;
}
LL tym = r2/gc;
((k = (k * (a1-a2) / gc) % tym) += tym) %= tym;
a1 = (a1 + lc - k * r1 % lc) % lc;
r1 = lc;
}
if(!flag) printf("-1\n");
else printf("%lld\n",a1 == 0 ? r1:a1);
}
return 0;
}

中国剩余定理+扩展中国剩余定理 讲解+例题(HDU1370 Biorhythms + POJ2891 Strange Way to Express Integers)的更多相关文章

  1. P4777 【模板】扩展中国剩余定理(EXCRT)/ poj2891 Strange Way to Express Integers

    P4777 [模板]扩展中国剩余定理(EXCRT) excrt模板 我们知道,crt无法处理模数不两两互质的情况 然鹅excrt可以 设当前解到第 i 个方程 设$M=\prod_{j=1}^{i-1 ...

  2. [poj2891]Strange Way to Express Integers(扩展中国剩余定理)

    题意:求解一般模线性同余方程组 解题关键:扩展中国剩余定理求解.两两求解. $\left\{ {\begin{array}{*{20}{l}}{x = {r_1}\,\bmod \,{m_1}}\\{ ...

  3. POJ2891 Strange Way to Express Integers【扩展中国剩余定理】

    题目大意 就是模板...没啥好说的 思路 因为模数不互质,所以直接中国剩余定理肯定是不对的 然后就考虑怎么合并两个同余方程 \(ans = a_1 + x_1 * m_1 = a_2 + x_2 * ...

  4. POJ2891 Strange Way to Express Integers [中国剩余定理]

    不互质情况的模板题 注意多组数据不要一发现不合法就退出 #include <iostream> #include <cstdio> #include <cstring&g ...

  5. POJ-2891 Strange Way to Express Integers(拓展中国剩余定理)

    放一个写的不错的博客:https://www.cnblogs.com/zwfymqz/p/8425731.html POJ好像不能用__int128. #include <iostream> ...

  6. POJ2891 Strange Way to Express Integers (扩展欧几里德)

    本文为博主原创文章,欢迎转载,请注明出处 www.cnblogs.com/yangyaojia 题目大意 求解一组同余方程 x ≡ r1 (mod a1) x ≡ r2 (mod a2) x ≡ r ...

  7. POJ2891 Strange Way to Express Integers 扩展欧几里德 中国剩余定理

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - POJ2891 题意概括 给出k个同余方程组:x mod ai = ri.求x的最小正值.如果不存在这样的x, ...

  8. 数论F - Strange Way to Express Integers(不互素的的中国剩余定理)

    F - Strange Way to Express Integers Time Limit:1000MS     Memory Limit:131072KB     64bit IO Format: ...

  9. poj 2981 Strange Way to Express Integers (中国剩余定理不互质)

    http://poj.org/problem?id=2891 Strange Way to Express Integers Time Limit: 1000MS   Memory Limit: 13 ...

随机推荐

  1. 【二分图】匈牙利 & KM

    [二分图]匈牙利 & KM 二分图 概念: 一个图 \(G=(V,E)\) 是无向图,如果顶点 \(V\) 可以分成两个互不相交地子集 \(X,Y\) 且任意一条边的两个顶点一个在 \(X\) ...

  2. MongoDB学习总览

    第1部分: MongoDB入门(第1~6章) 该部分介绍MongoDB的基本概念及入门知识. 通过该部分的学习,读者可对MongoDB自身的技术全貌形成一定的认识. 第2部分: MongoDB微服务开 ...

  3. 前缀和与差分(Acwing795-798)

    一维前缀和 Acwing795.前缀和 #include <iostream> using namespace std; const int N = 100010; int n, m; i ...

  4. re学习笔记

    re学习笔记 学习链接: https://regexlearn.com/zh-cn/learn \w: 数字字母下划线 \W: 非\w \d \D: !\d \s: space cha \S: !\s ...

  5. vue大型电商项目尚品汇(后台篇)day05

    今天继续是对后台管理部分的一个操作,但是快要结束了,今天结束,明天会进入一个从Vue以来,另外一个名声显著的东西了,一只耳闻从未见识,而且十分的炫酷 他就是------数据可视化Echarts,迫不及 ...

  6. 记录一下MySql update会锁定哪些范围的数据

    目录 1.背景 2.前置知识 2.1 数据库的隔离级别 2.2 数据库版本 2.3 数据库的存储引擎 2.4 锁是加在记录上还是索引上 2.5 update...where加锁的基本单位是 2.6 行 ...

  7. 安装ImageMagick7.1库以及php的Imagick扩展

    由于ImageMagick7以下不支持heic等图片格式,所以重新安装了ImageMagick7.1版本支持heic格式,并写此文章记录一下. 如果安装过程中遇到一些未知的错误,https://ima ...

  8. python爬虫之protobuf协议介绍

    前言 在你学习爬虫的知识过程中是否遇到下面的类型.如果有兴趣学习一下或者了解相关知识的,且不嫌在下才疏学浅,可以参考一下.欢迎各位网友的指正. 首先叙述一下问题的会出现的式样. 你可能会在请求参数中看 ...

  9. zabbix实时监控mysql业务数据

    1. 安装zabbix agent 下载zabbix:过往的软件包都有:https://sourceforge.mirrorservice.org/z/za/zabbix/ZABBIX%20Lates ...

  10. Educational Codeforces Round 129 (Rated for Div. 2) A-D

    Educational Codeforces Round 129 (Rated for Div. 2) A-D A 题目 https://codeforces.com/contest/1681/pro ...