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. POJ 1664 放苹果( 递推关系 )

    链接:传送门 思路:苹果m个,盘子n个.假设 f ( m , n ) 代表 m 个苹果,n个盘子有 f ( m , n ) 种放法. 根据 n 和 m 的关系可以进一步分析: 特殊的 n = 1 || ...

  2. JavaScript:获取上传图片的base64

    文章来源:http://www.cnblogs.com/hello-tl/p/7661535.html 1.HTML代码 <!DOCTYPE html> <html lang=&qu ...

  3. 新进Linux菜鸟,请多多关照

    早早知晓Linux的大名,一直未研究学习,近来看了kernel一些源代码,在网上搜过很多基础的知识.感觉这个Linux的世界很广大,值得好好深入学习.初生婴儿,呱呱落地,必先躺若干日后能坐,在学爬,进 ...

  4. C语言学习6

    int i; 定义整形变量i int *p;  p为指向整型数据的指针变量 int a[n]: 定义整形数组a,他有n个元素 int *p[n]:   定义指针数组p,它有n个指向整型数据的指针元素组 ...

  5. hive 删除表内容

    TRUNCATE:truncate用于删除所有的行,这个行为在hive元存储删除数据是不可逆的delect:用于删除特定行条件,你可以从给定表中删除所有的行insert overwrite table ...

  6. Qt 安装与配置记录

    一 安装的时候得选一个Qt安装啊!!不要忘了展开这一项,而只安装Qt creator 展开之后会发现有很多版本,为了方便,选自带编译器mingw,就不需要麻烦的配置了 二 打开Qt creator 后 ...

  7. Selenium打开谷歌浏览器提示chromedriver.exe停止运行且浏览器显示请关闭开发者模式

    提示关闭开发者模式,其实是chromedriver.exe与chrome 浏览器版本不一致导致的, 查看浏览器版本方法  在浏览框输入,chrome://version/. 显示是 Google Ch ...

  8. BZOJ 1225: [HNOI2001] 求正整数 高精度+搜索+质数

    题意:给定n求,有n个因子的最小正整数. 题解:水题,zcr都会,我就不说什么了. 因数个数球求法应该知道,将m分解质因数,然后发现 a1^p1*a2^p2....an^pn这样一个式子, (1+p1 ...

  9. [ C语言 ] 迷宫 迷宫生成器 [ 递归与搜索 ]

    [原创]转载请注明出处 [浙江大学 程序设计专题] [地图求解器] 本题目要求输入一个迷宫地图,输出从起点到终点的路线. 基本思路是从起点(Sx,Sy)每次枚举该格子上下左右四个方向,直到走到终点(T ...

  10. 1017 乘积最大 2000年NOIP全国联赛普及组NOIP全国联赛提高组

    1017 乘积最大 2000年NOIP全国联赛普及组NOIP全国联赛提高组   #include <iostream> #include <string> #include & ...