1+ #include < iostream>
2+ #include < algorithm>
3+ #include < random>
4+ #include < chrono>
5+
6+ #define int long long
7+
8+ using namespace std ;
9+
10+ typedef long long ll;
11+
12+ auto seed = chrono::system_clock::now().time_since_epoch().count();
13+ mt19937 gen (seed);
14+
15+ uniform_int_distribution<long long > dis (1 , (long long )1e18);
16+
17+ ll qpow (ll a, ll b, ll mod)
18+ {
19+ ll r = 1 ;
20+
21+ for (; b; b >>= 1 )
22+ {
23+ if (b & 1 )
24+ r = (__int128)r * a % mod;
25+ a = (__int128)a * a % mod;
26+ }
27+
28+ return r;
29+ }
30+
31+ bool miller_rabin (ll p, ll k = 30 ) // 判断素数
32+ {
33+ if (p < 2 ) return false ;
34+ if (p == 2 ) return true ;
35+ if (p == 3 ) return true ;
36+
37+ ll d = p - 1 , r = 0 ;
38+ while (!(d & 1 )) ++r, d >>= 1 ; // 将d处理为奇数
39+
40+ while (k--) // k次随机运算提高准确率
41+ {
42+ ll a = dis (gen) % (p - 2 ) + 2 ;
43+ ll x = qpow (a, d, p);
44+
45+ if (x == 1 || x == p - 1 ) continue ;
46+
47+ for (ll i = 0 ; i < r - 1 ; ++i)
48+ {
49+ x = (__int128)x * x % p;
50+
51+ if (x == p - 1 ) break ;
52+ }
53+
54+ if (x != p - 1 ) return false ;
55+ }
56+
57+ return true ;
58+ }
59+
60+ ll pollard_rho (ll x)
61+ {
62+ ll c = dis (gen) % (x - 1 ) + 1 ;
63+
64+ for (ll goal = 1 , s = 0 , t; true ; goal <<= 1 , s = t)
65+ {
66+ ll val = 1 ;
67+
68+ for (ll step = 1 ; step <= goal; ++step)
69+ {
70+ t = ((__int128)t * t + c) % x; // 随机数生成器
71+ val = ((__int128)val * abs (t - s)) % x;
72+
73+ if (step % 127 == 0 )
74+ {
75+ ll d = __gcd (val, x);
76+ if (d > 1 ) return d;
77+ }
78+ }
79+
80+ ll d = __gcd (val, x);
81+ if (d > 1 ) return d;
82+ }
83+ }
84+
85+ ll get_maxi_prime_factor (ll x)
86+ {
87+ if (x < 2 ) return 0 ;
88+
89+ if (miller_rabin (x)) return x;
90+
91+ ll p = x;
92+ while (p >= x) p = pollard_rho (x); // 直到找到平凡
93+ while (x % p == 0 ) x /= p;
94+
95+ return max (get_maxi_prime_factor (x), get_maxi_prime_factor (p)); // 返回最大质因子
96+ }
97+
98+ signed main ()
99+ {
100+ ios::sync_with_stdio (false ),cin.tie (0 ),cout.tie (0 );
101+
102+ ll t, n;
103+
104+ cin >> t;
105+ while (t--)
106+ {
107+ cin >> n;
108+
109+ ll mx = get_maxi_prime_factor (n);
110+
111+ if (mx == n) cout << " Prime\n " ;
112+ else cout << mx << ' \n ' ;
113+ }
114+
115+ return 0 ;
116+ }
117+
118+ /* 因式分解:
119+ void get_prime_factors(ll base, ll index, map<ll, ll>& cnt_prime_fac)
120+ {
121+ if (base < 2) return;
122+
123+ if (miller_rabin(base))
124+ {
125+ cnt_prime_fac[base]+=index;
126+
127+ return;
128+ }
129+
130+ ll new_fac= base, multimes = 0;
131+ while (new_fac >= base) new_fac = pollard_rho(base); //直到找到平凡
132+ while (base % new_fac == 0) base /= new_fac, multimes++;
133+
134+ get_prime_factors(base, index, cnt_prime_fac);
135+ get_prime_factors(new_fac, index * multimes, cnt_prime_fac);
136+
137+ return;
138+ }
139+
140+ signed main()
141+ {
142+ ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
143+
144+ map<ll,ll> prime_factors;
145+ get_prime_factors(27, 2, prime_factors);
146+
147+ for(auto x:prime_factors) cout<<x.first<<':'<<x.second<<'\n';
148+
149+ return 0;
150+ }
151+
152+ */
0 commit comments