Description
Problem C
Polynomial GCD
Input:Standard Input
Output:Standard output
Given two PolynomialsF (x)AndG (x)InZn, You have to find theirGCDPolynomial, ie, A PolynomialR (X)(Also inZn) Which has the greatest degree of all the polynomials inZnThat divide bothF (x)AndG (x). There can be more than one such polynomial, of which you are to find the one with a leading coefficient1(1Is the unity inZn. Such polynomial is also calledMonic Polynomial).
(Note: A FunctionF (x)Is inZnMeans all the coefficients inF (x)IsModulo n.)
Input
There will be no more101Test Cases. Each test case consists of three lines: the first line hasN, Which will be a prime number not more1500. The second and third lines give the two PolynomialsF (x)AndG (x). The polynomials are represented by first an integerDWhich represents the degree of the polynomial, followed(D + 1)Positive integers representing the coefficients of the polynomial. The coefficients are in decreasing order of exponent. Input endsN = 0. The valueDWon't be more100.
Output
For each test case, print the test case number andR (X), In the same format as the input
Sample input output for sample input
3 3 2 2 1 1 4 1 0 2 22 0 |
Case 1: 2 1 2 1
|
Problem setter: sadrulhabibchowdhury
Special thanks: Derek kisman, EPS
Note: The first sample input has2x3 + 2X2 + x + 1AndX4 + 2X2 + 2x + 2As the functions.
Calculate the maximum common polynomial of two polynomials.
Thought: applied the Template
# Include <iostream> # include <cstdio> # include <cstring> # include <algorithm> # include <vector> typedef long ll; using namespace STD; const int maxn = 100005; vector <int> G [maxn]; int MOD; int pow_mod (int A, int B) {int ans = 1; while (B) {If (B & 1) ans = ans * A % MOD; B >>= 1; A = A * A % MOD;} return ans ;} /* calculate the maximum public term of Polynomial */vector <int> poly_gcd (vector <int> A, vector <int> B) {If (B. size () = 0) return a; int T =. size ()-B. size (); vector <int> C; For (INT I = 0; I <= T; I ++) {int TMP = A [I] * pow_mod (B [0], mod-2) % MOD; For (ll j = 0; j <B. size (); j ++) A [I + J] = (a [I + J]-TMP * B [J] % mod + mod) % MOD ;} int P =-1; for (INT I = 0; I <. size (); I ++) {if (a [I]! = 0) {P = I; break;} If (P> = 0) {for (INT I = P; I <. size (); I ++) C. push_back (A [I]);} return poly_gcd (B, c);} int main () {int CAS = 1; while (scanf ("% d ", & mod )! = EOF & mod) {for (INT I = 0; I <2; I ++) g [I]. clear (); int A, B; For (INT I = 0; I <2; I ++) {scanf ("% d", & ); for (Int J = 0; j <= A; j ++) {scanf ("% d", & B); G [I]. push_back (B) ;}}vector <int> ans = poly_gcd (G [0], G [1]); printf ("case % d: % d ", CAS ++, ans. size ()-1); int CNT = pow_mod (ANS [0], mod-2); For (INT I = 0; I <ans. size (); I ++) {ans [I] = ans [I] * CNT % MOD; printf ("% d", ANS [I]);} printf ("\ n");} return 0 ;}
Ultraviolet-10951 polynomial gcd (maximum common polynomial)