POJ 1060:Modular multiplication of polynomials
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 4377 | Accepted: 1980 |
Description
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
above.
Output
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
你 离 开 了 , 我 的 世 界 里 只 剩 下 雨 。 。 。
#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的更多相关文章
- POJ 1060 Modular multiplication of polynomials(多项式的加减乘除,除法转化成减法来求)
题意:给出f(x),g(x),h(x)的 (最高次幂+1)的值,以及它们的各项系数,求f(x)*g(x)/h(x)的余数. 这里多项式的系数只有1或0,因为题目要求:这里多项式的加减法是将系数相加/减 ...
- POJ1060 Modular multiplication of polynomials
题目来源:http://poj.org/problem?id=1060 题目大意: 考虑系数为0和1的多项式.两个多项式的加法可以通过把相应次数项的系数相加而实现.但此处我们用模2加法来计算系数之和. ...
- POJ1060 Modular multiplication of polynomials解题报告 (2011-12-09 20:27:53)
Modular multiplication of polynomials Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 3 ...
- UVALive 2323 Modular Multiplication of Polynomials(模拟)
这是一个相对简单的模拟,因为运算规则已经告诉了我们,并且比较简单,不要被吓到…… 思路:多项式除以另外一个多项式,如果能除,那么他的最高次一定被降低了,如果最高次不能被降低,那说明已经无法被除,就是题 ...
- 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 ...
- poj 1060
http://poj.org/problem?id=1060 题意:多项式的运算的题目,不过这个运算有个特点,就是只要是同项的多项式,无论相加还是相减,都为0,给你三个多项式,分别为a,b,c. 要你 ...
- POJ 3673 Cow Multiplication
Cow Multiplication Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13312 Accepted: 93 ...
- Poj 3318 Matrix Multiplication( 矩阵压缩)
Matrix Multiplication Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 18928 Accepted: ...
- poj 2505 A multiplication game(博弈)
A multiplication game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5622 Accepted: ...
随机推荐
- javascript事件委托和jquery事件委托
元旦过后,新年第一篇. 初衷:很多的面试都会涉及到事件委托,前前后后也看过好多博文,写的都很不错,写的各有千秋,自己思前想后,为了以后自己的查看,也同时为现在找工作的前端小伙伴提供一个看似更全方位的解 ...
- Linux系统下设置vi编辑器,tab键为4
1.cd ~ 2.vi .exrc 3.set tabstop=4(保存并退出)即可
- javamail实现注册激活邮件
http://www.jb51.net/article/111926.htm https://www.cnblogs.com/ganchuanpu/archive/2016/11/29/6115691 ...
- UVA 10652 凸包问题
#include <cstdio> #include <cstring> #include <algorithm> #include <cmath> # ...
- POJ1422 Air Raid
Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 8006 Accepted: 4803 Description Consi ...
- JSP内置对象和EL内置对象
JSP共有九大内置对象: (1) HttpSession类的session对象作用:主要用于来分别保存每个用户信息,与请求关联的会话: 会话状态维持是Web应用开发者必须面对的问题. ...
- JSP的Cookie处理
以下内容引用自http://wiki.jikexueyuan.com/project/jsp/Cookies-handling.html: Cookies是存储在客户端计算机的文本文件,保存各种跟踪目 ...
- mybatis xml标签,批量插入
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-/ ...
- jmeter的master-slave模式
要求: 1.相同的jmeter版本 2.最好相同的java版本 jmeter可以通过master-slave的方式实现更大的并发,但是作为master的机器将会消耗更多的资源,因为所有的slave的压 ...
- zabbix学习系列之基础概念
触发器 概念 "监控项"仅负责收集数据,而通常收集数据的目的还包括在某指标对应的数据超出合理范围时给相关人员发送警告信息,"触发器"正式英语为监控项所收集的数据 ...