poj1060 大数

来源:互联网 发布:减脂训练计划 知乎 编辑:程序博客网 时间:2024/06/07 00:47

Modular multiplication of polynomials
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 4289 Accepted: 1944

Description

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 





题意:就是给你多项式,然后让你求出两个多项式相乘,然后取模

这里有点特殊就是都是0  1 

然后注意一下输出0 0的情况就行了



下面find_max函数表示寻找最大的不为 0 的最高项



#include<stdio.h>#include<algorithm>#include<string.h>using namespace std;const int maxx=2010;int f[maxx],g[maxx],h[maxx];int sum[maxx],p;int pf,pg,ph;void init(){        scanf("%d",&pf);        for(int i=pf-1;i>=0;i--)            scanf("%d",&f[i]);        scanf("%d",&pg);        for(int i=pg-1;i>=0;i--)            scanf("%d",&g[i]);        scanf("%d",&ph);        for(int i=ph-1;i>=0;i--)            scanf("%d",&h[i]);}void Multiplication(){    memset(sum,0,sizeof(sum));    for(int i=0;i<pf;i++){        for(int j=0;j<pg;j++){            if(f[i]&&g[j]&&sum[i+j]==0)                sum[i+j]=1;            else if(f[i]&&g[j])                sum[i+j]=0;        }    }    p=pf+pg-1;}void mod(int x){    int temp[maxx];    memset(temp,0,sizeof(temp));    for(int i=ph-1;i>=0;i--)        temp[i+x]=h[i];    for(int i=0;i<p;i++){        if((temp[i]+sum[i])%2==0)            sum[i]=0;        else            sum[i]=1;    }}int find_max(){    for(int i=p;i>=0;i--)        if(sum[i])            return i;    return 0;}int main(){    //freopen("in.txt","r",stdin);    int T;    scanf("%d",&T);    while(T--)    {        init();        Multiplication();        p=find_max()+1;        while(p>=ph)        {            mod(p-ph);            p=find_max()+1;        }        if(p==1&&sum[0]==0)            printf("0 0\n");        else{            printf("%d",p);            for(int i=p-1;i>=0;i--)                printf(" %d",sum[i]);            puts("");        }    }    return 0;}


0 0
原创粉丝点击