zoj 1026 Modular multiplication of polynomials

Modular multiplication of polynomials

Time Limit: 2 Seconds      Memory Limit: 65536 KB

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 


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)mod h(x)。

做法:用num1,num2,num3,l1,l2,l3分別記錄f,g,h中的多項式係數以及長度,首先算出f(x)*g(x)記爲ans(x),乘法就是兩個多項式直接相乘(代碼中有解釋),因爲係數要mod2所以在記錄答案的時候可以直接用^,然後mod的話,就是ans(x)一直減h(x)*x直到ans(x)<h(x),剩下的ans(x)就是答案。

代碼:

#include <iostream>
#include <cstdio>
#include <stdlib.h>
#include <climits>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <algorithm>
#define esp 1e-6
#define inf 0x0f0f0f0f
#define LL long long
//#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;
int cmp(int a[],int b[],int l1,int l2)
{
    int i,j,t;
    t=max(l1,l2);
    for(i=t-1;i>=0;i--)
    {
        if(a[i]<b[i])
            return -1;
        if(a[i]>b[i])
            return 1;
    }
    return 1;
}//判斷ans(x)是否>h(x)
int main()
{
    int i,j,t;
    int num1[2005],num2[2005],num3[2005],ans[4005];
    int l1,l2,l3,la;
    scanf("%d",&t);
    while(t--)
    {
        memset(ans,0,sizeof(ans));
        scanf("%d",&l1);
        for(i=l1-1;i>=0;i--)
            scanf("%d",&num1[i]);
        scanf("%d",&l2);
        for(i=l2-1;i>=0;i--)
            scanf("%d",&num2[i]);
        scanf("%d",&l3);
        for(i=l3-1;i>=0;i--)
            scanf("%d",&num3[i]);
        for(i=0;i<l1;i++)
            for(j=0;j<l2;j++)
            ans[i+j]^=(num1[i]&num2[j]);//兩者相乘
        la=l1+l2-1;
        while(cmp(ans,num3,la,l3)>0)
        {
            int d=la-l3;
            for(i=0;i<la;i++)
                ans[i+d]^=num3[i];
            while(la&&ans[la-1]==0)
                la--;
        }//更新la的長度
        if(la==0)
            la=1;
        printf("%d ",la);
        for(i=la-1;i>=0;i--)
        {
            if(i!=la-1)
                printf(" ");
            printf("%d",ans[i]);
        }
        printf("\n");
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章