首页 > 其他 > 详细

poj 1060 Modular multiplication of polynomials

时间:2015-03-19 23:20:32      阅读:286      评论:0      收藏:0      [点我收藏+]

方法(无证明,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

poj 1060 Modular multiplication of polynomials

原文:http://www.cnblogs.com/Scale-the-heights/p/4351887.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!