Chinese remainder theorem againTime
limit:1000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): Accepted submission (s): 777
Problem description I know some of the students are looking at the Chinese remainder theorem recently, this theorem itself, or relatively simple:
Assuming that the m1,m2,..., Mk 22, the following equation group is the same:
X≡A1 (mod M1)
X≡A2 (mod m2)
...
X≡ak (MoD mk)
There is a unique solution within the 0<=<m1m2...mk.
Kee Mi=m/mi (1<=i<=k), because (mi,mi) = 1, so there are two integers pi,qi meet Mipi+miqi=1, if you remember Ei=mi/pi, then there will be:
Ei≡0 (mod MJ), j!=i
Ei≡1 (mod MJ), j=i
Obviously, E1a1+e2a2+...+ekak is a solution of the equations, and this solution adds and subtract an integer multiple of M to get the least nonnegative integer solution.
This is the Chinese remainder theorem and its solution process.
Now there is a problem with this:
A positive integer n divided by M1 (m1-a), divided by M2 remainder (m2-a), divided by M3 remainder (m3-a), in short, divided by MI remainder (mi-a), where (a<mi<100 i=1,2,... I) to find the minimum number of conditions to satisfy.
Input data contains multiple sets of test instances, the first line of each instance is two integers I (1<i<10) and a, where I represents the number of M, a meaning as described above, followed by a line is I integer m1,m1 ... Mi,i=0 and a=0 end input, not processed.
Output for each test instance, export the minimum number of conditions that meet the criteria in a row. The output for each instance takes up one row.
Sample Input
2 12 30 0
Sample Output
5
Seeking n%mi==m-a;
IE (n+a)%mi==0
The least common multiple of this group of MI
Code:
#include <stdio.h>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\n", ans-a);} return 0;}
with June
Hdu 1788 Chinese remainder theorem again least common multiple