跳到正文

剪贴板

剪贴板 8gnuzk91

原文
剪贴板信息

剪贴板信息

收录
更新

特别声明

  • 本人的代码被这篇的数据给搞挂了,下面采用的因子库个数为 12。
  • 原本采用的因子个数为 5,所以可能会对博客中给出的数据造成一定的干扰,但大致走向是不变的。
C++
#include<fstream>
#include<iostream>
#include<ctime>
#include<cstdlib>
#include<map>
#define auto unsigned long long
#define abs
using namespace std;
const int tl=12;
const auto p[]={2,3,5,7,11,13,17,19,23,29,31,37};
auto product(auto x,auto y,auto mod)
{
	return (__int128)x*y%mod;
}
auto gcd(auto a,auto b)
{
	return (b==0)?a:gcd(b,a%b);
}/*
auto gcd(auto a,auto b)//所谓的快速gcd 实测一点都不快
{
	if(!a||!b)return a|b;//这个特判是有用的
	int k=__builtin_ctzll(a|b);//作用是取某个数最后一位0的个数
	b>>=__builtin_ctzll(b);
	while(a)
	{
		a>>=__builtin_ctzll(a);
		if(a<b)swap(a,b);
		a-=b;
	}
	return (b<<k);
}*/
auto power(auto x,auto a,auto mod)//快速幂的另一种写法
{
	auto res;
	for(res=1;a!=0;a>>=1)
	{
		if((a&1)!=0)res=product(res,x,mod);
		x=product(x,x,mod);
	}
	return res;
}
bool Miller_Rabin(auto x)
{
	if(x==1)return 0;//特判
	for(int i=0;i<tl;i++)
	{
		if(x==p[i])return 1;//显然
		bool ok=0;
		auto v=x-1;
		while((v&1)==0)v>>=1;
		auto y=power(p[i],v,x);
		if(y==1)ok=1;
		else
		{
			while(v<x-1)
			{
				if(y==x-1)
				{
					ok=1;
					break;
				}
				v<<=1;
				y=product(y,y,x);
			}
		}
		if(!ok)return 0;
	}
	return 1;
}
auto big_rand()//[0,2^45)
{
	auto x=rand();
	for(int i=0;i<3;++i)
	x=(x<<15|rand());
	return rand();
}
auto seed;
auto f(auto a,auto mod)
{
	return (product(a,a,mod)+seed)%mod;
}
auto floyd(auto n) //绕环
{
	seed=big_rand()%n;
//	cout<<seed;
	auto fast,slow,res;
	fast=slow=big_rand()%n;
	fast=f(fast,n);//注意一开始不要先跳两步
	while(fast!=slow)
	{
		res=gcd(fast-slow+n,n);
	//	cout<<fast<<' '<<slow<<' '<<(fast-slow+n)%n<<endl;
		if(res!=1)return res;
		fast=f(f(fast,n),n);
		slow=f(slow,n);
	}
//	for(;;);
	return 1;
}
void Pollard_rho(auto n,map<auto,int>&fat)
{
	if(n==1)return;
//	cout<<n<<endl;
	if(Miller_Rabin(n))
	{
		fat[n]++;
		return;
	}
	auto k=1;
	while(k==1)k=floyd(n);
//	cout<<n<<'!'<<k<<endl;
	Pollard_rho(k,fat);
	Pollard_rho(n/k,fat);
}
int main()
{
	srand(time(NULL));
	auto n;
	map<auto,int>fat;
	cin>>n;
	Pollard_rho(n,fat);
	for(map<auto,int>::iterator it=fat.begin();it!=fat.end();it++)
	cout<<it->first<<' '<<it->second<<endl;
	return 0;
}