NTL密码算法开源库——大整数上多项式(ZZX,GF2X)

2021SC@SDUSC

GF(2)域求两多项式的最大公因式

//在GF(2)域中求两多项式的最大公因式
//注意多项式是降幂排列的还是升幂排列的
//b(x) = x^2, a(x) = x^3 + x^2 + 1
//降幂排列表示
//b = [1 0 0];
// a = [1 0 1 1];
//升幂排列表示
b = [ 0 0 1];
a = [1 1 0 1];
while(sum(b) > 0)
t = b;
//注意!conv和deconb函数认为多项式按降幂排列
[q,r] =deconv(a,b);
b =mod(r,2);
ind =find(b,1,‘first’);
b =b(ind:end);
//而gfconv和gfdeconv等函数认为多项式按升幂排列
//例如[0 0 1]代表x^2
[quot,remd] = gfdeconv(a,b);
b = remd;
a = t;
end

扩展欧几里得求两多项式最大公因式

设多项式的系数按照幂次由高到低的顺序存于一维数组中,多项式的最高幂次存于一变量中。
辗转相除法求两多项式去除另一个多项式,然后将所得余式变成除式,原除式变为被除式。如此反复相除,直到余式为零,最后的除式即为最大公因式
代码:(1)

#include<stdio.h>
#define N 10
int main()
{
	float a[N+1],b[N+1],c[N+1];
	int i,j,n,m,k,flag;
	float x,y;
	printf("Please input the highest power of two polynomials(n,m):");
	scanf("%d,%d",&n,&m);
	printf("Please enter the coefficients of the terms of the polynomial(x) in order of power from high to low:\n");
	for(i=ni>=0;i--)
		scanf("%f",&a[i]);
		printf("请按幂次由高到低输入多项式B(x)各项的系数:\n");
		for(j=m;j>=0;j--)
			scanf("%f",&b[j]);
		if(n<m)
		{
			for(i=n;i>=0;i--)
			{
				c[i] = a[i];
				a[i] = b[i];
				b[i] = c[i];
			}
			for(i=n+1;i<=m;i++)
				a[i] = b[i];
			flag = n;n = m;m = flag;
		}
		flag = 1;
		while(flag)
		{
			for(k=n-m;k>=0;k--)
			{
				x = a[k+m]/b[m];
				for(j=m-1;j>=0;j--)
				{
					y = a[k+j] - x * b[j];
					a[k+j] = (y<0.0005 && y>-0.0005)?0.0:y;
				}
			}
			for(i=m;i>=0;i--}
				c[i] = b[i];
			for(flag=0,i=m-1;i>=0;i--)
			{
				if(a[i]!=0)
					flag = 1;
				b[i] = a[i];
			}
			for(i=m;i>=0;i--)
				a[i] = c[i];
			n = m--;
			}
			printf("这两个多项式的公因式是:");
			for(i=n;i>=0;i--)
				if(i!=0)
					printf("%4.1fx^%d+",a[i],i);
				else
					printf("%4.1f\n",a[i]);
				return 0;
			}

代码:(2)

#include <iostream>
#include <string.h>
#include <stdio.h>
#include <math.h>
 
using namespace std;
typedef long long LL;
const double eps = 1e-8;
const int MOD = 999983;
const int N = 55;
 
struct Poly
{
    int n;
    LL a[N];
};
 
Poly p[25];
 
LL gcd(LL a,LL b)
{
    return b? gcd(b,a%b):a;
}
 
Poly delete_zero(Poly x)
{
    int i,j;
    Poly tmp;
    for(i=0;i<x.n && x.a[i] == 0;i++);
    for(j=0;i<x.n;i++,j++) tmp.a[j] = x.a[i];
    tmp.n = j;
    return tmp;
}
 
Poly poly_gcd(Poly x,Poly y)
{
    x = delete_zero(x);
    y = delete_zero(y);
    Poly yy = y,tmp;
    tmp.n = 0;
    int i=0;
    if(x.n == y.n)
    {
        double k = x.a[0] / y.a[0];
        for(i=1;i<x.n;i++)
            if(fabs(x.a[i]*1.0 - k*y.a[i]) > eps) break;
        if(i == x.n) return y;
    }
    LL g = gcd(x.a[0],y.a[0]);
    LL tx = y.a[0] / g;
    LL ty = x.a[0] / g;
    for(i=0;i<x.n;i++)
    {
        x.a[i] *= tx;
        x.a[i] %= MOD;
    }
    for(i=0;i<y.n;i++)
    {
        y.a[i] *= ty;
        y.a[i] %= MOD;
    }
    if(x.n < y.n) swap(x,y);
    for(i=1;i<y.n;i++)
        tmp.a[i-1] = ((x.a[i] - y.a[i])%MOD + MOD)%MOD;
    for(;i<x.n;i++)
        tmp.a[i-1] = x.a[i];
    tmp.n = x.n - 1;
    tmp = delete_zero(tmp);
    if(tmp.n == 0) return yy;
    return poly_gcd(y,tmp);
}

具体数学原理见@回首~阑珊

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

元解~殇怀

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值