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…
http://poj.org/problem?id=2891 题目大意: k个不同的正整数a1,a2,...,ak.对于一些非负m,满足除以每个ai(1≤i≤k)得到余数ri.求出最小的m. 输入和输出中的所有整数都是非负数,可以用64位整数类型表示. —————————————— 首先我们打眼一看可能是孙子定理. 但是我们无法保证a一定互质. 那么显然就要用我们的可爱的exgcd啦! (下面题解根据这位大佬所懂http://blog.csdn.net/zmh964685331/article/…
欢迎访问~原文出处——博客园-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…
题目大意 求最小整数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…
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…
不互质情况的模板题 注意多组数据不要一发现不合法就退出 #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…
题目大意 就是模板...没啥好说的 思路 因为模数不互质,所以直接中国剩余定理肯定是不对的 然后就考虑怎么合并两个同余方程 \(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)\) 然后就不停合并就可以了…
题意:求解一般模线性同余方程组 解题关键:扩展中国剩余定理求解.两两求解. $\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…
怎样求同余方程组?如: \[\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}\] 就是 \[…
本文为博主原创文章,欢迎转载,请注明出处 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…
放一个写的不错的博客: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: 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…
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: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…
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]…
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…
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…
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…
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…
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 ≤ …
http://poj.org/problem?id=2891 (题目链接) 题意 求解线性同余方程组,不保证模数一定两两互质. Solotion 一般模线性方程组的求解,详情请见:中国剩余定理 细节 注意当最后发现方程无解直接退出时,会导致有数据没有读完,然后就会Re,所以先用数组将所有数据存下来. 代码 // poj2891 #include<algorithm> #include<iostream> #include<cstdlib> #include<cst…
题意: 给出n个模方程x=a(mod r) 求x的最小解 题解: 这就是个线性模方程组的模版题- - 但是有一些要注意的地方 extgcd算出来的解x可能负数  要让x=(x%mo+mo)%mo 而且mo不是等于lcm(r1,r2) 而是r2/gcd(r1,r2) 代码: #include <cstdio> typedef long long ll; ll n,a,r; ll extgcd(ll &x,ll &y,ll a,ll b){ if (!b){ x=,y=; retu…