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 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.

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.

The output should contain the polynomial f(x)g(x) modulo h(x), one per line.

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

8 1 1 0 0 0 0 0 1 14 1 1 0 1 1 0 0 1 1 1 0 1 0 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^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
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(结果)
1 0 0 0 0 0 0 1 1(后取两位补上)
1 1(减)
.......0 0 0 0 1 1 0 0 0 (结果)
 0 0 0 0 0 1(补上位后发现不够长度,所以算完了)
最后要注意最后余数可能是0,所以要对此加以判断。
#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)(转)

时间: 2024-10-07 14:00:25

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

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 coeffici

解题报告-2019.12.16

解题报告-2019.12 题目:6-3[拓展编程题_课后练习3][P215 习题8-三-4] 报数 (20分) 题目详情: 报数游戏是这样的:有n个人围成一圈,按顺序从1到n编好号.从第一个人开始报数,报到m(<n)的人退出圈子:下一个人从1开始报数,报到m的人退出圈子.如此下去,直到留下最后一个人. 本题要求编写函数,给出每个人的退出顺序编号. 函数接口定义:void CountOff( int n, int m, int out[] ); 其中n是初始人数:m是游戏规定的退出位次(保证为小于

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 +

UVALive 2323 Modular Multiplication of Polynomials(模拟)

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

2019模拟赛09场解题报告

目录 2019模拟赛09场解题报告 目录la~~ 题一:瞬间移动 题二:食物订购 题三:马蹄印 题四:景观美化 2019模拟赛09场解题报告 标签(空格分隔): 解题报告 Forever_chen 2019.8.20 目录la~~ 题一:瞬间移动 [题面] 有一天,暮光闪闪突然对如何将一个整数序列a1,a2,...,an排序为一个不下降序列起了兴趣.身为一只年轻独角兽的她,只能进行一种叫做"单元转换"(unit shift)的操作.换句话说,她可以将序列的最后一个元素移动到它的起始位置

poj分类解题报告索引

图论 图论解题报告索引 DFS poj1321 - 棋盘问题 poj1416 - Shredding Company poj2676 - Sudoku poj2488 - A Knight's Journey poj1724 - ROADS(邻接表+DFS) BFS poj3278 - Catch That Cow(空间BFS) poj2251 - Dungeon Master(空间BFS) poj3414 - Pots poj1915 - Knight Moves poj3126 - Prim

NOIP2009解题报告

09年的题总体来说 没有难题,但是每道题除了第一题都要认真的慢慢写才能AC, 第一题: R国和S国正陷入战火之中,双方都互派间谍,潜入对方内部,伺机行动. 历经艰险后,潜伏于S国的R国间谍小C终于摸清了S国军用密码的编码规则:1. S国军方内部欲发送的原信息经过加密后在网络上发送,原信息的内容与加密后所的内容均由大写字母‘A’—‘Z’构成(无空格等其他字母).2. S国对于每个字母规定了对应的“密字”.加密的过程就是将原信息中的所有字母替换为其对应的“密字”.3. 每个字母只对应一个唯一的“密字

解题报告 之 POJ3057 Evacuation

解题报告 之 POJ3057 Evacuation Description Fires can be disastrous, especially when a fire breaks out in a room that is completely filled with people. Rooms usually have a couple of exits and emergency exits, but with everyone rushing out at the same time

hdu 1541 Stars 解题报告

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1541 题目意思:有 N 颗星星,每颗星星都有各自的等级.给出每颗星星的坐标(x, y),它的等级由所有比它低层(或者同层)的或者在它左手边的星星数决定.计算出每个等级(0 ~ n-1)的星星各有多少颗. 我只能说,题目换了一下就不会变通了,泪~~~~ 星星的分布是不是很像树状数组呢~~~没错,就是树状数组题来滴! 按照题目输入,当前星星与后面的星星没有关系.所以只要把 x 之前的横坐标加起来就可以了