UVA - 10951 Polynomial GCD (最大公共多项式)
2014-08-25 15:24
309 查看
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 polynomial
r(x) (also in Zn) which has the greatest degree of all the polynomials in
Zn 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 of
1 (1 is the unity in Zn. Such polynomial is also called a
monic polynomial).
(Note: A function f(x) is in Zn means all the coefficients in
f(x) is modulo n.)
Input
There will be no more than 101 test cases. Each test case consists of three lines: the first line has
n, which will be a prime number not more than 1500. The second and third lines give the two polynomials
f(x) and g(x). The polynomials are represented by first an integer
D 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 with
n = 0. The value of D won't be more than
100.
Problem setter: SadrulHabibChowdhury
Special Thanks: Derek Kisman, EPS
Note: The first sample input has 2x3 + 2x2 + x + 1
and x4 + 2x2 + 2x + 2 as the functions.
题意:求两个多项式的最大公共多项式
思路:套用了模板
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 polynomial
r(x) (also in Zn) which has the greatest degree of all the polynomials in
Zn 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 of
1 (1 is the unity in Zn. Such polynomial is also called a
monic polynomial).
(Note: A function f(x) is in Zn means all the coefficients in
f(x) is modulo n.)
Input
There will be no more than 101 test cases. Each test case consists of three lines: the first line has
n, which will be a prime number not more than 1500. The second and third lines give the two polynomials
f(x) and g(x). The polynomials are represented by first an integer
D 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 with
n = 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 inputSample Input Output for Sample Input
3 3 2 2 1 1 4 1 0 2 22 0 | Case 1: 2 1 2 1 |
Special Thanks: Derek Kisman, EPS
Note: The first sample input has 2x3 + 2x2 + x + 1
and 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 多项式欧几里德求最大公共多项式
- UVA 10951 - Polynomial GCD(数论)
- HDU3892 Common Roots 多项式欧几里德求最大公共多项式
- UVA 10951 - Polynomial GCD(数论)
- Uva 11488(Trie数统计最大公共子串长度*个数)
- uva 10951 - Polynomial GCD(欧几里得)
- UVa 10951 Polynomial GCD (数论)
- 求公共前缀长度与所选字符串个数的乘积的最大值 Trie树求最值 UVA 11488 Hyper Prefix Sets
- UVA 10951 - Polynomial GCD []【数论】
- UVa 11488 Hyper Prefix Sets Trie树计算最大公共前缀长度*个数
- uva 10951 - Polynomial GCD(欧几里得)
- UVA 12125 - March of the Penguins(最大流)
- UVAlive2531 The K-League(最大流)
- UVA-10249 - The Grand Dinner(最大流)
- UVA 10048 Audiophobia 任意两点的路径上最大的边
- 100道动态规划——36 UVA 1220 Party at Hali-Bula 树形DP 最大独立集 思考欠妥
- HDU - 4512吉哥系列故事――完美队形I最大上升公共子序列
- UVA 10755 Garbage Heap 最大子立方体和
- UVALive 2531 The K-League 最大流
- HDU 1513 Palindrome(dp最大公共子序列+滚动数组)