poj 2191 大数素数判定 && 大数素数分解
2017-05-24 16:00
375 查看
再次用到Miller_rabin 和Pollard - rho,
题意: 给出一个梅森数,2^x - 1,;
然后要对x为素数的时候,梅森数不为素数时的数进行素数分解;
思路:打表;
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<algorithm>
#include<map>
#include<string>
using namespace std;
const int maxn = 64 ;
#define INF 0x3f3f3f3f
#define clr(x,y) memset(x,y,sizeof x )
typedef long long ll;
#define eps 10e-10
const ll Mod = 1000000007;
typedef pair<ll, ll> P;
ll Mult_mod (ll a,ll b,ll c) //减法实现比取模速度快
{ //返回(a*b) mod c,a,b,c<2^63
a%=c;
b%=c;
ll ret=0;
while (b)
{
if (b&1)
{
ret+=a;
if (ret>=c) ret-=c;
}
a<<=1;
if (a>=c) a-=c;
b>>=1;
}
return ret;
}
//计算 x^n %c
ll Pow_mod (ll x,ll n,ll mod) //x^n%c
{
if (n==1) return x%mod;
x%=mod;
ll tmp=x;
ll ret=1;
while (n)
{
if (n&1) ret=Mult_mod(ret,tmp,mod);
tmp=Mult_mod(tmp,tmp,mod);
n>>=1;
}
return ret;
}
//以a为基,n-1=x*2^t a^(n-1)=1(mod n) 验证n是不是合数
//一定是合数返回true,不一定返回false
bool Check (ll a,ll n,ll x,ll t)
{
ll ret=Pow_mod(a,x,n);
ll last=ret;
for (ll i=1;i<=t;i++)
{
ret=Mult_mod(ret,ret,n);
if(ret==1&&last!=1&&last!=n-1) return true; //合数
last=ret;
}
if (ret!=1) return true;
return false;
}
// Miller_Rabin()算法素数判定
//是素数返回true.(可能是伪素数,但概率极小)
//合数返回false;
ll S = 20;
bool Miller_Rabin (ll n)
{
if (n<2) return false;
if (n==2) return true;
if ((n&1)==0) return false;//偶数
ll x=n-1;
ll t=0;
while ((x&1)==0) {x>>=1;t++;}
for (ll i=0;i<S;i++)
{
ll a=rand()%(n-1)+1; //rand()需要stdlib.h头文件
if (Check(a,n,x,t))
return false;//合数
}
return true;
}
//************************************************
//pollard_rho 算法进行质因数分解
//************************************************
ll factor[maxn];//质因数分解结果(刚返回时是无序的)
ll tol;//质因数的个数。数组下标从0开始
ll Gcd (ll a,ll b)
{
if (a==0) return 1; //???????
if (a<0) return Gcd(-a,b);
while (b)
{
ll t=a%b;
a=b;
b=t;
}
return a;
}
ll Pollard_rho (ll x,ll c)
{
ll i=1,k=2;
ll x0=rand()%x;
ll y=x0;
while (true)
{
i++;
x0=(Mult_mod(x0,x0,x)+c)%x;
ll d=Gcd(y-x0,x);
if (d!=1 && d!=x) return d;
if (y==x0) return x;
if (i==k) {y=x0;k+=k;}
}
}
//对n进行素因子分解
void Findfac (ll n)
{
if (Miller_Rabin(n)) //素数
{
factor[tol++]=n;
return;
}
ll p=n;
while (p>=n) p=Pollard_rho(p,rand()%(n-1)+1);
Findfac(p);
Findfac(n/p);
}
int len = 0;
ll a[maxn];
ll prime[maxn];
ll ans[maxn][maxn];
bool flag[maxn];
int cnt[maxn];
void Init()
{
a[0] = 1;
for(int i = 1; i < maxn; i ++)
a[i] = a[i - 1] * 2;
bool is_[maxn];
clr(is_,true);
for(int i = 2; i < maxn; i ++)
{
if(is_[i])
{
for(int j = i * 2; j < maxn ; j += i)
is_[j] = false;
prime[len ++] = i;
}
}
clr(flag,true);
for(int i = 0; i < len; i ++)
{
// cout << i << " " << prime[i] << endl;
if(!Miller_Rabin(a[prime[i]] - 1))
{
tol = 0;
// cout << a[prime[i]] - 1 << " ";
flag[i] = false;
Findfac(a[prime[i]] - 1);
// cout << tol << endl;
sort(factor,factor + tol);
cnt[i] = tol;
for(int j = 0; j < tol; j ++)
{
ans[i][j] = factor[j];
}
}
}
}
int main()
{
Init();
int n;
while( ~ scanf("%d",&n))
{
for(int i = 0; i < len; i ++)
{
if(!flag[i] && prime[i] < n)
{
printf("%lld",ans[i][0]);
for(int j = 1; j < cnt[i]; j ++)
{
printf(" * %lld",ans[i][j]);
}
printf(" = %lld = ( 2 ^ %d ) - 1\n",a[prime[i]],prime[i]);
}
}
}
return 0;
}
题意: 给出一个梅森数,2^x - 1,;
然后要对x为素数的时候,梅森数不为素数时的数进行素数分解;
思路:打表;
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<algorithm>
#include<map>
#include<string>
using namespace std;
const int maxn = 64 ;
#define INF 0x3f3f3f3f
#define clr(x,y) memset(x,y,sizeof x )
typedef long long ll;
#define eps 10e-10
const ll Mod = 1000000007;
typedef pair<ll, ll> P;
ll Mult_mod (ll a,ll b,ll c) //减法实现比取模速度快
{ //返回(a*b) mod c,a,b,c<2^63
a%=c;
b%=c;
ll ret=0;
while (b)
{
if (b&1)
{
ret+=a;
if (ret>=c) ret-=c;
}
a<<=1;
if (a>=c) a-=c;
b>>=1;
}
return ret;
}
//计算 x^n %c
ll Pow_mod (ll x,ll n,ll mod) //x^n%c
{
if (n==1) return x%mod;
x%=mod;
ll tmp=x;
ll ret=1;
while (n)
{
if (n&1) ret=Mult_mod(ret,tmp,mod);
tmp=Mult_mod(tmp,tmp,mod);
n>>=1;
}
return ret;
}
//以a为基,n-1=x*2^t a^(n-1)=1(mod n) 验证n是不是合数
//一定是合数返回true,不一定返回false
bool Check (ll a,ll n,ll x,ll t)
{
ll ret=Pow_mod(a,x,n);
ll last=ret;
for (ll i=1;i<=t;i++)
{
ret=Mult_mod(ret,ret,n);
if(ret==1&&last!=1&&last!=n-1) return true; //合数
last=ret;
}
if (ret!=1) return true;
return false;
}
// Miller_Rabin()算法素数判定
//是素数返回true.(可能是伪素数,但概率极小)
//合数返回false;
ll S = 20;
bool Miller_Rabin (ll n)
{
if (n<2) return false;
if (n==2) return true;
if ((n&1)==0) return false;//偶数
ll x=n-1;
ll t=0;
while ((x&1)==0) {x>>=1;t++;}
for (ll i=0;i<S;i++)
{
ll a=rand()%(n-1)+1; //rand()需要stdlib.h头文件
if (Check(a,n,x,t))
return false;//合数
}
return true;
}
//************************************************
//pollard_rho 算法进行质因数分解
//************************************************
ll factor[maxn];//质因数分解结果(刚返回时是无序的)
ll tol;//质因数的个数。数组下标从0开始
ll Gcd (ll a,ll b)
{
if (a==0) return 1; //???????
if (a<0) return Gcd(-a,b);
while (b)
{
ll t=a%b;
a=b;
b=t;
}
return a;
}
ll Pollard_rho (ll x,ll c)
{
ll i=1,k=2;
ll x0=rand()%x;
ll y=x0;
while (true)
{
i++;
x0=(Mult_mod(x0,x0,x)+c)%x;
ll d=Gcd(y-x0,x);
if (d!=1 && d!=x) return d;
if (y==x0) return x;
if (i==k) {y=x0;k+=k;}
}
}
//对n进行素因子分解
void Findfac (ll n)
{
if (Miller_Rabin(n)) //素数
{
factor[tol++]=n;
return;
}
ll p=n;
while (p>=n) p=Pollard_rho(p,rand()%(n-1)+1);
Findfac(p);
Findfac(n/p);
}
int len = 0;
ll a[maxn];
ll prime[maxn];
ll ans[maxn][maxn];
bool flag[maxn];
int cnt[maxn];
void Init()
{
a[0] = 1;
for(int i = 1; i < maxn; i ++)
a[i] = a[i - 1] * 2;
bool is_[maxn];
clr(is_,true);
for(int i = 2; i < maxn; i ++)
{
if(is_[i])
{
for(int j = i * 2; j < maxn ; j += i)
is_[j] = false;
prime[len ++] = i;
}
}
clr(flag,true);
for(int i = 0; i < len; i ++)
{
// cout << i << " " << prime[i] << endl;
if(!Miller_Rabin(a[prime[i]] - 1))
{
tol = 0;
// cout << a[prime[i]] - 1 << " ";
flag[i] = false;
Findfac(a[prime[i]] - 1);
// cout << tol << endl;
sort(factor,factor + tol);
cnt[i] = tol;
for(int j = 0; j < tol; j ++)
{
ans[i][j] = factor[j];
}
}
}
}
int main()
{
Init();
int n;
while( ~ scanf("%d",&n))
{
for(int i = 0; i < len; i ++)
{
if(!flag[i] && prime[i] < n)
{
printf("%lld",ans[i][0]);
for(int j = 1; j < cnt[i]; j ++)
{
printf(" * %lld",ans[i][j]);
}
printf(" = %lld = ( 2 ^ %d ) - 1\n",a[prime[i]],prime[i]);
}
}
}
return 0;
}
相关文章推荐
- 数学#素数判定Miller_Rabin+大数因数分解Pollard_rho算法 POJ 1811&2429
- poj 2429 GCD & LCM Inverse miller_rabin素数判定和pollard_rho因数分解
- 素数判定(Miller_Rabin) & 大数分解(Pollard_Pho)
- POJ 2429 GCD & LCM Inverse(素数判定Miller-Rabin+素因子分解Pollard-rho)
- POJ 1811 Prime Test【素数判定与大数分解】
- POJ 2429 GCD &amp; LCM Inverse(大数分解)
- POJ 2429 GCD & LCM Inverse Pollard_rho大数因子分解
- 【POJ】-3641-Pseudoprime numbers(快速幂,大数素数判定)
- 【世界数学难题】素数判定与大数因子分解问题(下)
- poj 1811 随机素数和大数分解(模板)
- poj 1811 Prime Test(数论:大素数判定-分解)
- (Relax 数论 1.5)POJ 1811 Prime Test(MillRabin模板题+Pollard模板题:判定大素数&&合数分解)
- POJ 3101 素数分解+大数
- poj 1811(大素数模版 miller_rabbin素数判定+pollard_rho分解)
- poj 1811 Prime Test(大素数判定和素因子分解)
- poj 1181(大数判素数 ,分解)
- poj 1811 Prime Test【 随机素数测试与大数分解】
- 素数判定 & 素数筛法 & poj_3292_Semi-prime H-numbers
- poj 2429 GCD & LCM Inverse(拉宾米勒测试+大数分解+dfs)
- 【知识点】大数分解与素数判定 --- 【Miller-rabin算法】【pollard-rho算法】