中国剩余定理(自己)

数论的题目中会涉及一些中国剩余定理的问题,会给你一些互质的数,比如a, b, c.

然后呢,有一个n,说这个n对a取余为dd,  n对b取余为ff,  n对c取余为gg.   现在让你来

计算这个n的最小数值。

解法如下:

x = (a 与 b 的某个公倍数) % c = = 1;

y = (b 与 c 的某个公倍数) % a = = 1;

z = (a 与 c 的某个公倍数) % b = = 1;

n =  (   x*gg  +  y*dd  + z*ff  ) % (  x*Y*z ) ;

为了避免特殊数据( 比如出现 负值 ),可以改写成这样:

n =  (  x*gg + y*dd + z*ff  +x*Y*Z ) % ( x*y*z );

中国剩余定理(自己)

时间: 2024-11-10 13:19:08

中国剩余定理(自己)的相关文章

Hello Kiki(中国剩余定理——不互质的情况)

Hello Kiki Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 247 Accepted Submission(s): 107   Problem Description One day I was shopping in the supermarket. There was a cashier counting coins serio

Biorhythms(中国剩余定理)

Biorhythms Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 127339   Accepted: 40342 Description Some people believe that there are three cycles in a person's life that start the day he or she is born. These three cycles are the physical,

lightoj 1319 - Monkey Tradition (中国剩余定理)

1319 - Monkey Tradition PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB In 'MonkeyLand', there is a traditional game called "Bamboo Climbing". The rules of the game are as follows: 1)       There are N monkeys who play

HDU 5446 Unknown Treasure(lucas + 中国剩余定理 + 模拟乘法)

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5446 题目大意:求C(n, m) % M, 其中M为不同素数的乘积,即M=p1*p2*...*pk, 1≤k≤10.1≤m≤n≤10^18. 分析: 如果M是素数,则可以直接用lucas定理来做,但是M不是素数,而是素数的连乘积.令C(n, m)为 X ,则可以利用lucas定理分别计算出 X%p1,X%p2, ... , X % pk的值,然后用中国剩余定理来组合得到所求结果. 比较坑的地方是,

UVA 11754 Code Feat (枚举,中国剩余定理)

转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud C Code Feat   The government hackers at CTU (Counter-Terrorist Unit) have learned some things about the code, but they still haven't quite solved it.They know it's a single, strictly positive

hdu1573X问题(不互素的中国剩余定理)

X问题 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3295    Accepted Submission(s): 1068 Problem Description 求在小于等于N的正整数中有多少个X满足:X mod a[0] = b[0], X mod a[1] = b[1], X mod a[2] = b[2], -, X mo

对于中国剩余定理(CRT)的初步理解

以前觉得用中国剩余定理来求同余方程组很鸡肋,因为可以用拓展欧几里得算法来构造出一种更加强大(可以处理取模的数(默认为mi)不互质的情况)的算法. 今天查了点资料,发现我太天真了. 首先讲讲中国剩余定理: 即 : x ≡ a[i] (mod m[i]) 1<= i <= r (m[i] 两两互质) 求这个同余方程组可以快速算: x = ∑M/m[i] * Inv(M/m[i], m[i]) * a[i] (mod M) 其中M = m[1]*m[2]*m[3]...m[r]  , Inv(x,

数论F - Strange Way to Express Integers(不互素的的中国剩余定理)

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.

poj1006生理周期(中国剩余定理)

1 /* 2 中国剩余定理可以描述为: 3 若某数x分别被d1..….dn除得的余数为r1.r2.….rn,则可表示为下式: 4 x=R1r1+R2r2+…+Rnrn+RD 5 其中R1是d2.d3.….dn的公倍数,而且被d1除,余数为1:(称为R1相对于d1的数论倒数) 6 R1 . 7 R2 . 8 … . 9 Rn是d1.d2.….dn-1的公倍数,而且被dn除,余数为1: 10 D是d1.d2.….的最小公倍数: 11 R是任意整数(代表倍数),可根据实际需要决定: 12 且d1..…

HDU 5446 中国剩余定理+lucas

Unknown Treasure Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 2389    Accepted Submission(s): 885 Problem Description On the way to the next secret treasure hiding place, the mathematician