本文为博主原创文章,欢迎转载,请注明出处 www.cnblogs.com/yangyaojia 题目大意 求解一组同余方程 x ≡ r1 (mod a1) x ≡ r2 (mod a2) x ≡ r3 (mod a3) ...... x ≡ rk (mod ak) 的解x(a1,a2,a3,.....ak 并不一定互质).如果不存在则输出-1. 输入格式 有多组数据,每组数组第一行为k,后面有k行,每行两个数,代表ai,ri. 输出格式 每一行对应每一个询问的解x. 样例输入 2 8 7 11…
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - POJ2891 题意概括 给出k个同余方程组:x mod ai = ri.求x的最小正值.如果不存在这样的x,那么输出-1.不满足所有的ai互质. 题解 UPD(2018-08-07): 本题做法为扩展中国剩余定理. 我写了一篇证明:链接:https://www.cnblogs.com/zhouzhendong/p/exCRT.html 代码就不要看了,很久之前写的,太丑了. 代码 #include <cs…
题意:求解一般模线性同余方程组 解题关键:扩展中国剩余定理求解.两两求解. $\left\{ {\begin{array}{*{20}{l}}{x = {r_1}\,\bmod \,{m_1}}\\{x = {r_2}\,\bmod \,{m_2}}\end{array}} \right.$ 为了代码的符号清晰,将转化后的系数都为正,故如下设方程. $\left\{ {\begin{array}{*{20}{l}}{x = {r_1} - {k_1}{m_1}}\\{x = {r_2} + {k…
0.引子 每一个讲中国剩余定理的人,都会从孙子的一道例题讲起 有物不知其数,三三数之剩二,五五数之剩三,七七数之剩二.问物几何? 1.中国剩余定理 引子里的例题实际上是求一个最小的x满足 关键是,其中r1,r2,--,rk互质 这种问题都有多解,每一个解都为最小的解加上若干个lcm(r1,r2,...,rk),这个不用我证了吧(-_-||) 解决这个问题的方法是构造法, 先构造k个数 满足, 这样就保证 ,但是由于 bi 乘了除 ri 以外所有 r,所以bi模其它的 r 都为 0, 再把所有 b…
Strange Way to Express Integers DescriptionElina is reading a book written by Rujia Liu, which introduces a strange way to express non-negative integers. The way is described as following:Choose k different positive integers a1, a2, …, ak. For some n…
题意 Language:Default Strange Way to Express Integers Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 21651 Accepted: 7266 Description Elina is reading a book written by Rujia Liu, which introduces a strange way to express non-negative inte…
P4777 [模板]扩展中国剩余定理(EXCRT) excrt模板 我们知道,crt无法处理模数不两两互质的情况 然鹅excrt可以 设当前解到第 i 个方程 设$M=\prod_{j=1}^{i-1}b[j]$ ,$ res$是前$ i-1 $个方程的最小解 则$ res+x*M$ 是前 $i-1 $个方程的通解 那么我们求的就是 $res+x*M ≡ a[i] (mod b[i])$ $<=> x*M - y*b[i] = a[i]-res$ 用exgcd求出的解为 t (当且仅当 gcd…
题目大意 就是模板...没啥好说的 思路 因为模数不互质,所以直接中国剩余定理肯定是不对的 然后就考虑怎么合并两个同余方程 \(ans = a_1 + x_1 * m_1 = a_2 + x_2 * m_2\) \(x_1 * m_1 + x_2 * m_2 = a _ 2 - a _ 1\)(因为正负号没影响嘛) 然后就可以exgcd解出来\(x_1, x_2\), 最后就可以得到\(x' = a_1 + x_1 * m_1, m' = lcm(m_1, m_2)\) 然后就不停合并就可以了…
题目链接 扩展中国剩余定理:1(直观的).2(详细证明). [Upd:]https://www.luogu.org/problemnew/solution/P4774 #include <cstdio> #include <cctype> #define gc() getchar() typedef long long LL; const int N=1e6+5; LL n,m[N],r[N]; inline LL read() { LL now=0,f=1;register ch…
题目链接 扩展CRT模板题,原理及证明见传送门(引用) #include<cstdio> #include<algorithm> using namespace std; typedef long long ll; ; ll n,m[N],c[N]; void exgcd(ll a,ll b,ll& x,ll& y,ll& g) { ,y=,g=a; else exgcd(b,a%b,y,x,g),y-=x*(a/b); } bool CRT(ll&…
题目链接 题意:给k对数,每对ai, ri.求一个最小的m值,令m%ai = ri; 分析:由于ai并不是两两互质的, 所以不能用中国剩余定理. 只能两个两个的求. a1*x+r1=m=a2*y+r2联立得:a1*x-a2*y=r2-r1;设r=r2-r2; 互质的模线性方程组m=r[i](mod a[i]).两个方程可以合并为一个,新的a1为lcm(a1,a2), 新的r为关于当前两个方程的解m,然后再和下一个方程合并…….(r2-r1)不能被gcd(a1,a2)整除时无解.   怎么推出的看…
题目大意 求最小整数x,满足x≡a[i](mod m[i])(没有保证所有m[i]两两互质) 题解 中国剩余定理显然不行....只能用方程组两两合并的方法求出最终的解,刘汝佳黑书P230有讲~~具体证明和实现我是参考此大神的 代码: #include<iostream> using namespace std; #define MAXN 100000 typedef long long LL; LL m[MAXN],a[MAXN]; void extended_gcd(LL a,LL b,LL…
不互质情况的模板题 注意多组数据不要一发现不合法就退出 #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> using namespace std; typedef long long ll; inline ll read(){ ,f=; ;c=getchar();} +c-';c=getchar();} re…
怎样求同余方程组?如: \[\begin{cases} x \equiv a_1 \pmod {m_1} \\ x \equiv a_2 \pmod {m_2} \\ \cdots \\ x \equiv a_n \pmod {m_n} \end{cases}\] 不保证 \(m\) 两两互素? 两两合并! 比方说 \[\begin{cases} x \equiv a_1 \pmod {m_1} \\ x \equiv a_2 \pmod {m_2} \\ \end{cases}\] 就是 \[…
放一个写的不错的博客:https://www.cnblogs.com/zwfymqz/p/8425731.html POJ好像不能用__int128. #include <iostream> #include <stdio.h> typedef long long ll; const int maxn=1e6+10; ll m[maxn],r[maxn]; void exgcd(ll a,ll b,ll &x,ll &y) { if (b==0) { x=1; y=…
[POJ2891]Strange Way to Express Integers(拓展CRT) 题面 Vjudge 板子题. 题解 拓展\(CRT\)模板题. #include<iostream> #include<cstdio> using namespace std; #define ll long long #define MAX 111111 ll exgcd(ll a,ll b,ll &x,ll &y) { if(!b){x=1,y=0;return a;…
Strange Way to Express Integers Time Limit:1000MS Memory Limit:131072KB 64bit IO Format:%I64d & %I64u Submit Status Practice POJ 2891   Description Elina is reading a book written by Rujia Liu, which introduces a strange way to express non-negative i…
http://poj.org/problem?id=2891 Strange Way to Express Integers Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 11970   Accepted: 3788 Description Elina is reading a book written by Rujia Liu, which introduces a strange way to express no…
F - Strange Way to Express Integers Time Limit:1000MS     Memory Limit:131072KB     64bit IO Format:%I64d & %I64u Submit Status Description Elina is reading a book written by Rujia Liu, which introduces a strange way to express non-negative integers.…
Strange Way to Express Integers Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 9472   Accepted: 2873 Description Elina is reading a book written by Rujia Liu, which introduces a strange way to express non-negative integers. The way is…
Strange Way to Express Integers Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 10907   Accepted: 3336 Description Elina is reading a book written by Rujia Liu, which introduces a strange way to express non-negative integers. The way is…
1635:[例 5]Strange Way to Express Integers sol:貌似就是曹冲养猪的加强版,初看感觉非常没有思路,经过一番艰辛的***,得到以下的结果 随便解释下给以后的自己听:K是要求的数字 第一个读入的A1,Mod1不用改,从2开始做,把Mod2改成LCM,A2改成Ans,接着搞3 /* 原式: X = A[1] (%Mod[1]) X = A[2] (%Mod[2]) ... X = A[n] (%Mod[n]) K[1]*Mod[1]+A[1] = X K[2]…
Strange Way to Express Integers Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 8193   Accepted: 2448 Description Elina is reading a book written by Rujia Liu, which introduces a strange way to express non-negative integers. The way is…
Strange Way to Express Integers Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 16839   Accepted: 5625 Description Elina is reading a book written by Rujia Liu, which introduces a strange way to express non-negative integers. The way is…
I. Strange Way to Express Integers 题目描述 原题来自:POJ 2891 给定 2n2n2n 个正整数 a1,a2,⋯,ana_1,a_2,\cdots ,a_na​1​​,a​2​​,⋯,a​n​​ 和 m1,m2,⋯,mnm_1,m_2,\cdots ,m_nm​1​​,m​2​​,⋯,m​n​​,求一个最小的正整数 xxx,满足 ∀i∈[1,n],x≡ai (modmi ),或者给出无解. 输入格式 多组数据. 每组数据第一行一个整数 nnn:接下来 nn…
Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 8476   Accepted: 2554 Description Elina is reading a book written by Rujia Liu, which introduces a strange way to express non-negative integers. The way is described as following: Choose k…
Description Elina is reading a book written by Rujia Liu, which introduces a strange way to express non-negative integers. The way is described as following: Choose k different positive integers a1, a2, …, ak. For some non-negative m, divide it by ev…
Description Elina is reading a book written by Rujia Liu, which introduces a strange way to express non-negative integers. The way is described as following: Choose k different positive integers a1, a2, -, ak. For some non-negative m, divide it by ev…
http://poj.org/problem?id=2891 Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 16849   Accepted: 5630 Description Elina is reading a book written by Rujia Liu, which introduces a strange way to express non-negative integers. The way is…
Elina is reading a book written by Rujia Liu, which introduces a strange way to express non-negative integers. The way is described as following: Choose k different positive integers a1, a2, …, ak. For some non-negative m, divide it by every ai (1 ≤ …