Skip to content

Commit 30d3bb7

Browse files
committed
v1.2.3:添加和修复和若干内容
1 parent 0ecfcf8 commit 30d3bb7

3 files changed

Lines changed: 268 additions & 0 deletions

File tree

code/ex_euler.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# https://oj.socoding.cn/p/2117
2+
p, phi, f, fp = 998244353, 998244352, [1, 1], [1, 1]
3+
for i in range(2, 5001):
4+
f.append((f[-1] * i) % p)
5+
fp.append((fp[-1] * i) % phi)
6+
for _ in range(int(input())):
7+
n = int(input())
8+
print(pow(f[n], fp[n], p))

code/p4718_pollard_rho.cpp

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
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+
*/

code/p4718_pollard_rho_old.cpp

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
#define sc(x) scanf("%lld", &x)
4+
typedef long long ll;
5+
ll qpow(ll a, ll b, ll p)
6+
{
7+
ll r = 1;
8+
for (; b; b >>= 1)
9+
{
10+
if (b & 1)
11+
r = (__int128)r * a % p;
12+
a = (__int128)a * a % p;
13+
}
14+
return r;
15+
}
16+
bool millerRabin(ll p)
17+
{ // 判断素数
18+
if (p < 2)
19+
return 0;
20+
if (p == 2)
21+
return 1;
22+
if (p == 3)
23+
return 1;
24+
ll d = p - 1, r = 0;
25+
while (!(d & 1))
26+
++r, d >>= 1; // 将d处理为奇数
27+
for (ll k = 0; k < 10; ++k)
28+
{
29+
ll a = rand() % (p - 2) + 2;
30+
ll x = qpow(a, d, p);
31+
if (x == 1 || x == p - 1)
32+
continue;
33+
for (int i = 0; i < r - 1; ++i)
34+
{
35+
x = (__int128)x * x % p;
36+
if (x == p - 1)
37+
break;
38+
}
39+
if (x != p - 1)
40+
return 0;
41+
}
42+
return 1;
43+
}
44+
ll pollard_rho(ll x)
45+
{
46+
ll s = 0, t = 0, c = (ll)rand() % (x - 1) + 1;
47+
ll step = 0, goal = 1, val = 1;
48+
for (goal = 1;; goal <<= 1, s = t, val = 1)
49+
{
50+
for (step = 1; step <= goal; ++step)
51+
{
52+
t = ((__int128)t * t + c) % x; //随机数生成器
53+
val = ((__int128)val * abs(t - s)) % x;
54+
if (step % 127 == 0)
55+
{
56+
ll d = __gcd(val, x);
57+
if (d > 1)
58+
return d;
59+
}
60+
}
61+
ll d = __gcd(val, x);
62+
if (d > 1)
63+
return d;
64+
}
65+
}
66+
ll mx;
67+
void fac(ll x)
68+
{
69+
if (x <= mx || x < 2)
70+
return;
71+
if (millerRabin(x))
72+
{
73+
mx = max(mx, x); //最大质因子
74+
return;
75+
}
76+
ll p = x;
77+
while (p >= x) //直到找到平凡
78+
{
79+
p = pollard_rho(x);
80+
}
81+
while (x % p == 0)
82+
{
83+
x /= p;
84+
}
85+
fac(x), fac(p); // x*p=raw x
86+
}
87+
signed main()
88+
{
89+
ll t, n;
90+
cin.tie(0)->ios::sync_with_stdio(false);
91+
cin >> t;
92+
while (t--)
93+
{
94+
srand(time(0));
95+
cin >> n;
96+
mx = 0;
97+
fac(n);
98+
if (mx == n)
99+
{
100+
cout << "Prime\n";
101+
}
102+
else
103+
{
104+
cout << mx << '\n';
105+
}
106+
}
107+
return 0;
108+
}

0 commit comments

Comments
 (0)