码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 1060 Modular multiplication of polynomials 逻辑运算

时间:2015-08-11 12:20:47      阅读:133      评论:0      收藏:0      [点我收藏+]

标签:poj   c++   iostream   

Modular multiplication of polynomials
Time Limit: 1000MS   Memory Limit: 10000K
Total 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 

Source

AC代码:
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#define maxn 1010
using namespace std;
int compare(int a[],int la,int b[],int lb){
    if(la>lb)return 1;
    if(la<lb)return -1;
    for(int i=la-1;i>=0;--i){
        if(a[i]&&!b[i])return 1;
        else if(!a[i]&&b[i])return -1;
    }
    return 0;
}
int main(){
    int f[maxn],g[maxn],h[maxn],s[2*maxn];
    int lf,lg,lh,ls;
    int n,i,j;
    scanf("%d",&n);
    while(n--){
        scanf("%d",&lf);
        for(i=lf-1;i>=0;--i)scanf("%d",&f[i]);
        scanf("%d",&lg);
        for(i=lg-1;i>=0;--i)scanf("%d",&g[i]);
        scanf("%d",&lh);
        for(i=lh-1;i>=0;--i)scanf("%d",&h[i]);
        ls=lf+lg-1;
        for(i=0;i<ls;++i)s[i]=0;
        for(i=0;i<lf;++i){
            for(j=0;j<lg;++j){
                s[i+j]^=(f[i]&g[j]);
            }
        }
        while(compare(s,ls,h,lh)>=0){
            int d=ls-lh;
            for(i=0;i<lh;++i){
                s[i+d]^=h[i];
            }
            while(ls&&!s[ls-1]){
                --ls;
            }
        }
        if(ls==0)ls=1;
        cout<<ls<<' ';
        for(i=ls-1;i>0;--i){
            cout<<s[i]<<' ';
        }
        cout<<s[0]<<'\12';
    }
    return 0;
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

POJ 1060 Modular multiplication of polynomials 逻辑运算

标签:poj   c++   iostream   

原文地址:http://blog.csdn.net/zp___waj/article/details/47417085

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!