中国剩余定理 Chinese remainder theorem(CRT)
来源:互联网 发布:慢跑鞋哪个牌子好 知乎 编辑:程序博客网 时间:2024/05/21 19:34
中国剩余定理又称孙子定理, 主要是为了解线同余方程组
- x ≡ a1 ( mod m1 )
- x ≡ a2 ( modm2 )
- x ≡ a3 ( modm3 )
- .........
- x ≡ an ( modmn )
成立条件是m1,m2, ... ,mn 两两互质, 则对任意的整数:a1,a2, ... ,an,方程组S 有解
并且可以构造出通解 x = (a1*t1*M1 + a2*t2*M2 + a3*t3*M3 +...+ an*tn*Mn ) + KM
在模M的意义下 x =( a1*t1*M1 + a2*t2*M2 + a3*t3*M3 +...+ an*tn*Mn )
M = m1 *m2 *m3 * ... *mn ,
Mi = M / mi
ti
为 Mi
模 mi 意义下的逆元, 即 ti * Mi ≡ 1 ( mod mi ) 显然带入方程组中可验证正确性,证明略
//非递归的扩展欧几里德算法//返回a、b的gcd,同时x、y满足ax+by=gcdint_t _exEuclid(int_t a,int_t b,int_t&x,int_t&y){ int_t x0 = 1, y0 = 0; int_t x1 = 0, y1 = 1; x = 0; y = 1; int_t r = a % b; int_t q = ( a - r ) / b; while( r ){ x = x0 - q * x1; y = y0 - q * y1; x0 = x1; y0 = y1; x1 = x; y1 = y; a = b; b = r; r = a % b; q = ( a - r ) / b; } return b;}//求a相对于p的逆元,a、p互质才存在逆元int_t _inv(int_t a,int_t p){ int_t x,y; int_t r = _exEuclid(a,p,x,y); if ( r != 1 ) return 0; x = x % p; if ( x < 0 ) x += p; return x;}// 中国剩余定理int_t CRT( int n ,int_t *remain, int_t *mod){ int_t M = 1, x = 0; for (int i = 0;i < n;++i) M *= mod[i]; for ( int i = 0;i < n;++i ){ M /= mod[i]; x += remain[i] * M * _inv(M,mod[i]) ; M *= mod[i]; } return x;}
0 0
- 中国剩余定理 Chinese remainder theorem(CRT)
- 中国剩余定理(chinese remainder theorem)【模板】
- Chinese remainder theorem again(hdu1788中国剩余定理)
- HDU1788 Chinese remainder theorem again 中国剩余定理
- HDU 1788 Chinese remainder theorem again 中国剩余定理转换
- 中国剩余定理(chinese remainder theorem)【模板】
- HDU 1788 Chinese remainder theorem again 中国剩余定理
- HDU1788 Chinese remainder theorem again【中国剩余定理】
- Chinese remainder theorem again(中国剩余定理+不互质版+hud1788)
- hdu 1788 Chinese remainder theorem again 披着中国剩余定理的皮
- hdu 1788Chinese remainder theorem again(中国剩余定理or同余性质的应用)
- 中国剩余定理(余数定理)Chinese remainder
- HDU1788Chinese remainder theorem again(中国剩余定理 简单)
- Chinese Remainder Theorem
- 中国剩余定理 【CRT】 【记录】
- 中国剩余定理(CRT)
- 中国剩余定理(CRT)
- [CRT][中国剩余定理]膜法
- solr的基本配置(一)
- 工厂方法模式
- 开博留念
- Builder
- zurmo视图配置
- 中国剩余定理 Chinese remainder theorem(CRT)
- 使用UIBezierPath和CAShapeLayer画各种图形
- 观察者模式
- easyUI日常问题之自定义输入框验证
- C4.5决策树算法
- Java 多线程(二) 线程的实现
- Lua 字符串
- AFN 的一些记录
- linux安装oracle 11g r2 详细步骤及问题