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)(转)