Given 2n integers, A . 1 , A 2 , ......, A n-, m . 1 , m 2 , ......, m n- ,
Seeking a smallest positive integer x, satisfies ≦ i ≦ n ∀, X m ≡ I (A MOD I )
If there is no solution "-1", or the output of the smallest positive integer x
data range:
1 ≤ a i ≤ 2 31 -1
1 ≤ m i <a i
1 ≤ n ≤ 25
# Explanations
First consider the first two formulas:
x ≡ m1 (mod a1) ↔ x = k1 * a1 + m1
x ≡ m2 (mod a2) ↔ x = k2 * a2 + m2
k1 * a1 + m1 = k2 * a2 + m2
k1 * a1 -k2 * a2 = m2-m1
There is no solution that is determine what gcd (A 1 , A 2 ) | (m 2 -m1) is established
European expansion determined K . 1 * A . 1 -k 2 * A 2 = GCD (A . 1 , A 2 ) Solution K . 1 '' and K 2 ''
k1'=k1'' * (m2-m1)/gcd(a1,a2)
k2'=k2'' * (m2-m1)/gcd(a1,a2)
K = a x . 1 * A . 1 + m . 1 or K = x 2 * A 2 + m 2 can be obtained through the solutions of x
Wherein K . 1 the general solution K = . 1 + K * (A 2 / GCD (A . 1 , A 2 )) clearly K . 1 when the acquired minimum x minimum
But when the formula behind and merged with the new constraints,
The minimum value of x may change it to not find a specific x,
Obtained by k 1 or k 2 constituting a new congruence equation general solution of
The K . 1 the general solution K = . 1 ' + K * (A 2 / GCD (A . 1 , A 2 )) into X = K . 1 * A . 1 + m . 1
x = ( k1' + k* ( a2/gcd(a1,a2) ) ) * a1 + m1
x = k * ( a1 * a2 )/gcd(a1,a2) + k1'*a1+m1
a'=( a1 * a2 )/gcd(a1,a2) , m'=k1'*a1+m1
x = k * a' + m'
K = X 3 * of a 3 + m No 3
Continuously merging into k-1, determined in accordance with the smallest x k is the general solution,
k = k0 + t * (an/gcd(a',an))
Clearly K 0 % (A n- / GCD (A ', A n- )) i.e., the smallest solution