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

Description

Download as PDF

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;
}


一元多项式表示为 a_n * x^n + a_(n-1) * x^(n-1) + ... + a_1 * x + a_0,其中 a_i 是系数,x 是变量,n 是指数。 要实现一元多项式的链表表示,我们可以定义一个结构体来表示多项式的每一项,包括系数和指数,并使用指针来连接每一项。示例代码如下: ``` #include <stdio.h> #include <stdlib.h> // 定义多项式项的结构体 typedef struct Node { int coefficient; // 系数 int exponent; // 指数 struct Node* next; // 下一项 } Node; // 创建多项式 Node* createPolynomial() { Node* head = (Node*)malloc(sizeof(Node)); // 创建头结点 head->next = NULL; Node* current = head; int coefficient, exponent; printf("请输入项数: "); int count; scanf("%d", &count); for (int i = 0; i < count; i++) { printf("请输入第%d项的系数和指数: ", i + 1); scanf("%d %d", &coefficient, &exponent); Node* newNode = (Node*)malloc(sizeof(Node)); newNode->coefficient = coefficient; newNode->exponent = exponent; newNode->next = NULL; current->next = newNode; current = newNode; } return head; } // 输出多项式 void printPolynomial(Node* polynomial) { Node* current = polynomial->next; while (current != NULL) { printf("%dx^%d", current->coefficient, current->exponent); if (current->next != NULL) { printf(" + "); } current = current->next; } printf("\n"); } int main() { Node* polynomial = createPolynomial(); printf("多项式为: "); printPolynomial(polynomial); return 0; } ``` 通过上述代码,我们可以创建一个包含多项式的链表,并打印出其内容。你可以根据需要对多项式链表进行其他基本操作的实现,如插入、删除、合并等。 此外,你还可以在多项式链表的基础上实现一个简单的多项式计算器。你可以根据用户输入的操作,对多项式进行加法、减法、乘法等运算,最后输出结果。在计算过程中,你需要实现将多项式链表转换为多项式数组的功能,并根据相应的运算规则进行计算。这样,你就可以通过简单的计算器来计算和操作一元多项式了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值