拉斯维加斯算法对n进行因子分解&&n取值范围2 <= N < 2^54
来源:互联网 发布:虐杀原形2a卡优化 编辑:程序博客网 时间:2024/05/16 10:42
代码:
#include <cstdio>#include <cstdlib>#include <ctime>#include <cstring>#include <iostream>#include<algorithm>const int S=20;using namespace std;typedef long long LL;#define maxn 10000LL factor[maxn];int tot;LL muti_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;}LL pow_mod(LL x,LL n,LL mod){ //返回x^n mod c ,非递归版 if (n==1) return x%mod; int bit[64],k=0; while (n){ bit[k++]=n&1; n>>=1; } LL ret=1; for (k=k-1;k>=0;k--){ ret=muti_mod(ret,ret,mod); if (bit[k]==1) ret=muti_mod(ret,x,mod); } return ret;}bool check(LL a,LL n,LL x,LL t){ //以a为基,n-1=x*2^t,检验n是不是合数 LL ret=pow_mod(a,x,n),last=ret; for (int i=1;i<=t;i++){ ret=muti_mod(ret,ret,n); if (ret==1 && last!=1 && last!=n-1) return 1; last=ret; } if (ret!=1) return 1; return 0;}bool Miller_Rabin(LL n){ LL x=n-1,t=0; while ((x&1)==0) x>>=1,t++; bool flag=1; if (t>=1 && (x&1)==1){ for (int k=0;k<S;k++){ LL a=rand()%(n-1)+1; if (check(a,n,x,t)) {flag=1;break;} flag=0; } } if (!flag || n==2) return 0; return 1;}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,x0=rand()%x,y=x0,k=2; while (1){ i++; x0=(muti_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; } }}void findfac(LL n){ //递归进行质因数分解N if (!Miller_Rabin(n)){ factor[tot++] = n; return; } LL p=n; while (p>=n) p=Pollard_rho(p,rand() % (n-1) +1); findfac(p); findfac(n/p);}int main(){ srand(time(NULL)); int t; scanf("%d",&t); while (t--){ LL n; scanf("%lld",&n); if (!Miller_Rabin(n)) { //printf("Prime\n"); cout<<n<<endl; } else{ tot = 0; findfac(n); sort(factor,factor+tot); for (int i = 0; i < tot; i++) printf("%lld ",factor[i]); printf("\n"); } }return 0;}
法二:
#include <cstdio>#include <cstdlib>#include <ctime>#include <cstring>#include <iostream>#include<algorithm>const int S=20;using namespace std;typedef long long LL;#define maxn 10000LL factor[maxn];int tot;LL muti_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;}LL pow_mod(LL x,LL n,LL mod){ //返回x^n mod c LL ret=1; while(n) { if(n&1) ret=muti_mod(ret,x,mod); x=muti_mod(x,x,mod); n=n>>1; }return ret;}bool check(LL a,LL n,LL x,LL t){ //以a为基,n-1=x*2^t,检验n是不是合数 LL ret=pow_mod(a,x,n),last=ret; for (int i=1;i<=t;i++){ ret=muti_mod(ret,ret,n); if (ret==1 && last!=1 && last!=n-1) return 1;//n是合数 last=ret; } if (ret!=1) return 1; return 0;}bool Miller_Rabin(LL n){ LL x=n-1,t=0; while ((x&1)==0) x>>=1,t++; bool flag=0;//flag是0表示合数,1表示素数 if (t>=1 && (x&1)==1){//当n为偶数时,它一定不是素数(2除外) for (int k=0;k<S;k++){ LL a=rand()%(n-2)+1; if (check(a,n,x,t)) {flag=0;break;} flag=1; } } if (flag || n==2) return 1; return 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,x0=rand()%x,y=x0,k=2; while (1){ i++; x0=(muti_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; } }}void findfac(LL n){ //递归进行质因数分解N if (Miller_Rabin(n)){ factor[tot++] = n; return; } LL p=n; while (p>=n) p=Pollard_rho(p,rand() % (n-1) +1); findfac(p); findfac(n/p);}int main(){ srand(time(NULL)); int t; scanf("%d",&t); while (t--){ LL n; scanf("%lld",&n); if (Miller_Rabin(n)) { //printf("Prime\n"); cout<<n<<endl; } else{ tot = 0; findfac(n); sort(factor,factor+tot); for (int i = 0; i < tot; i++) printf("%lld ",factor[i]); printf("\n"); } }return 0;}
- 拉斯维加斯算法对n进行因子分解&&n取值范围2 <= N < 2^54
- n!素因子分解2
- n! 素因子分解
- n!素因子分解
- n!的素因子分解
- n!的质因子分解
- POJ1401 数学N!因子分解
- n!的素因子分解
- N!的素因子分解
- N!的质因子分解
- 输入90,打印出90=2*3*3*5 对n进行分解质因数
- N后问题(拉斯维加斯算法)
- 拉斯维加斯算法求n皇后问题
- n皇后的拉斯维加斯回溯算法
- n后问题2.0(拉斯维加斯随机算法)
- 拉斯维加斯算法和N皇后问题
- 因数分解 因数分解是十分基本的数学运算,应用广泛。下面的程序对整数n(n>1)进行因数分解。 比如,n=60, 则输出:2 2 3 5。请补充缺失的部分。
- n皇后问题--拉斯维加斯
- 复杂的1秒 图解Google搜索技术
- xml 操作
- Tomcat Spring ActiveMQ MySQL JMX Integration
- 设置虚拟wifi网卡开启无线热点的方法
- js:匿名与闭包函数
- 拉斯维加斯算法对n进行因子分解&&n取值范围2 <= N < 2^54
- 国外vim-python的发布版本
- Java Servlet学习一
- 第十一周任务四多重继承
- 【解决方法】win7 x64 vs中sql语句连接 ora-12154:tns:无法解析指定的连接标识符
- 中文搜索引擎技术揭密
- Android2.3系统的overscroll效果 下滑的时候出现系统的效果
- ACM输入输出技术总结
- 个人博客wordpress搭建之进阶篇