140. Integer Sequences time limit per test: 0.25 sec. A sequence A is called an integer sequence of length N if all its elements A1 A2 .. AN are non-negative integers less than 2 000 000 000. Consider two integer sequences of length N, A and X. The result of their multiplication (A*X) is an integer number R=A1*X1 + A2*X2 + .. + AN*XN. Your task is to solve the equation A*X=B (mod P), given the integer sequence A and the integer numbers Band P. Input The first line contains the integer numbers N (1<=N<=100) - the length of the integer sequences - P (1<=P<=10 000) and B (0<=B<=P-1). The second line contains the elements of the sequence A, separated by blanks: A1 A2 .. AN. Output You should print one line containing the word "YES" if there exists at least one integer sequence X which is a solution to the equation, or print "NO" otherwise. If the answer is "YES", the next line should contain N non-negative integers separated by blanks: X1 X2 .. XN. Sample Input #1 2 7 4 7 3 Sample Output #1 YES 0 6 Sample Input #2 3 10 1 2 4 6 Sample Output #2 NO 一道扩展欧几里得的题,开始DP还想了一会,结果发现还是道数学题。 由于有mod P,为了方便,可以表示为A1X1+A2X2+A3X3+...+AnXn+P*Q=B,Q∈Z。 对于A1X1+A2X2,我们可以求出A1X1+A2X2=gcd(A1,A2)的X1、X2,存入X数组中,再把gcd(A1,A2)当作新的元素,于是方程变为: k*gcd(A1,A2)+A3X3+...+An*Xn+P*Q=B,我们再对gcd(A1,A2),A3同样进行上述操作,求出k、X3,把之前求出的所有的Xi乘上k,再将X3加入X数组中,不断重复,直到算到An为止。 再把gcd(A1,A2,A3...An)与P进行同样的操作,同时把所有解乘上B/gcd(A1,A2...An,P),如果除不尽无解,否则输出,记得取模。
|