首页 > 代码库 > UVA - 10951 Polynomial GCD (最大公共多项式)
UVA - 10951 Polynomial GCD (最大公共多项式)
Description
Problem C
Polynomial GCD
Input: standard input
Output: standard output
Given two polynomials f(x) and g(x) in Zn, you have to find their GCD polynomial, ie, a polynomialr(x) (also in Zn) which has the greatest degree of all the polynomials inZn that divide both f(x) and g(x). There can be more than one such polynomial, of which you are to find the one with a leading coefficient of1 (1 is the unity in Zn. Such polynomial is also called amonic polynomial).
(Note: A function f(x) is in Zn means all the coefficients inf(x) is modulo n.)
Input
There will be no more than 101 test cases. Each test case consists of three lines: the first line hasn, which will be a prime number not more than 1500. The second and third lines give the two polynomialsf(x) and g(x). The polynomials are represented by first an integerD which represents the degree of the polynomial, followed by (D + 1) positive integers representing the coefficients of the polynomial. the coefficients are in decreasing order of Exponent. Input ends withn = 0. The value of D won‘t be more than 100.
Output
For each test case, print the test case number and r(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 has 2x3 + 2x2 + x + 1and x4 + 2x2 + 2x + 2 as the functions.
题意:求两个多项式的最大公共多项式
思路:套用了模板
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <vector> typedef long 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; } /*多项式求最大公共项*/ vector<int> poly_gcd(vector<int> a,vector<int> b) { if (b.size() == 0) return a; int t = a.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 < a.size(); i++) { if (a[i] != 0) { p = i; break; } } if (p >= 0) { for (int i = p; i < a.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", &a); 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; }
UVA - 10951 Polynomial GCD (最大公共多项式)