Topic Links:
http://acm.hdu.edu.cn/showproblem.php?pid=3579
Main topic:
Kiki had x coins, she counted n times in different ways, each time she divided the coins into equal-sized groups and recorded each A group of coins
The number of MI and the last remaining coins in the number of AI. So here's the question: how many coins are there in total?
Ideas:
The typical unary linear congruence equation Group x = Ai (mod Mi) solution. The problem requires the output of the minimum positive integer solution, if the same remainder is obtained
The solution of the equations is 0, then the answer is the least common multiple of all mi.
AC Code:
#include <iostream> #include <algorithm> #include <cstdio> #include <cstring>using namespace STD; #define LL __int64ll GCD (ll a,ll b) {if (b = = 0) return A; Return GCD (b,a%b);} void Exgcd (ll a,ll b,ll &d, ll &x,ll &y) {if (!b) {x = 1; y = 0; D = A; } else {EXGCD (b,a%b,d,y,x); Y-= x * (A/b); }}ll A[15],r[15];int Main () {LL A,B,C,D,X0,Y0,LCM; int n,m,kase = 0; scanf ("%d", &n); while (n--) {LCM = 1; BOOL flag = 1; scanf ("%d", &m); for (int i = 1; I <= M; ++i) {scanf ("%i64d", &a[i]); LCM = LCM/GCD (A[I],LCM) * A[i]; } for (int i = 1; I <= M; ++i) scanf ("%i64d", &r[i]); printf ("Case%d:", ++kase); for (int i = 2; I <= M; ++i) {a = a[1]; b = A[i]; c = r[i]-r[1]; EXGCD (A,B,D,X0,Y0); if (c%d! = 0) {flag = 0; Break } LL temp = b/d; x0 = (x0* (C/D)%temp + temp)% temp; R[1] = a[1]*x0 + r[1]; A[1] = a[1]* (a[i]/d); } if (!flag) printf (" -1\n"); else {if (r[1]! = 0) printf ("%i64d\n", r[1]); else printf ("%i64d\n", LCM); }} return 0;}
HDU3579 Hello Kiki "one-element linear congruence Equation Group"