zoj1026

原题:

Modular multiplication of polynomials

Time limit: 1 Seconds   Memory limit: 32768K  

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. (x6 + x4 + x2 + x + 1) + (x7 + x + 1) = x7 + x6 + x4 + x2 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. (x6 + x4 + x2 + x + 1) - (x7 + x + 1) = x7 + x6 + x4 + x2 Multiplication of two polynomials is done in the usual way (of course, addition of coefficients is performed by addition modulo 2). (x6 + x4 + x2 + x + 1) (x7 + x + 1) = x13 + x11 + x9 + x8 + x6 + x5 + x4 + x3 + 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). (x6 + x4 + x2 + x + 1) (x7 + x + 1) modulo (x8 + x4 + x3 + x + 1) = x7 + x6 + 1 The largest exponent of a polynomial is called its degree. For example, the degree of x7 + x6 + 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, x7 + x6 + 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. 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

Output for the Sample Input

8 1 1 0 0 0 0 0 1

14 1 1 0 1 1 0 0 1 1 1 0 1 0 0

题目的大意就是有三个多项式 f(x)、g(x)、h(x) , 计算出 f(x)*g(x)Modh(x), 但是这个多项式的系数不是十进制的,而是只能由0、1表示. 1+1得0, 0-1得1.

思路:利用布尔型数组模拟多项式, 并利用与、或、异或实现多项式的加减乘.

//Zoj Acm 1026 //Run time: 00:00.08 //Run memory: 844K #include<iostream> using namespace std; int main() { bool *f,*g,*h,*temp; bool flag; int fsize,gsize,hsize; int t,i,n; cin>>t; while(t>0) { cin>>fsize; f=new bool [fsize]; for(i=fsize-1;i>=0;i--) cin>>f[i]; cin>>gsize; g=new bool [gsize]; for(i=gsize-1;i>=0;i--) cin>>g[i]; n=fsize+gsize-1; temp=new bool[n]; for(i=0;i<n;i++) temp[i]=false; for(i=0;i<fsize;i++) for(n=0;n<gsize;n++) if(temp[i+n])temp[i+n]^=(f[i]&&g[n]); else temp[i+n]|=(f[i]&&g[n]); cin>>hsize; h=new bool[hsize]; for(i=hsize-1;i>=0;i--) cin>>h[i]; n=fsize+gsize-1; while(n>=hsize) { for(i=hsize-1;i>=0;i--) if(temp[n-(hsize-i)])temp[n-(hsize-i)]^=h[i]; else temp[n-(hsize-i)]|=h[i]; flag=false; for(i=n-1;i>=0;i--) if(temp[i]) { n=i+1; flag=true; break; } if(!flag)n=0; } cout<<n; for(i=n-1;i>=0;i--) cout<<' '<<temp[i]; cout<<endl; t--; delete [] f; delete [] g; delete [] h; delete [] temp; } return 0; }


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值