輸入輸出樣例 輸入 #1復制 6 2 13 134 8897 1234567654321 1000000000000 輸出 #1復制 Prime Prime 67 41 4649 5 這個題目之考察了計算,沒考察分解,我博客里有帶輸出元素的代碼。
#include<bits/stdc++.h>usingnamespace std;typedeflonglong ll;
ll pr;
ll pmod(ll a, ll b, ll p){return(a * b -(ll)((longdouble)a / p * b)* p + p)% p;}//普通的快速乘會T
ll gmod(ll a, ll b, ll p){ll res =1;while(b){if(b &1) res =pmod(res, a, p);a =pmod(a, a, p);b >>=1;}return res;}inline ll gcd(ll a, ll b){//聽說二進制算法特快if(!a)return b;if(!b)return a;int t =__builtin_ctzll(a | b);a >>=__builtin_ctzll(a);do{b >>=__builtin_ctzll(b);if(a > b){ll t = b;b = a, a = t;}b -= a;}while(b);return a << t;}boolMiller_Rabin(ll n){if(n ==46856248255981ll|| n <2)returnfalse;//強偽素數if(n ==2|| n ==3|| n ==7|| n ==61|| n ==24251)returntrue;if(!(n &1)||!(n %3)||!(n %61)||!(n %24251))returnfalse;ll m = n -1, k =0;while(!(m &1))k++, m >>=1;for(int i =1; i <=20;++i)// 20為Miller-Rabin測試的迭代次數{ll a =rand()%(n -1)+1, x =gmod(a, m, n), y;for(int j =1; j <= k;++j){y =pmod(x, x, n);if(y ==1&& x !=1&& x != n -1)return0;x = y;}if(y !=1)return0;}return1;}
ll Pollard_Rho(ll x){ll n =0, m =0, t =1, q =1, c =rand()%(x -1)+1;for(ll k =2;; k <<=1, m = n, q =1){for(ll i =1; i <= k;++i){n =(pmod(n, n, x)+ c)% x;q =pmod(q,abs(m - n), x);}t =gcd(x, q);if(t >1)return t;}}voidfid(ll n){if(n ==1)return;if(Miller_Rabin(n)){pr =max(pr, n);return;}ll p = n;while(p >= n)p =Pollard_Rho(p);fid(p);fid(n / p);}intmain(){int T;ll n;scanf("%d",&T);while(T--){scanf("%lld",&n);pr =0;fid(n);if(pr == n)puts("Prime");elseprintf("%lld\n", pr);}return0;}