Modular multiplication of polynomials
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 4377   Accepted: 1980

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

你  离  开  了  ,  我  的  世  界  里  只  剩  下  雨  。  。  。

#include <iostream>
#include<string.h>
using namespace std;
int pd(int sum[],int ls,int h[],int lh)
{
if(ls>lh)return 1;
if(ls<lh)return -1;
if(ls==lh)
{
int i;
for(i=ls-1; i>=0; i--)
{
if(sum[i]&&!h[i])return 1;
if(!sum[i]&&h[i])return -1;
}
}
return 0;
}
int main()
{
int n;
cin>>n;
int c;
for(c=1; c<=n; c++)
{
int lf,lg,lh;
int f[1001],g[1001],h[1001];
int i;
cin>>lf;
for(i=lf-1; i>=0; i--)
cin>>f[i];
cin>>lg;
for(i=lg-1; i>=0; i--)
cin>>g[i];
cin>>lh;
for(i=lh-1; i>=0; i--)
cin>>h[i];
int sum[2001];
memset(sum,0,sizeof(sum));
int j;
for(i=0; i<lf; i++)
for(j=0; j<lg; j++)
sum[i+j]=sum[i+j]^(f[i]&g[j]);
int ls;
ls=lf+lg-1;
while(pd(sum,ls,h,lh)>=0)
{
int d=ls-lh;
for(i=0; i<lh; i++)
sum[i+d]=sum[i+d]^h[i];
while(ls&&!sum[ls-1])
--ls;
}
if(ls==0)ls=1;
cout<<ls<<" ";
for(i=ls-1; i>0; i--)
cout<<sum[i]<<" ";
cout<<sum[0]<<endl;
}
return 0;
}

POJ 1060:Modular multiplication of polynomials的更多相关文章

  1. POJ 1060 Modular multiplication of polynomials(多项式的加减乘除,除法转化成减法来求)

    题意:给出f(x),g(x),h(x)的 (最高次幂+1)的值,以及它们的各项系数,求f(x)*g(x)/h(x)的余数. 这里多项式的系数只有1或0,因为题目要求:这里多项式的加减法是将系数相加/减 ...

  2. POJ1060 Modular multiplication of polynomials

    题目来源:http://poj.org/problem?id=1060 题目大意: 考虑系数为0和1的多项式.两个多项式的加法可以通过把相应次数项的系数相加而实现.但此处我们用模2加法来计算系数之和. ...

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

    Modular multiplication of polynomials Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 3 ...

  4. UVALive 2323 Modular Multiplication of Polynomials(模拟)

    这是一个相对简单的模拟,因为运算规则已经告诉了我们,并且比较简单,不要被吓到…… 思路:多项式除以另外一个多项式,如果能除,那么他的最高次一定被降低了,如果最高次不能被降低,那说明已经无法被除,就是题 ...

  5. Lintcode: Hash Function && Summary: Modular Multiplication, Addition, Power && Summary: 长整形long

    In data structure Hash, hash function is used to convert a string(or any other type) into an integer ...

  6. poj 1060

    http://poj.org/problem?id=1060 题意:多项式的运算的题目,不过这个运算有个特点,就是只要是同项的多项式,无论相加还是相减,都为0,给你三个多项式,分别为a,b,c. 要你 ...

  7. POJ 3673 Cow Multiplication

    Cow Multiplication Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13312   Accepted: 93 ...

  8. Poj 3318 Matrix Multiplication( 矩阵压缩)

    Matrix Multiplication Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 18928   Accepted: ...

  9. poj 2505 A multiplication game(博弈)

    A multiplication game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5622   Accepted: ...

随机推荐

  1. 2019ICPC西安邀请赛(计蒜客复现赛)总结

    开始时因为吃饭晚了一刻钟,然后打开比赛.看了眼榜单A题已经过了二十来个队伍了,宝儿就去做A. 传师说最后一题看题目像最短路,于是我就去看M了,宝儿做完之后也来陪我看.M一开始看到时以为是像   POJ ...

  2. Kvm:通过 libvirt 远程管理虚拟机

    1.通过qemu+ssh方式 2.通过qemu+tcp方式 主控端需要安装相关工具包: #yum groupinstall "Virtualization" #yum instal ...

  3. ruby rspec安装

    在rubymine里新建Rails application

  4. hdu 1698区间延迟更新

    #include<stdio.h> #define N 100100 struct node { int x,y,yanchi; }a[N*4];//注意数组范围 void build(i ...

  5. poj3440--Coin Toss(几何上的概率)

    Coin Toss Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 3946   Accepted: 1076 Descrip ...

  6. cdq分治入门--BZOJ1176: [Balkan2007]Mokia

    对w*w,w<=2000000的矩形,一开始全是0(或一开始全是s),n<=170000个操作,每次操作:矩阵内某点加上一个数,查某一个子矩阵的和,保证修改数<=160000,询问数 ...

  7. Toast自定义

    Toast toast=new Toast(MainActivity.this); toast.setView(getLayoutInflater().inflate(R.layout.toast,n ...

  8. iOS点击cell时,控件背景色消失的解决方法

    同时调用一下两个方法: - (void)setSelected:(BOOL)selected animated:(BOOL)animated { [super setSelected:selected ...

  9. hdu - 1072 Nightmare(bfs)

    http://acm.hdu.edu.cn/showproblem.php?pid=1072 遇到Bomb-Reset-Equipment的时候除了时间恢复之外,必须把这个点做标记不能再走,不然可能造 ...

  10. zoj3988 Prime Set

    思路不难想到二分图求个最大匹配P,若P>=K,则2*K即可,否则应为P*2+min(K-P,未匹配且有度数不为0的顶点个数s).但坑点在于有1的情况,所以如果直接建二分图去跑最大匹配会因为1的影 ...