Sweety

Practice makes perfect

导航

4.3.2 C - Modular multiplication of polynomials(简单线性表)

Posted on 2014-07-20 09:49  蓝空  阅读(212)  评论(0编辑  收藏  举报

 

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 

 

整体思路:这道题其实没怎么看懂就是简单的从网上找了一下,才知道具体是什么意思,按照意思搬了一下砖

乘法的话就是和一般的一样,不过因为系数是确定的,因此在做的时候可以通过与运算来实现比较快,加法的话不能忘了对2取余。除法的话麻烦,需要用被除数的最高次幂减掉除数的最高次幂,然后再相加,(在此时一定注意,要将修改的除数放在另一个数组中,因为在下一次运算时还要用到这个数组)知道除数的位数大于被除数的位数。

代码:

#include<iostream>
#include<algorithm>
using namespace std;
int main(void)
{
 int m,n,o,j,x,nn,ii;
 cin>>nn;
 for(ii=0;ii<nn;ii++)
 {
  int c[2000]={0},i,d[2000]={0},e[2000]={0};
  cin>>m;         // 第一组个数
  int a[2000],b[2000];
  for(i=0;i<m;i++)
   cin>>a[m-1-i];
  cin>>n;               //第二组个数
  for(i=0;i<n;i++)
   cin>>b[n-i-1];
  for(i=0;i<m;i++)
   for(j=0;j<n;j++)
    c[i+j]=((a[i]&&b[j])+c[i+j])%2;                   ///////////////注意运算的先后顺序,如果不加上()的话就会出错,因为算数运算比关系运算的级别高
   x=m+n-1;                          //前两个相乘后的位数
    // for(j=0;j<x;j++)
     // cout<<j<<' '<<c[j]<<endl;
   cin>>o;                       //第三组个数
   for(i=0;i<o;i++)
    cin>>d[o-1-i];
   int distance=x-o;               //两个相差的级数
   while(o<=x)//当除数的位数比被除数的位数高的时候,继续进行,知道除数的位数比被除数高为止
   {
    memset(e,0,sizeof(int)*1000);
    for(i=o-1;i>=0;i--)                //     (1)
     e[i+distance]=d[i];          //           (2)
    for(i=0;i<x;i++)
     c[i]=(c[i]+e[i])%2;
    for(i=x-1;i>=0;i--)
     if(c[i]==1)
     {
      x=i+1;
      break;
     }
     distance=x-o;                    //(3)
   }
   
   for(i=o-1;i>=0;i--)//输出时控制位数
    if(c[i]!=0)
    {
     x=i+1;
     break;
    }
    cout<<x<<' ';
    for(i=x-1;i>=0;i--)
     cout<<c[i]<<' ';
    cout<<endl;
    
 }
 return 0;
}

 

本题还有个稍微简单的方法就是将被除数和除数都高位对齐存储这样的话就可以直接加减了,简化了上述代码中的(1)(2)(3)这几步了思路也比较简单了,但是弊端是不能再记录商了,这个算法中不能再得到商值了。。。