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 + x^4 + x^3 + 1 ,b=x^8 + x^4 + x^3 + x + 1;

求a/b的余数,首先b*x^(13-8),由于系数为0或1,所以a=   b*(x^(13-8))异或a=x^11+x^4+x^3+1

11>8重复上述步骤:a= b*(x^(11-8))异或 a=x^7+x^6+x^5+1

7<8得结构,因此(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^7+x^6+x^5+1

主要就这个不好做,其他都好写

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;
const int maxn=2010;
//f,g,h存储的是多项式的系数,sum存储的是f*g的系数以及最后余数的系数
int f[maxn],g[maxn],h[maxn],sum[maxn];
int lf,lg,lh,ls;//分别为f,g,h,sum的最高次幂

//比较sum表示的多项式与h表示的多项式的大小
int compare() {
    if(ls<lh)
        return -1;
    if(ls>lh)
        return 1;
    for(int i=ls-1; i>=0; i--) {
        if(sum[i]==h[i])
            continue;
        if(sum[i]>h[i])
            return 1;
        if(sum[i]<h[i])
            return -1;
    }
    return 0;
}
int main() {
    int t,d;
    scanf("%d",&t);
    while(t--) {
        memset(h,0,sizeof(h));
        memset(sum,0,sizeof(sum));
        //将f多项式的信息存入f数组
        scanf("%d",&d);
        lf=d-1;
        for(int j=lf; j>=0; j--) {
            scanf("%d",&f[j]);
        }
        //将g多项式的信息存入g数组
        scanf("%d",&d);
        lg=d-1;
        for(int j=lg; j>=0; j--) {
            scanf("%d",&g[j]);
        }
        //将h多项式的信息存入h数组
        scanf("%d",&d);
        lh=d-1;
        for(int j=lh; j>=0; j--) {
            scanf("%d",&h[j]);
        }
        //计算f*g的多项式
        ls=lf+lg;
        for(int i=lf; i>=0; i--) {
            for(int j=lg; j>=0; j--) {
                sum[i+j]=sum[i+j]^(f[i]&g[j]);
            }
        }
        /*
          关键是怎么求余数,这里是先判断sum多项式是否大于h多项式,
          若大于,则sum减一次h,减去后的信息存入sum中。
          再继续判断,直到sum小于h,则此时的sum为余数。
          总之,就是把除法改成较简单的减法操作。
        */
        while(compare()>=0) {
            d=ls-lh;
            for(int i=ls; i-d>=0; i--) {
                sum[i]=sum[i]^h[i-d];
            }
            while(ls && !sum[ls])
                ls--;

        }

        printf("%d",ls+1);
        for(int i=ls; i>=0; i--) {
            printf(" %d",sum[i]);
        }
        printf("\n");
    }
    return 0;
}

参考http://www.cnblogs.com/chenxiwenruo/p/3332698.html

时间: 2024-12-31 21:14:14

poj 1060 Modular multiplication of polynomials的相关文章

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

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 polynomia

UVALive 2323 Modular Multiplication of Polynomials(模拟)

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

POJ 3318 Matrix Multiplication(随机化算法)

给你三个矩阵A,B,C.让你判断A*B是否等于C. 随机一组数据,然后判断乘以A,B之后是否与乘C之后相等. 很扯淡的啊,感觉这种算法不严谨啊... Matrix Multiplication Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 16255   Accepted: 3515 Description You are given three n × n matrices A, B and C. Does the e

poj 3318 Matrix Multiplication

http://poj.org/problem?id=3318 矩阵A*矩阵B是否等于矩阵C 1 #include <cstdio> 2 #include <cstring> 3 #include <time.h> 4 #include <algorithm> 5 #define maxn 1010 6 using namespace std; 7 8 int a[maxn][maxn],b[maxn][maxn],c[maxn][maxn],d[maxn];

[求PN点] poj 2505 A multiplication game

题目链接: http://poj.org/problem?id=2505 A multiplication game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5098   Accepted: 2573 Description Stan and Ollie play the game of multiplication by multiplying an integer p by one of the numbers

Lintcode: Hash Function &amp;&amp; Summary: Modular Multiplication, Addition, Power &amp;&amp; Summary: 长整形long

In data structure Hash, hash function is used to convert a string(or any other type) into an integer smaller than hash size and bigger or equal to zero. The objective of designing a hash function is to "hash" the key as unreasonable as possible.

POJ 3318 Matrix Multiplication(矩阵乘法)

题目链接 题意 : 给你三个n维矩阵,让你判断A*B是否等于C. 思路 :优化将二维转化成一维的.随机生成一个一维向量d,使得A*(B*d)=C*d,多次生成多次测试即可使错误概率大大减小. 1 //3318 2 #include <stdio.h> 3 #include <string.h> 4 #include <time.h> 5 #include <stdlib.h> 6 #include <iostream> 7 8 using nam

[ACM] POJ 3318 Matrix Multiplication (随机化算法)

Matrix Multiplication Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 16118   Accepted: 3485 Description You are given three n × n matrices A, B and C. Does the equation A × B = C hold true? Input The first line of input contains a posit