Modular multiplication of polynomials
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 3239   Accepted: 1459

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
题目是说以0 1给3个数,
先按照它的乘法规则把头两个数相乘,再把结果除以第三个数求余。
对于它的加法,就是对应为上0+0=0,0+1=1,1+0=1,1+1=0.
这我们可以用位运算的异或运算符“^”来完成。
减法和加法是相同的……
乘法就是说x^6 X x^7结果x^13.按照一般乘法步骤是先两个数每个位上的数字相乘后加到对应的位置上。
求余我们可以用减法代替。
像例子中的(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
x^13 + x^11 + x^9 + x^8 + x^6 + x^5 + x^4 + x^3 + 1 对应序列是1 0 1 0 1 1 0 1 1 1 1 0 0 1
x^8 + x^4 + x^3 + x + 1 对应序列是1 0 0 0 1 1 0 1 1.下面是求余过程:
1) 1 0 1 0 1 1 0 1 1 1 1 0 0 1
- 1 0 0 0 1 1 0 1 1(减)
-------------
...0 0 1 0 0 0 0 0 0(结果)
....2) 1 0 0 0 0 0 0 1 1(后取两位补上)
....- 1 0 0 0 1 1 0 1 1(减)
.....-------------
.......0 0 0 0 1 1 0 0 0 (结果)
...............1 1 0 0 0 0 0 1(补上位后发现不够长度,所以算完了)
上述所说的补位,其实可以用个变量j指定被减的开始位置,当位置小于某个数时停止。
最后要注意最后余数可能是0,所以要对此加以判断。
还有数组要开到2000……因为两个1000长度的数相乘最长是2000.
#include <iostream>
using namespace std;
int main()
{char f[4][2046];
 int n,i,m,l[3],j;
 cin>>n;
 while (n--)
 {memset(f,0,sizeof(f));
  for (i=0;i<3;i++)
  {cin>>l[i];
   l[i]-=1;
  for (m=l[i];m>=0;m--) {cin>>f[i][m];f[i][m]-=48;}
  }
  for (i=l[0];i>=0;i--)
   for (m=l[1];m>=0;m--)
   f[3][i+m]=f[3][i+m]^(f[0][i]&&f[1][m]);//mutiply
   j=l[0]+l[1];
  while (f[3][j]==0) if (j) j--;else j=-1;
  while (j>=l[2])
  {for (i=j;i>=j-l[2];i--) 
   f[3][i]^=f[2][i-j+l[2]];
   while (f[3][j]==0) if (j) j--;else j=-1;
  }
  if (j>=0)
  {cout<<j+1;
  for (i=j;i>=0;i--) cout<<' '<<f[3][i]+0;}
  else cout<<"1 0";
  cout<<endl;
 }
 return 0;
}

POJ1060 Modular multiplication of polynomials解题报告 (2011-12-09 20:27:53)的更多相关文章

  1. POJ1060 Modular multiplication of polynomials

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

  2. POJ 1060:Modular multiplication of polynomials

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

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

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

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

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

  5. PAT 解题报告 1050. String Subtraction (20)

    1050. String Subtraction (20) Given two strings S1 and S2, S = S1 - S2 is defined to be the remainin ...

  6. leetcode解题报告(12):Maximum Subarray

    描述 Find the contiguous subarray within an array (containing at least one number) which has the large ...

  7. 2011 ACM-ICPC 成都赛区解题报告(转)

    2011 ACM-ICPC 成都赛区解题报告 首先对F题出了陈题表示万分抱歉,我们都没注意到在2009哈尔滨赛区曾出过一模一样的题.其他的话,这套题还是非常不错的,除C之外的9道题都有队伍AC,最终冠 ...

  8. [置顶] 刘汝佳《训练指南》动态规划::Beginner (25题)解题报告汇总

    本文出自   http://blog.csdn.net/shuangde800 刘汝佳<算法竞赛入门经典-训练指南>的动态规划部分的习题Beginner  打开 这个专题一共有25题,刷完 ...

  9. poj分类解题报告索引

    图论 图论解题报告索引 DFS poj1321 - 棋盘问题 poj1416 - Shredding Company poj2676 - Sudoku poj2488 - A Knight's Jou ...

随机推荐

  1. 给定一个字符串s,你可以从中删除一些字符,使得剩下的串是一个回文串。如何删除才能使得回文串最长呢? 输出需要删除的字符个数。

    // ConsoleApplication1.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<iostream> ...

  2. NYOJ 492 King (状态压缩)

    做题感悟:做完这题发现状态压缩有很多须要优化的地方. 解题思路:状态压缩 開始自己用的一般的思路,就和炮兵阵地,郑厂长等题类似的方法做的,開始超时,然后把数组开到了最小的极限就险过.然后看了别人的代码 ...

  3. IE下object元素遮挡div表单

    目前遇到这样的一个问题: 我用ActiveX插件做了一个C#的播放器,要将这个插件放到web浏览器中,然后可以通过前台页面来控制视频的播放,暂停还有回放,这个时候发现object的onclick事件无 ...

  4. SecureCRT 设置字体跟颜色

    SecureCRT 绝佳配色方案, 保护你的眼睛 分类: Linux 软件使用2013-05-17 08:45 24038人阅读 评论(11) 收藏 举报 SecureCRT 绝佳配色方案, 保护你的 ...

  5. 关于iphone自动播放音频和视频问题的解决办法

    大家都知道 做移动端 会遇到音频和视频无法自动播放问题(我也遇到了)于是想办法解决这个问题 我只是找到了在微信中解决的办法(如果谁有在别的浏览器有这个办法  请私聊我 )我是没有发现 document ...

  6. PHP中foreach用法详细讲解

    1.foreach是什么? foreach是PHP的一种语法结构,其实就是一个工具,(工具:就是工作的时候用到的器具),那么在程序开发过程中,为了达到程序效果,就用到了foreach. 2.如何用? ...

  7. PIL+百度aip

    1.PIL模块安装 选择PIL 官方没有支持python3.6的PIL库,所以用pillow代替 http://www.lfd.uci.edu/~gohlke/pythonlibs/#pillow 链 ...

  8. 二、Android应用的界面编程(一)界面编程与视图(View)组件

    Android应用的绝大部分UI组件都放在android.widget包及其子包.android.view包及其子包中,Android应用的所有UI组件都继承了View类.它代表一个空白的矩形区域.V ...

  9. 【BZOJ3331】[BeiJing2013]压力 Tarjan求点双

    [BZOJ3331][BeiJing2013]压力 Description 如今,路由器和交换机构建起了互联网的骨架.处在互联网的骨干位置的核心路由器典型的要处理100Gbit/s的网络流量.他们每天 ...

  10. Nearest Common Ancestors(LCA)

    Description A rooted tree is a well-known data structure in computer science and engineering. An exa ...