POJ 2891】的更多相关文章

http://poj.org/problem?id=2891 题意:求解一个数x使得 x%8 = 7,x%11 = 9; 若x存在,输出最小整数解.否则输出-1: ps: 思路:这不是简单的中国剩余定理问题,由于输入的ai不一定两两互质,而中国剩余定理的条件是除数两两互质. 这是一般的模线性方程组,对于 X mod m1=r1 X mod m2=r2 ... ... ... X mod mn=rn 首先,我们看两个式子的情况 X mod m1=r1-----------------------(…
http://poj.org/problem?id=2891 题意就是孙子算经里那个定理的基础描述不过换了数字和约束条件的个数…… https://blog.csdn.net/HownoneHe/article/details/52186204 这个博客提供了互质情况下的代码以及由此递推出的(另一个版本的)非互质情况下的代码. 假如给出m[n],a[n]分别代表要求的除数和余数: 互质情况下: ( 做n次 ) 对不包含m[i]的所有m求积 ( 互质的数的最小公倍数 ) , exgcd求出来逆元后…
http://poj.org/problem?id=2891 711323 97935537 475421538 1090116118 2032082 120922929 951016541 15898 418373 161478614 149488440 1748022751 21618619576 810918992 241779667 1772616743 1953316358 125248280 2273149397 3849022001 2509433771 3885219405 35…
[题目链接] http://poj.org/problem?id=2891 [算法] exgcd [代码] #include <algorithm> #include <bitset> #include <cctype> #include <cerrno> #include <clocale> #include <cmath> #include <complex> #include <cstdio> #incl…
题目链接:http://poj.org/problem?id=2891 题目大意: 求解同余方程组,不保证模数互质 题解: 扩展中国剩余定理板子题 #include<algorithm> #include<cstring> #include<cstdio> #include<iostream> #include<cmath> using namespace std; typedef long long ll; +; int k; ll m[N],…
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: 17877   Accepted: 6021 Description Elina is reading a book written by Rujia Liu, which introduces a strange way to express non-negative integers. The way is…
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…
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…
2891 -- Strange Way to Express Integers import java.math.BigInteger; import java.util.Scanner; public class Main { static final BigInteger ZERO = new BigInteger("0"); static final BigInteger ONE = new BigInteger("1"); static BigInteger…
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…
求解方程组 X%m1=r1 X%m2=r2 .... X%mn=rn 首先看下两个式子的情况 X%m1=r1 X%m2=r2 联立可得 m1*x+m2*y=r2-r1 用ex_gcd求得一个特解x' 得到X=x'*m1+r2 X的通解X'=X+k*LCM(m1,m2) 上式可化为:X'%LCM(m1,m2)=X 到此即完成了两个式子的合并,再将此式子与后边的式子合并,最后的得到的X'即为答案的通解,求最小整数解即可. #include<stdio.h> #include<string.h…
Reference: http://www.cnblogs.com/ka200812/archive/2011/09/02/2164404.html 之前说过中国剩余定理传统解法的条件是m[i]两两互质,所以这题就不能用传统解法了= = 其实还有种方法: 先来看只有两个式子的方程组: c≡b1 (mod a1) c≡b2 (mod a2) 变形得c=a1*x+b1,c=a2*x+b2 a1*x-a2*y=b2-b1 可以用扩展欧几里得求出x和y,进而求出c 那么多个式子呢?可以两个两个的迭代求.…
题目链接 虽然我不懂... #include <cstdio> #include <cstring> #include <map> #include <cmath> using namespace std; #define LL __int64 LL p[],o[]; LL x,y; LL ext_eulid(LL a,LL b) { LL t,d; ) { x = ; y = ; return a; } d = ext_eulid(b,a%b); t =…
求解一元线性同余方程组: x=ri(mod ai) i=1,2,...,k 解一元线性同余方程组的一般步骤:先求出前两个的解,即:x=r1(mod a1)     1x=r2(mod a2)     21式等价于x=r1+a1*m,2式等价于x=r2+a2*n联立可得:m*a1-n*a2=r2-r1=c若方程有解,则必须(a1,a2)|c设d=(a1,a2),那么如果有解,即可求得 m*a1-n*a2=d的解,m=m'则   m*a1-n*a2=c的解,m0=m'*c/d通解m*=m0+(a2/…
题目链接 题意:给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)整除时无解.   怎么推出的看…
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…
这个题乍一看跟剩余定理似的,但是它不满足两两互素的条件,所以不能用剩余定理,也是给了一组同余方程,找出一个X满足这些方程,如果找不到的话就输出-1 因为它不满足互素的条件,所以两个两个的合并,最后合成一个. 题目给定的是 M % m1 = r1 M % m2 = r2 ...... M % mn = rn 只需将两个式子合并成一个式子,那么这个合并的这个式子就可以继续和下面的式子继续合并,知道合到最后一个式子. 首先来看下两个式子怎么合并. M % m1 = r1    可以写成  M = k1…
题目链接 扩展中国剩余定理: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…
中国剩余定理的非互质形式 任意n个表达式一对对处理,故只需处理两个表达式. x = a(mod m) x = b(mod n) km+a = b (mod n) km = (a-b)(mod n) 利用扩展欧几里得算法求出k k = k0(mod n/(n,m)) = k0 + h*n/(n,m) x = km+a = k0*m+a+h*n*m/(n,m) = k0*m+a (mod n*m/(n,m)) #include <cstdio> #include <cstring> #…
\(mod\)存在不互素情况下的CRT #include<iostream> #include<algorithm> #include<cstdio> #include<cstring> #include<cstdlib> #include<cmath> #include<string> #include<vector> #include<stack> #include<queue> #…
题目链接 扩展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&…
题面就是让你解同余方程组(模数不互质) 题解: 先考虑一下两个方程 x=r1 mod(m1) x=r2 mod (m2) 去掉mod x=r1+m1y1   ......1 x=r2+m2y2   ......2 1-2可以得到 m1y1-m2y2=r1-r2 形同ax+by=c形式,可以判无解或者解出一个y1的值 带回1式可得到一个x的解x0=r1-y1a1 通解为x=x0+k*lcm(m1,m2) 即x=x0 mod(lcm(m1,m2)) 令M=lcm(m1,m2) R=x0 所以x满足x…
我真傻,真的 我单知道这道题在(b-b1)%d!=0时要判无解,哪成想自己却没有读完这组后面的数据而直接break掉...qwqfk 当 $ x \equiv b_1 (  mod    a_1  ) $ $ x \equiv b_2   ( mod  a_2 )  $ .... $x  \equiv b_n (mod a_n)$ 且 $a_1,a_2,...,a_n$ 不互质时,正常的中国剩余定理是用不了的 所以有了EX版 求解: 我们先看第1,2个方程,它们可以转化为: x=a1*k1+b1…
Strange Way to Express Integers Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 19509   Accepted: 6592 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: 8005   Accepted: 2378 Description Elina is reading a book written by Rujia Liu, which introduces a strange way to express non-negative integers. The way is…
扩展中国剩余定理板子 #include<iostream> #include<cstdio> using namespace std; const int N=100005; int n; long long m[N],r[N],M,R,x,y,d; void exgcd(long long a,long long b,long long &d,long long &x,long long &y) { if(!b) { d=a,x=1,y=0; return…
一种不断迭代,求新的求余方程的方法运用中国剩余定理. 总的来说,假设对方程操作.和这个定理的数学思想运用的不多的话.是非常困难的. 參照了这个博客的程序写的: http://scturtle.is-programmer.com/posts/19363.html 这个博客举例说的挺好的:http://blog.csdn.net/mishifangxiangdefeng/article/details/7109217 hdu 3579 Hello Kiki 中国剩余定理(不互质的情况) 对互质的情况…
Strange Way to Express Integers 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…
中国剩余定理模数不互质的情况主要有一个ax+by==k*gcd(a,b),注意一下倍数情况和最小 https://vjudge.net/problem/POJ-2891 #include <iostream> #include <cstdio> #include <queue> #include <algorithm> #include <cmath> #include <cstring> #define inf 2147483647…