UVA - 10951 Polynomial GCD (最大公共多项式)
来源:互联网 发布:数据集成的两种方法 编辑:程序博客网 时间:2024/05/22 06:57
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 (最大公共多项式)
- UVA 10951 Polynomial GCD 多项式欧几里德求最大公共多项式
- UVA 10951 - Polynomial GCD(数论)
- uva 10951 - Polynomial GCD(欧几里得)
- UVa 10951 Polynomial GCD (数论)
- Uva 10719 - Quotient Polynomial (多项式求解)
- UVa Problem 10105 Polynomial Coefficients (多项式系数)
- uva 10719 Quotient Polynomial(多项式相除)
- uva 10105 - Polynomial Coefficients(多项式系数)
- UVa:10105 Polynomial Coefficients(多项式定理)
- Polynomial Coefficients - UVa 10105 多项式系数
- 多项式Polynomial
- HDu1296(Polynomial Problem)多项式的求解
- DSOJ Addition of Polynomial(多项式求和)
- 多项式曲线拟合(Polynomial Curve Fitting)
- Polynomial类 多项式
- HDU3892 Common Roots 多项式欧几里德求最大公共多项式
- poj2527——Polynomial Remains(多项式相除)
- 选项卡调整子窗口位置和大小
- Makefile简明教程
- sqlite3数据的导入和导出
- Android程序对不同手机屏幕分辨率自适应的总结
- 什么是匿名内部类什么时侯用?
- UVA - 10951 Polynomial GCD (最大公共多项式)
- 精彩BUG人生[4] -- bug free or not
- 网络监控工具弥补云监控短板
- Search in Rotated Sorted Array
- 【读书笔记】安卓异步任务
- 自定义的一些button样式
- 9--Linux学习----信号
- Remove Duplicates from Sorted Array
- 线索二叉树_legend