哪位大佬帮我看看高精度分解质因数的代码
  • 板块学术版
  • 楼主FoxWasDead
  • 当前回复3
  • 已保存回复3
  • 发布时间2022/1/16 19:19
  • 上次更新2023/10/28 12:12:44
查看原帖
哪位大佬帮我看看高精度分解质因数的代码
326893
FoxWasDead楼主2022/1/16 19:19
#include<bits/stdc++.h>
#define MAXN 4000
using namespace std;
struct BIGNUM   //高精度以及重置运算符
{
	int len,s[MAXN];
	BIGNUM()
	{
		len=1;
		memset(s,0,sizeof(s));
	}
	bool operator < (const BIGNUM &x) const
	{
		if(len!=x.len) return len<x.len;
		for(int i=len-1;i>=0;i--)
			if(s[i]!=x.s[i]) return s[i]<x.s[i];
		return false;
	}
	bool operator > (const BIGNUM &x) const
	{
		return x<*this;
	}
	bool operator <= (const BIGNUM &x) const
	{
		return !(x<*this);
	}
	bool operator >= (const BIGNUM &x) const
	{
		return !(*this<x);
	}
	bool operator == (const BIGNUM &x) const
	{
		return !(x<*this||*this<x);
	}
	bool operator != (const BIGNUM &x) const
	{
		return x<*this||*this<x;
	}
	BIGNUM operator = (const char* num)
	{
		len=strlen(num);
		for(int i=0;i<len;i++)
			s[i]=num[len-i-1]-'0';
		return *this;
	}
	BIGNUM operator = (const int num)
	{
		char a[MAXN];
		sprintf(a,"%d",num);
		*this=a;
		return *this;
	}
	BIGNUM (int num)
	{
		*this=num;
	}
	BIGNUM (const char* num)
	{
		*this=num;
	}
	BIGNUM operator + (const BIGNUM &a)
	{
		BIGNUM c;
		c.len=max(len,a.len)+1;
		for(int i=0,x=0;i<c.len;i++)
		{
			c.s[i]=s[i]+a.s[i]+x;
			x=c.s[i]/10;
			c.s[i]=c.s[i]%10;
		}
		if(c.s[c.len-1]==0) c.len--;
		return c;
	}
	BIGNUM operator += (const BIGNUM &a)
	{
		*this=*this+a;
		return *this;
	}
	BIGNUM operator * (const BIGNUM &a)
	{
		BIGNUM c;
		for(BIGNUM i=0;i<*this;i+=1)
			c+=a;
		return c;
	}
	BIGNUM operator *= (const BIGNUM &a)
	{
		*this=*this*a;
		return *this;
	}
	BIGNUM operator - (const BIGNUM &a)
	{
		BIGNUM c;
		c.len=max(len,a.len);
		for(int i=0;i<c.len; i++)
		{
			if(s[i]<a.s[i])
			{
				s[i]+=10;
				s[i+1]--;
			}
			c.s[i]=s[i]-a.s[i];
		}
		while(c.s[c.len-1]==0) c.len--;
		return c;
	}
	BIGNUM operator -= (const BIGNUM &a)
	{
		*this=*this-a;
		return *this;
	}
	BIGNUM operator / (const BIGNUM &a)
	{
		BIGNUM c;
		c.len=max(len,a.len);
		while(*this>=a)
		{
			c+=1;
			*this-=a;
		}
		while(c.s[c.len-1]==0) c.len--;
		return c;
	}
	BIGNUM operator /= (const BIGNUM &a)
	{
		*this=*this/a;
		return *this;
	}
	BIGNUM operator % (const BIGNUM &a)
	{
		if(*this==a) return 0;
		while(*this>a)
		{
			*this-=a;
			if(*this==a) return 0;
		}
		while(s[len-1]==0) len--;
		return *this;
	}
};
ostream& operator << (ostream &out,const BIGNUM& x)   //重置 <<
{
	for(int i=x.len-1;i>=0;i--)
		cout<<x.s[i];
	return out;
}
istream& operator >> (istream &in,BIGNUM &x)   // 重置 >>
{
	char num[MAXN];
	in>>num;
	x=num;
	return in;
}
BIGNUM a;
void fj(BIGNUM x)
{
	while(x>1)
	{
		for(BIGNUM i=2;i<=x;i+=1)
			if(x%i==0)
			{
				x/=i;
				cout<<i<<" ";
				break;
			}
	}
}
int main()
{
	cin>>a;
	fj(a);
	return 0;
}
2022/1/16 19:19
加载中...