POJ 1811Prime Test(米勒拉宾素数测试)

摘要:
GCD:a;}30模板<classT>TLCM{returna/GCD(a,b)*b;}31template<classT>voidSWAP{Tt=a;a=b;b=t;}3233类型定义_ int64LL;34//类型定义长LL;35组分最大值=10005;36组分最大值=110000;37constdoubleeps=1e-14;38constdoublePI=4.0*atan(1.0);3940414243LLn,ans;44英寸;4546474849#defineTimes105051//生成[0,n-1]的随机数52LLrandom53{54return;55}56//,并使用加法模拟避免中间结果超过LL57LLmulti58{59LLans=0;60while61{62if(b&1)63{64b-;65ans=%mod;66}67else68{69b/=2;70a=(2*a)%mod,71}72}73返回;74}7576//快速求幂,也避免了超过LL 77LLPow78{79LLans=1;80而81{82if(b&1)83{84b-;85ans=multi;86}87else88{89b/=2;90a=multi,91}92}93返回的方法;94}9596//miller_Rabin的单程检测返回false,表明它是复合97boolwitness98{99LLd=n-1;100while(!=n-1)104{105t=multi;106d<<=1;107}108返回==n-1 | |d&1;109}110111//miller_Rabin算法,return false表示它是一个复合数,否则它是素数112//返回的素数的错误概率(最大值)为1/113布尔米尔_ rabin114{115if116returntrue;117if(n<2||!见证(a,n))123returnfalse;124}125returntrue;126}127128129//************************************130//pollard_rho算法执行素因子分解131//***********************************************//素因子分解结果133 inttol//返回的素因子数=x);159ifeturnx;160如果{y=x0;k+=k;}161}162}163164//对n 165空findfac 166{167if//素数168{169factor〔tol++〕=n;170ans=MIN;171return;172}173LLp=n;174while175p=Pollard_rho;176findfac;177英尺

直接套用模板,以后接着用

