POJ--1060--Modular multiplication of polynomials

Consider polynomials whose coefficients are 0 and 1. Addition of two polynomials is achieved by 'adding' the coefficients for the corresponding powers in the polynomials. The addition of coefficients is performed by addition modulo 2, i.e., (0 + 0) mod 2 = 0, (0 + 1) mod 2 = 1, (1 + 0) mod 2 = 1, and (1 + 1) mod 2 = 0. Hence, it is the same as the exclusive-or operation.

(x^6 + x^4 + x^2 + x + 1) + (x^7 + x + 1) = x^7 + x^6 + x^4 + x^2

Subtraction of two polynomials is done similarly. Since subtraction of coefficients is performed by subtraction modulo 2 which is also the exclusive-or operation, subtraction of polynomials is identical to addition of polynomials.

(x^6 + x^4 + x^2 + x + 1) - (x^7 + x + 1) = x^7 + x^6 + x^4 + x^2

Multiplication of two polynomials is done in the usual way (of course, addition of coefficients is performed by addition modulo 2).

(x^6 + x^4 + x^2 + x + 1) (x^7 + x + 1) = x^13 + x^11 + x^9 + x^8 + x^6 + x^5 + x^4 + x^3 + 1

Multiplication of two polynomials f(x) and g(x) modulo a polynomial h(x) is the remainder of f(x)g(x) divided by h(x).

(x^6 + x^4 + x^2 + x + 1) (x^7 + x + 1) modulo (x^8 + x^4 + x^3 + x + 1) = x^7 + x^6 + 1
The largest exponent of a polynomial is called its degree. For example, the degree of x^7 + x^6 + 1 is 7.

Given three polynomials f(x), g(x), and h(x), you are to write a program that computes f(x)g(x) modulo h(x).
We assume that the degrees of both f(x) and g(x) are less than the degree of h(x). The degree of a polynomial is less than 1000.

Since coefficients of a polynomial are 0 or 1, a polynomial can be represented by d+1 and a bit string of length d+1, where d is the degree of the polynomial and the bit string represents the coefficients of the polynomial. For example, x^7 + x^6 + 1 can be represented by 8 1 1 0 0 0 0 0 1.

Input

The input consists of T test cases. The number of test cases (T) is given in the first line of the input file. Each test case consists of three lines that contain three polynomials f(x), g(x), and h(x), one per line. Each polynomial is represented as described above.

Output

The output should contain the polynomial f(x)g(x) modulo h(x), one per line.

Sample Input
2 
7 1 0 1 0 1 1 1 
8 1 0 0 0 0 0 1 1 
9 1 0 0 0 1 1 0 1 1 
10 1 1 0 1 0 0 1 0 0 1 
12 1 1 0 1 0 0 1 1 0 0 1 0 
15 1 0 1 0 1 1 0 1 1 1 1 1 0 0 1
Sample Output
8 1 1 0 0 0 0 0 1 
14 1 1 0 1 1 0 0 1 1 1 0 1 0 0 

思路:具体实现步骤看代码即可;

注意:gf这个数组的空间一定要开大,否则可能导致数据溢出; 

//多项式的乘法
#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
const int MAX=1010;
int g[MAX],f[MAX],h[MAX],gf[MAX*MAX];//gf[@@] 空间一定要开大,否则aw; 
int a,b,c;
int d;
//写一个比较函数
int compare(){
	if(d<c)
		return -1;
	if(d>c)
		return 1;
	for(int i=d-1;i>=0;i--){ 
		if(gf[i]==h[i])
			continue;
		else if(gf[i]>h[i])
			return 1;
		else if(gf[i]<h[i])
			return -1; 
	} 
	return 0;//两者完全相同 
} 
int main(){
	int t;
	cin>>t;
	while(t--){
		memset(h,0,sizeof(h));
		memset(gf,0,sizeof(gf));//不能少,否则会第二个实例结果会出现问题 		
		cin>>a;
		a--;
		for(int i=a;i>=0;i--){
			cin>>g[i];//存入g(x)函数 
		}
		cin>>b;
		b--;
		for(int i=b;i>=0;i--){
			cin>>f[i];//存入f(x)函数 
		}
		cin>>c;
		c--;
		for(int i=c;i>=0;i--){
			cin>>h[i];//存入h(x)函数 
		}
		//计算乘法
		for(int i=a;i>=0;i--)
			for(int j=b;j>=0;j--){
				gf[i+j]=gf[i+j]^(g[i]&f[j]);
			} 
		d=a+b;
        //求余就是多项式相减到小于除数  
    	while(compare()>=0){
    	 	int e=d-c;//长度相减 
            for(int i=d; i-e>=0; i--){  
                gf[i]=gf[i]^h[i-e];  
            }  
            while(d&&(gf[d]==0))  
                d--; 
        }  
        cout<<d+1;
        for(int i=d;i>=0;i--)
        	cout<<" "<<gf[i];
        	cout<<endl; 
	} 
} 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值