中国剩余定理求解同余线性方程组(模数互素和非互素的情况)
来源:互联网 发布:小米换苹果 迁移数据 编辑:程序博客网 时间:2024/06/05 02:05
参考:http://yzmduncan.iteye.com/blog/1323599
中国剩余定理
中国剩余定理是中国古代求解一次同余方程组的方法,是数论中的一个重要定理。
设m1,m2,m3,...,mk是两两互素的正整数,即gcd(mi,mj)=1,i!=j,i,j=1,2,3,...,k.
则同余方程组:
x = a1 (mod n1)
x = a2 (mod n2)
...
x = ak (mod nk)
模[n1,n2,...nk]有唯一解,即在[n1,n2,...,nk]的意义下,存在唯一的x,满足:
x = ai mod [n1,n2,...,nk], i=1,2,3,...,k。
解可以写为这种形式:
x = sigma(ai* mi*mi') mod(N)
其中N=n1*n2*...*nk,mi=N/ni,mi'为mi在模ni乘法下的逆元。
中国剩余定理非互质版
中国剩余定理求解同余方程要求模数两两互质,在非互质的时候其实也可以计算,这里采用的是合并方程的思想。下面是详细推导。
例
FZU1402 中国剩余定理
- #include <iostream>
- #include <cstdio>
- #include <cstring>
- using namespace std;
- typedef __int64 int64;
- int64 a[15],b[15];
- int64 Extend_Euclid(int64 a, int64 b, int64&x, int64& y)
- {
- if(b==0)
- {
- x=1,y=0;
- return a;
- }
- int64 d = Extend_Euclid(b,a%b,x,y);
- int64 t = x;
- x = y;
- y = t - a/b*y;
- return d;
- }
- //求解模线性方程组x=ai(mod ni)
- int64 China_Reminder(int len, int64* a, int64* n)
- {
- int i;
- int64 N = 1;
- int64 result = 0;
- for(i = 0; i < len; i++)
- N = N*n[i];
- for(i = 0; i < len; i++)
- {
- int64 m = N/n[i];
- int64 x,y;
- Extend_Euclid(m,n[i],x,y);
- x = (x%n[i]+n[i])%n[i];
- result = (result + m*a[i]*x%N)%N;
- }
- return result;
- }
- int main()
- {
- int n;
- while(scanf("%d",&n)!=EOF)
- {
- for(int i = 0; i < n; i++)
- scanf("%I64d %I64d",&a[i],&b[i]);
- printf("%I64d\n",China_Reminder(n,b,a));
- }
- return 0;
- }
POJ2891 非互质版
- /**
- 中国剩余定理(不互质)
- */
- #include <iostream>
- #include <cstdio>
- #include <cstring>
- using namespace std;
- typedef __int64 int64;
- int64 Mod;
- int64 gcd(int64 a, int64 b)
- {
- if(b==0)
- return a;
- return gcd(b,a%b);
- }
- int64 Extend_Euclid(int64 a, int64 b, int64&x, int64& y)
- {
- if(b==0)
- {
- x=1,y=0;
- return a;
- }
- int64 d = Extend_Euclid(b,a%b,x,y);
- int64 t = x;
- x = y;
- y = t - a/b*y;
- return d;
- }
- //a在模n乘法下的逆元,没有则返回-1
- int64 inv(int64 a, int64 n)
- {
- int64 x,y;
- int64 t = Extend_Euclid(a,n,x,y);
- if(t != 1)
- return -1;
- return (x%n+n)%n;
- }
- //将两个方程合并为一个
- bool merge(int64 a1, int64 n1, int64 a2, int64 n2, int64& a3, int64& n3)
- {
- int64 d = gcd(n1,n2);
- int64 c = a2-a1;
- if(c%d)
- return false;
- c = (c%n2+n2)%n2;
- c /= d;
- n1 /= d;
- n2 /= d;
- c *= inv(n1,n2);
- c %= n2;
- c *= n1*d;
- c += a1;
- n3 = n1*n2*d;
- a3 = (c%n3+n3)%n3;
- return true;
- }
- //求模线性方程组x=ai(mod ni),ni可以不互质
- int64 China_Reminder2(int len, int64* a, int64* n)
- {
- int64 a1=a[0],n1=n[0];
- int64 a2,n2;
- for(int i = 1; i < len; i++)
- {
- int64 aa,nn;
- a2 = a[i],n2=n[i];
- if(!merge(a1,n1,a2,n2,aa,nn))
- return -1;
- a1 = aa;
- n1 = nn;
- }
- Mod = n1;
- return (a1%n1+n1)%n1;
- }
- int64 a[1000],b[1000];
- int main()
- {
- int i;
- int k;
- while(scanf("%d",&k)!=EOF)
- {
- for(i = 0; i < k; i++)
- scanf("%I64d %I64d",&a[i],&b[i]);
- printf("%I64d\n",China_Reminder2(k,b,a));
- }
- return 0;
- }
阅读全文
0 0
- 中国剩余定理求解同余线性方程组(模数互素和非互素的情况)
- 中国剩余定理求解同余线性方程组—(互素和非互素的情况)
- 中国剩余定理求解同余线性方程组—(互素和非互素的情况)
- 解线性同余方程 中国剩余定理 和 非互质的中国剩余定理
- 模数非互质的同余方程组(非互质版中国剩余定理)
- 同余方程组,中国剩余定理,孙子定理(学习)
- 中国剩余定理(CRT):求解模线性方程组
- 再说中国剩余定理、扩展欧几里德和同余方程组
- POJ 1006 Biorhythms(中国剩余定理,同余方程)
- 中国剩余定理(同余方程组)小结
- 同余 模算术 中国剩余定理
- 中国剩余定理和一元同余线性方程的详细讲解
- 算法学习之模线性同余方程组(中国剩余定理+求解同余方程组) poj1006+hdu3579
- hdu 1788Chinese remainder theorem again(中国剩余定理or同余性质的应用)
- Hello Kiki hdu 3579 求解一元线性同余方程组ps:中国剩余定理
- poj 1006+hdu 1788(中国剩余定理求解同余方程组)
- 中国剩余定理 孙子定理 一元线性同余方程组
- Strange Way to Express Integers(扩展欧几里得+乘法逆元+中国剩余定理求解非互质的模线性方程组)
- 线性筛法
- (二十五)基础系列 面向对象
- python @wraps修饰器
- linux zip/unzip命令详解
- lower_bound & binary_search & equal_range
- 中国剩余定理求解同余线性方程组(模数互素和非互素的情况)
- sizeof在程序中的深层剖析
- 预算聚合引擎模型分析
- Javascript模块化编程(三):require.js的用法
- tomcat安装配置
- spring boot 读取配置文件(application.yml)中的属性值
- SQL SERVER跨数据库查询
- 初试ajax
- 厉害了,我的中国造@全闪存中国新势力