Hdu 1788 Chinese remainder theorem again minimum public multiple
Chinese remainder theorem againTime Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission (s): 2014 Accepted Submission (s): 777
Problem Description I know that some of my colleagues are reading the Chinese residue theorem recently. This theorem is simple:
Suppose m1, m2 ,..., If mk is a pair of elements, the following equations are the same:
X ipva1 (mod m1)
X 127a2 (mod m2)
...
X ≡ ak (mod mk)
At 0 <= Note Mi = M/mi (1 <= I <= k), because (Mi, mi) = 1, there are two integers pi, qi meets the requirements of Mipi + miqi = 1, if you remember ei = Mi/pi, there will be:
Ei Jun 0 (mod mj), j! = I
Ei Jun 1 (mod mj), j = I
Obviously, e1a1 + e2a2 +... + Ekak is a solution of the equations. After adding or subtracting an integer multiple of M, the minimum non-negative integer solution can be obtained.
This is the Chinese Residue Theorem and Its Solution Process.
Now there is a problem like this:
A positive integer N divided by M1 remainder (M1-a), divided by M2 remainder (M2-a), divided by M3 remainder (M3-a), in short, divided by MI remainder (MI-a), where (
The Input data contains multiple groups of test instances. The first line of each instance is two integer I (1
For each test instance, Output the minimum number that meets the conditions in one row. The output of each instance occupies one row.
Sample Input
2 12 30 0
Sample Output
5
Evaluate N % Mi = m-;
That is, (n + a) % Mi = 0
That is, to find the minimum public multiple of this set of Mi
Code:
#include
typedef long long ll ;ll gcd(ll a , ll b){if(b == 0){return a;}gcd(b,a%b) ;}ll lcm(ll a , ll b){return a*b/gcd(a,b) ;}int main(){int n , a ;while(~scanf(%d%d,&n,&a) && (n+a)){ll ans = 1 ;for(int i = 0 ; i < n ; ++i){int m ;scanf(%d,&m) ;ans = lcm(ans,m) ;}printf(%I64d,ans-a) ;}return 0 ;}