POJ 1060 Modular multiplication of polynomials 逻辑运算

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

Taejon 2001

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;
}

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

时间: 2024-10-05 20:39:29

POJ 1060 Modular multiplication of polynomials 逻辑运算的相关文章

poj 1060 Modular multiplication of polynomials

方法(无证明,lz弱渣请谅解): 以样例来讲:(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 . (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 . 令a=x^13 + x^11 + x^9 + x^8 + x^6 + x^5 +

POJ1060 Modular multiplication of polynomials解题报告 (2011-12-09 20:27:53)(转)

Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 3239   Accepted: 1459 Consider polynomials whose coefficients are 0 and 1. Addition of two polynomials is achieved by 'adding' the coefficients for the corresponding powers in the polynomia

UVALive 2323 Modular Multiplication of Polynomials(模拟)

这是一个相对简单的模拟,因为运算规则已经告诉了我们,并且比较简单,不要被吓到…… 思路:多项式除以另外一个多项式,如果能除,那么他的最高次一定被降低了,如果最高次不能被降低,那说明已经无法被除,就是题目要求输出的膜了,降低最高次的方法很简单,只要被除式的最高次 >= 除式的最高次,就将除式的最高次升高到与被除式一样高,然后让被除式减去它,直到不满足上述关系为止. 代码如下: #include<cstdio> #include<algorithm> #include<io

POJ 3318 Matrix Multiplication(随机化算法)

给你三个矩阵A,B,C.让你判断A*B是否等于C. 随机一组数据,然后判断乘以A,B之后是否与乘C之后相等. 很扯淡的啊,感觉这种算法不严谨啊... Matrix Multiplication Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 16255   Accepted: 3515 Description You are given three n × n matrices A, B and C. Does the e

poj 3318 Matrix Multiplication

http://poj.org/problem?id=3318 矩阵A*矩阵B是否等于矩阵C 1 #include <cstdio> 2 #include <cstring> 3 #include <time.h> 4 #include <algorithm> 5 #define maxn 1010 6 using namespace std; 7 8 int a[maxn][maxn],b[maxn][maxn],c[maxn][maxn],d[maxn];

[求PN点] poj 2505 A multiplication game

题目链接: http://poj.org/problem?id=2505 A multiplication game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5098   Accepted: 2573 Description Stan and Ollie play the game of multiplication by multiplying an integer p by one of the numbers

Lintcode: Hash Function &amp;&amp; Summary: Modular Multiplication, Addition, Power &amp;&amp; Summary: 长整形long

In data structure Hash, hash function is used to convert a string(or any other type) into an integer smaller than hash size and bigger or equal to zero. The objective of designing a hash function is to "hash" the key as unreasonable as possible.

POJ 3318 Matrix Multiplication(矩阵乘法)

题目链接 题意 : 给你三个n维矩阵,让你判断A*B是否等于C. 思路 :优化将二维转化成一维的.随机生成一个一维向量d,使得A*(B*d)=C*d,多次生成多次测试即可使错误概率大大减小. 1 //3318 2 #include <stdio.h> 3 #include <string.h> 4 #include <time.h> 5 #include <stdlib.h> 6 #include <iostream> 7 8 using nam

[ACM] POJ 3318 Matrix Multiplication (随机化算法)

Matrix Multiplication Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 16118   Accepted: 3485 Description You are given three n × n matrices A, B and C. Does the equation A × B = C hold true? Input The first line of input contains a posit