这里还有一个素因子分解的模板

  1 #include <map>
  2 #include <set>
  3 #include <stack>
  4 #include <queue>
  5 #include <cmath>
  6 #include <ctime>
  7 #include <vector>
  8 #include <cstdio>
  9 #include <cctype>
 10 #include <cstring>
 11 #include <cstdlib>
 12 #include <iostream>
 13 //#include <algorithm>
 14 using namespace std;
 15 #define INF 0x3f3f3f3f
 16 #define inf ((LL)1<<40)
 17 #define lson k<<1, L, mid
 18 #define rson k<<1|1, mid+1, R
 19 #define mem0(a) memset(a,0,sizeof(a))
 20 #define mem1(a) memset(a,-1,sizeof(a))
 21 #define mem(a, b) memset(a, b, sizeof(a))
 22 #define FOPENIN(IN) freopen(IN, "r", stdin)
 23 #define FOPENOUT(OUT) freopen(OUT, "w", stdout)
 24 template<class T> T ABS ( T a) { return a >= 0 ? a : -a;   }
 25 template<class T> T CMP_MIN ( T a, T b ) { return a < b;   }
 26 template<class T> T CMP_MAX ( T a, T b ) { return a > b;   }
 27 template<class T> T MAX ( T a, T b ) { return a > b ? a : b; }
 28 template<class T> T MIN ( T a, T b ) { return a < b ? a : b; }
 29 template<class T> T GCD ( T a, T b ) { return b ? GCD ( b, a % b ) : a; }
 30 template<class T> T LCM ( T a, T b ) { return a / GCD ( a, b ) * b;     }
 31 template<class T> void SWAP( T& a, T& b ) { T t = a; a = b;  b = t;     }
 32 
 33 typedef __int64 LL;
 34 //typedef long long LL;
 35 const int MAXN = 10005;
 36 const int MAXM = 110000;
 37 const double eps = 1e-14;
 38 const double PI = 4.0 * atan(1.0);
 39 
 40 
 41 
 42 
 43 LL n, ans;
 44 int t;
 45 
 46 
 47 
 48 
 49 #define Times 10
 50 
 51 //产生[0, n-1]的一个随机数
 52 LL random(LL n)
 53 {
 54     return ((double)rand() / RAND_MAX * n + 0.5);
 55 }
 56 //乘法,采用加法模拟,避免中间结果超出LL
 57 LL multi(LL a,LL b,LL mod)
 58 {
 59     LL ans=0;
 60     while(b)
 61     {
 62         if(b & 1)
 63         {
 64             b--;
 65             ans=(ans+a) % mod;
 66         }
 67         else
 68         {
 69             b/=2;
 70             a=(2*a) % mod;
 71         }
 72     }
 73     return ans;
 74 }
 75 
 76 //快速幂,同样避免超出LL的做法
 77 LL Pow(LL a, LL b, LL mod)
 78 {
 79     LL ans=1;
 80     while(b)
 81     {
 82         if(b&1)
 83         {
 84             b--;
 85             ans=multi(ans,a,mod);
 86         }
 87         else
 88         {
 89             b/=2;
 90             a=multi(a,a,mod);
 91         }
 92     }
 93     return ans;
 94 }
 95 
 96 //miller_rabin的一遍探测,返回false表示是合数
 97 bool witness(LL a,LL n)
 98 {
 99     LL d=n-1;
100     while( !(d&1) )
101         d >>= 1;
102     LL t=Pow(a, d, n);
103     while(d!=n-1 && t!=1 && t!=n-1)
104     {
105         t=multi(t,t,n);
106         d<<=1;
107     }
108     return t==n-1 || d&1;
109 }
110 
111 //miller_rabin算法,返回false表示是合数,否则是素数
112 //返回素数出错的概率(最高)为 1 / (4 ^ times)
113 bool miller_rabin(LL n)
114 {
115     if(n == 2)
116         return true;
117     if( n<2 || !(n&1) )
118         return false;
119     for(int i = 1; i <= Times ; i++ )
120     {
121         LL a = random(n-2) + 1;
122         if( !witness(a, n) )
123             return false;
124     }
125     return true;
126 }
127 
128 
129 //************************************************
130 //pollard_rho 算法进行质因数分解
131 //************************************************
132 LL factor[100];//质因数分解结果(刚返回时是无序的)
133 int tol;//质因数的个数。数组小标从0开始
134 
135 LL gcd(LL a,LL b)
136 {
137     if(a==0)return 1;//???????
138     if(a<0) return gcd(-a,b);
139     while(b)
140     {
141         LL t=a%b;
142         a=b;
143         b=t;
144     }
145     return a;
146 }
147 
148 LL Pollard_rho(LL x, LL c)
149 {
150     LL i=1,k=2;
151     LL x0=rand()%x;
152     LL y=x0;
153     while(1)
154     {
155         i++;
156         x0=(multi(x0, x0, x) + c) % x;
157         LL d=gcd(y-x0,x);
158         if(d!=1&&d!=x) return d;
159         if(y==x0) return x;
160         if(i==k){y=x0;k+=k;}
161     }
162 }
163 
164 //对n进行素因子分解
165 void findfac(LL n)
166 {
167     if(miller_rabin(n))//素数
168     {
169         factor[tol++]=n;
170         ans = MIN(ans, n);
171         return;
172     }
173     LL p=n;
174     while(p>=n)
175         p=Pollard_rho(p, rand()%(n-1)+1);
176     findfac(p);
177     findfac(n/p);
178 }
179 
180 
181 
182 int main()
183 {
184     //FOPENIN("in.txt");
185     while(~scanf("%d", &t))while(t--)
186     {
187         ans = inf;
188         scanf("%I64d", &n);
189         findfac(n);
190         if(miller_rabin(n))
191             printf("Prime
");
192         else printf("%I64d
", ans);
193     }
194     return 0;
195 }

免责声明:文章转载自《POJ 1811Prime Test(米勒拉宾素数测试)》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇ATL中使用CString【JUnit】junit4的几个assert方法下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

linux中给数据加上行号

1、测试数据 [root@PC3 test]# cat b.txt e t 3 d g 2 k 8 p m 9 p 5 7 8 m i o e t d 2、awk加行号 [root@PC3 test]# awk '{print NR,$p}' b.txt 1 e t 3 2 d g 2 3 k 8 p 4 m 9 p 5 5 7 8 6 m i o 7 e...

tensorflow安装: win10 + RTX2060 + tensorflow1.15.0+ cuda10.0 + VScode

引言: 之前用的tensorflow 1.10版本,发现在训练CNN的时候会自动中止,最后定位到加入卷积层就会导致训练崩溃/中止,只用全连接层却能正常训练。重装一天后无果,干脆全部升级使用tensorflow1.15: 改用WIN10+python3.7+tensorflow1.15.0+CUDA10.0(+cudnn7.6.5)+VScode 顺便记录下...

Web自动化测试之playwright:概述

playwright是由微软开发的Web UI自动化测试工具, 支持Node.js、Python、C# 和 Java语言,本文将介绍playwright的特性以及它的简单使用。 目录 playwright特性 安装 命令行工具 脚本录制 打开网页 截图 同步和异步API 浏览器 浏览器上下文 多页面 断言 playwright特性 play...

go mod

golang 终于出官方版本管理机制,名为 go modules 初体验 使用前: # 先升级 golang 到 1.11 版本,然后 export GO111MODULE=on 在项目github.com/humboldt-xie/test-mod下,通过go mod init go mod init 然后会在当前项目目录下出现 go.mod 文件,内容...

linux_流处理_sed

1. Sed简介    sed 是 一种在线编辑器,它一次处理一行内容。处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”(pattern space),接着用sed命令处 理缓冲区中的内容,处理完成后,把缓冲区的内容送往屏幕。接着处理下一行,这样不断重复,直到文件末尾。文件内容并没有 改变,除非你使用重定向存储输 出。Sed主要用来自动编辑一个...

unity shader 变种(多重编译 multi_compile)

一、定义 在unity中我们可以通过使用#pragma multi_compile或#pragma shader_feature指令来为shader创建多个稍微有点区别的shader变体。这个Shader被称为宏着色器(mega shader)或者超着色器(uber shader)。实现原理:根据不同的情况,使用不同的预处理器指令,来多次编译Shader代...