首页 > 其他 > 详细

UVA - 10951 Polynomial GCD (最大公共多项式)

时间:2014-08-25 17:09:34      阅读:483      评论:0      收藏:0      [点我收藏+]

Description

bubuko.com,布布扣

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 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 + 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 (最大公共多项式)

原文:http://blog.csdn.net/u011345136/article/details/38821067

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!