poj 1811 Prime Test(拉宾米勒测试+大数分解)
2015-04-09 22:57
375 查看
Language: Default Prime Test
Given a big integer number, you are required to find out whether it's a prime number. Input The first line contains the number of test cases T (1 <= T <= 20 ), then the following T lines each contains an integer number N (2 <= N < 254). Output For each test case, if N is a prime number, output a line containing the word "Prime", otherwise, output a line containing the smallest prime factor of N. Sample Input 2 5 10 Sample Output Prime 2 |
由于 给出的数可能会很大 所以 不能用普通的方法 在这里用到的就是拉宾米勒测试+pollard_rho
#include <cstdio> #include <iostream> #include <cstring> #include <cmath> #include <algorithm> #include <string.h> #include <string> #include <vector> #include <queue> #include <ctime> #include <cstdlib> #define MEM(a,x) memset(a,x,sizeof a) #define eps 1e-8 #define MOD 10009 #define MAXN 1010 #define MAXM 100010 #define INF 99999999 #define ll long long #define bug cout<<"here"<<endl #define fread freopen("ceshi.txt","r",stdin) #define fwrite freopen("out.txt","w",stdout) #define MAX ((ll)1<<61) #define Times 10 #define C 201 using namespace std; int Read() { char ch; int a = 0; while((ch = getchar()) == ' ' | ch == '\n'); a += ch - '0'; while((ch = getchar()) != ' ' && ch != '\n') { a *= 10; a += ch - '0'; } return a; } void Print(int a) //Êä³öÍâ¹Ò { if(a>9) Print(a/10); putchar(a%10+'0'); } ll mini; int cnt; ll pri[MAXN]; ll gcd(ll a,ll b) { if(b==0) return a; return gcd(b,a%b); } ll random(ll n) { return (ll)((double)rand()/RAND_MAX*n+0.5); } ll mult(ll a,ll b,ll m)//a*b%m { ll ret=0; while(b>0) { if(b&1) ret=(ret+a)%m; b>>=1; a=(a<<1)%m; } return ret; } ll quick_mod(ll a,ll b,ll m) { ll ans=1; a%=m; while(b) { if(b&1) { ans=mult(ans,a,m); b--; } b/=2; a=mult(a,a,m); } return ans; } bool Witness(ll a,ll n) { ll m=n-1; int j=0; while(!(m&1)) { j++; m>>=1; } ll x=quick_mod(a,m,n); if(x==1||x==n-1) return 0; while(j--) { x=x*x%n; if(x==n-1) return 0; } return 1; } bool miller_rabin(ll n) { // cout<<"n "<<n<<endl; if(n<2) return 0; if(n==2) return 1; if(!(n&1)) return 0; for(int i=1;i<=Times;i++) { ll a=random(n-2)+1; if(Witness(a,n)) return 0; } return 1; } ll pollard_rho(ll n,int c) { ll x,y,d,i=1,k=2; x=random(n-1)+1; y=x; while(1) { i++; x=(mult(x,x,n)+c)%n; d=gcd(y-x,n); if(1<d&&d<n) return d; if(y==x) return n; if(i==k) { y=x; k<<=1; } } } void find(ll n,int k) { if(n==1) return; if(miller_rabin(n)) { pri[++cnt]=n; if(n<mini) mini=n; return; } ll p=n; while(p>=n) p=pollard_rho(p,k--); find(p,k); find(n/p,k); } int main() { //fread; int tc; scanf("%d",&tc); while(tc--) { ll n; scanf("%lld",&n); mini=MAX; cnt=0; if(miller_rabin(n)) puts("Prime"); else { find(n,C); printf("%lld\n",mini); } } return 0; }
相关文章推荐
- POJ 1811Prime Test(米勒拉宾素数测试)
- poj 1811 Prime Test【 随机素数测试与大数分解】
- POJ 1811 Prime Test【素数判定与大数分解】
- POJ1811 Prime Test(miller素数判断&&pollar_rho大数分解)
- POJ 1811 Prime Test(素数测试和分解)
- poj 2429 GCD & LCM Inverse(拉宾米勒测试+大数分解+dfs)
- Poj 1811 Prime Test 素数测试 Miller-Rabin 与 整数的因子分解 Pollard rho
- pku 1811 Prime Test 大素数判定和大数分解
- POJ 1811 Prime Test(大素数判断和素因子分解)
- poj1811 + hdu4344 (素数测试及大数分解)
- POJ 1811 Prime Test(大素数判断+大合数素因子分解)
- 数论 - Miller_Rabin素数测试 + pollard_rho算法分解质因数 ---- poj 1811 : Prime Test
- Prime Test POJ - 1811 miller素数判断&pollar_rho大数分解
- POJ 1811 Prime Test【大素数判定】【素因子分解】
- POJ 1811 Prime Test (Pollard rho 大整数分解)
- POJ 1811 Prime Test (大素数判断和素因子分解)
- POJ 1811 Prime Test 【快速判质+因子分解】【模板】
- POJ 1811 Prime Test(大素数判断和素因子分解)
- POJ1811_Prime Test【Miller Rabin素数测试】【Pollar Rho整数分解】
- [POJ 1811 Prime Test] Miller_Rabin + Pollard_rho 大数质数判断/质因子分解模板