QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#448515 | #1285. Stirling Number | zlt | AC ✓ | 115ms | 35348kb | C++14 | 2.5kb | 2024-06-19 18:16:14 | 2024-06-19 18:16:14 |
Judging History
answer
#include <bits/stdc++.h>
#define pb emplace_back
#define fst first
#define scd second
#define mkp make_pair
#define mems(a, x) memset((a), (x), sizeof(a))
using namespace std;
typedef long long ll;
typedef double db;
typedef unsigned long long ull;
typedef long double ldb;
typedef pair<ll, ll> pii;
const int maxn = 1000100;
ll n, L, R, P, fac[maxn], ifac[maxn], N, G, q, r, inv[maxn], pw[maxn];
inline ll qpow(ll b, ll p) {
ll res = 1;
while (p) {
if (p & 1) {
res = res * b % P;
}
b = b * b % P;
p >>= 1;
}
return res;
}
inline ll C(ll n, ll m) {
if (n < m || n < 0 || m < 0) {
return 0;
}
if (n >= P) {
return C(n / P, m / P) * C(n % P, m % P) % P;
} else {
return fac[n] * ifac[m] % P * ifac[n - m] % P;
}
}
inline bool check(ll x) {
ll t = P - 1;
vector<ll> vc;
for (ll i = 2; i * i <= t; ++i) {
if (t % i == 0) {
vc.pb(i);
while (t % i == 0) {
t /= i;
}
}
}
if (t > 1) {
vc.pb(t);
}
for (ll y : vc) {
if (qpow(x, (P - 1) / y) == 1) {
return 0;
}
}
return 1;
}
inline ll g(ll n, ll m) {
ll p = 1, ip = 1, iG = qpow(G, P - 2), res = 0;
for (int i = 0; i <= P - 2; ++i, p = p * G % P, ip = ip * iG % P) {
if (p + n - 1 >= P) {
continue;
}
if (ip == 1) {
res = (res + (m + 1) * fac[p + n - 1] % P * ifac[p - 1]) % P;
} else {
res = (res + (pw[((-(m + 1) * i) % (P - 1) + P - 1) % (P - 1)] + P - 1) % P * inv[(ip + P - 1) % P] % P * fac[p + n - 1] % P * ifac[p - 1]) % P;
}
}
return (P - res) % P;
}
inline ll h(ll m) {
if (m < q) {
return 0;
}
ll k = (m - q) / (P - 1);
ll t = (m - q) % (P - 1);
ll ans = C(q, k) * g(r, t) % P;
ans = (ans - C(q - 1, k - 1) * fac[r] % P + P) % P;
if ((q - k) & 1) {
ans = (P - ans) % P;
}
return ans;
}
void solve() {
scanf("%lld%lld%lld%lld", &n, &L, &R, &P);
N = P - 1;
fac[0] = 1;
for (int i = 1; i <= N; ++i) {
fac[i] = fac[i - 1] * i % P;
}
ifac[N] = qpow(fac[N], P - 2);
for (int i = N - 1; ~i; --i) {
ifac[i] = ifac[i + 1] * (i + 1) % P;
}
inv[1] = 1;
for (int i = 2; i <= N; ++i) {
inv[i] = (P - P / i) * inv[P % i] % P;
}
for (G = 1;; ++G) {
if (check(G)) {
break;
}
}
pw[0] = 1;
for (int i = 1; i <= P - 2; ++i) {
pw[i] = pw[i - 1] * G % P;
}
q = n / P;
r = n - P * q;
printf("%lld\n", (h(R) - h(L - 1) + P) % P);
}
int main() {
int T = 1;
// scanf("%d", &T);
while (T--) {
solve();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 9912kb
input:
4 1 4 5
output:
4
result:
ok "4"
Test #2:
score: 0
Accepted
time: 0ms
memory: 9928kb
input:
6 5 5 29
output:
15
result:
ok "15"
Test #3:
score: 0
Accepted
time: 104ms
memory: 35060kb
input:
1000 685 975 999983
output:
482808
result:
ok "482808"
Test #4:
score: 0
Accepted
time: 1ms
memory: 9936kb
input:
8 7 8 7
output:
1
result:
ok "1"
Test #5:
score: 0
Accepted
time: 2ms
memory: 9888kb
input:
6 4 6 3
output:
2
result:
ok "2"
Test #6:
score: 0
Accepted
time: 1ms
memory: 9932kb
input:
31494923 16387579 27674098 2
output:
0
result:
ok "0"
Test #7:
score: 0
Accepted
time: 28ms
memory: 22640kb
input:
1000000000 179971578 813833436 383101
output:
53093
result:
ok "53093"
Test #8:
score: 0
Accepted
time: 24ms
memory: 21180kb
input:
1000000000 243662537 919841454 336437
output:
75332
result:
ok "75332"
Test #9:
score: 0
Accepted
time: 97ms
memory: 35108kb
input:
1000000000 802415407 880806868 999983
output:
960771
result:
ok "960771"
Test #10:
score: 0
Accepted
time: 103ms
memory: 35328kb
input:
1000000000 644768002 859679621 999983
output:
805072
result:
ok "805072"
Test #11:
score: 0
Accepted
time: 115ms
memory: 35016kb
input:
1000000000 413491669 805704689 999983
output:
138470
result:
ok "138470"
Test #12:
score: 0
Accepted
time: 52ms
memory: 35108kb
input:
48537068788 22847195743 28163317559 999983
output:
529264
result:
ok "529264"
Test #13:
score: 0
Accepted
time: 77ms
memory: 35076kb
input:
828536325370 779765412000 782633091631 999983
output:
836701
result:
ok "836701"
Test #14:
score: 0
Accepted
time: 69ms
memory: 35048kb
input:
258077969836231211 200983000620029238 226661348290193221 999983
output:
104488
result:
ok "104488"
Test #15:
score: 0
Accepted
time: 50ms
memory: 35036kb
input:
258904208719347339 185679775210965354 223112834501603079 999983
output:
0
result:
ok "0"
Test #16:
score: 0
Accepted
time: 84ms
memory: 35348kb
input:
259730451897430763 53367210716367086 159749126385022258 999983
output:
0
result:
ok "0"
Test #17:
score: 0
Accepted
time: 62ms
memory: 35048kb
input:
260556695075514187 28149045695465635 29562653808086859 999983
output:
669091
result:
ok "669091"
Test #18:
score: 0
Accepted
time: 75ms
memory: 34708kb
input:
1000000000000000000 199156813867768126 571262092911942493 919337
output:
732102
result:
ok "732102"
Test #19:
score: 0
Accepted
time: 66ms
memory: 34992kb
input:
353534534534 1999983 2234324324 999983
output:
613376
result:
ok "613376"
Test #20:
score: 0
Accepted
time: 36ms
memory: 35128kb
input:
353534534534 0 2234324324 999983
output:
520965
result:
ok "520965"
Test #21:
score: 0
Accepted
time: 64ms
memory: 35040kb
input:
353534534534 22342343 353534534534 999983
output:
281927
result:
ok "281927"