poj 2191 大数素数判定 && 大数素数分解
来源:互联网 发布:淘宝里的转化率是什么 编辑:程序博客网 时间:2024/05/21 09:15
再次用到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-10const 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 %cll 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,不一定返回falsebool 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;}
阅读全文
0 0
- poj 2191 大数素数判定 && 大数素数分解
- 大数分解与素数判定
- POJ 3101 素数分解+大数
- 素数判定(Miller_Rabin) & 大数分解(Pollard_Pho)
- 数论之大数分解与素数判定
- fzu1649(大数素数判定)
- 判定一个大数是否素数
- 【POJ】-3641-Pseudoprime numbers(快速幂,大数素数判定)
- poj 1811 Prime Test【 随机素数测试与大数分解】
- poj 1811 随机素数和大数分解(模板)
- 【世界数学难题】素数判定与大数因子分解问题(上)
- 【世界数学难题】素数判定与大数因子分解问题(下)
- 【知识点】大数分解与素数判定 --- 【Miller-rabin算法】【pollard-rho算法】
- 素数判定与大数分解【Miller-rabin算法】【pollard-rho算法】
- 随机素数测试和大数分解
- poj 1811 (素数判定+质因数分解)
- poj 1811解题报告——关于大数的素数鉴定与质因数分解
- 邝斌的ACM模板(随机素数测试和大数分解(POJ 1811) )
- SpringMVC 基础教程 简单入门实例
- 解决内存泄漏问题
- PHP学习笔记——删除数组中的值实例(猴子选大王)
- maven启动web工程
- surfaceiew 和texture view
- poj 2191 大数素数判定 && 大数素数分解
- tomcat日志配置-----自带log(一)
- python
- xcode soundtouch 一次编译armv7 armv7s arm64 i386 i64 库
- LeetCode 39 Combination Sum
- nth-child选择器用于表格
- 四元素与欧拉角之间的转换
- Find them, Catch them POJ
- ssm中mybatis处理数据库(mysql)与pojo属性中类型不一致(string转date)