QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#636946 | #9451. Expected Waiting Time | ucup-team2000# | WA | 855ms | 101468kb | C++23 | 1.9kb | 2024-10-13 04:20:25 | 2024-10-13 04:20:26 |
Judging History
answer
#include <bits/stdc++.h>
long long N, P, S, A, B;
long long a[2100000], b[2100000];
long long fpow(long long a, long long b, long long p) {
long long s = 1;
for (; b; b >>= 1, a = a * a % p)
if (b & 1) s = s * a % p;
return s;
}
long long fac[2100000], inv[2100000];
long long catalan(int n) { return fac[2 * n] * inv[n] % P * inv[n + 1] % P; }
int main() {
int T;
scanf("%d", &T);
while (T--) {
scanf("%lld%lld%lld%lld%lld", &N, &P, &S, &A, &B);
fac[0] = inv[0] = 1;
for (int i = 1; i <= 2 * N; ++i) fac[i] = fac[i - 1] * i % P;
inv[2 * N] = fpow(fac[2 * N], P - 2, P);
for (int i = 2 * N - 1; i; --i) inv[i] = inv[i + 1] * (i + 1) % P;
// for (int i = 1; i <= 2 * N; ++i) inv[i] = fpow(fac[i], P - 2, P);
a[0] = 0;
b[0] = S;
for (int i = 1; i <= 2 * N; ++i) {
b[i] = (1ll * A * b[i - 1] + B) % P;
a[i] = a[i - 1] + b[i] + 1;
}
static long long cat[2100000];
for (int i = 0; i <= 2 * N; ++i) {
cat[i] = catalan(i);
}
static long long cnt[2100000];
cnt[1] = cat[N];
for (int i = 2; i <= N; ++i) {
if (!(i & 1)) {
cnt[i] = (cnt[i - 1] - cat[N - i / 2] * cat[i / 2 - 1] % P + P) % P;
} else {
cnt[i] = cnt[i - 1];
}
}
if (N % 2 == 0) {
cnt[N] = cnt[1] * fpow(2, P - 2, P) % P;
}
for (int i = N + 1; i <= 2 * N; ++i) {
cnt[i] = (cnt[1] - cnt[2 * N + 1 - i] + P) % P;
}
// for (int i = 1; i <= 2 * N; ++i) {
// printf("%lld\n", cnt[i]);
// }
long long ans = 0;
for (int i = 1; i <= 2 * N; ++i) {
ans = (P + ans - cnt[i] * a[i] % P) % P;
ans = (ans + (P + cnt[1] - cnt[i]) % P * a[i] % P) % P;
}
printf("%lld\n", ans * fpow(cnt[1], P - 2, P) % P);
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 14144kb
input:
5 1 1000000007 0 1 0 2 1000000007 0 1 1 2 7 5 2 3 3 31 15 6 24 20 1000000007 0 1 0
output:
1 12 1 21 879705565
result:
ok 5 number(s): "1 12 1 21 879705565"
Test #2:
score: 0
Accepted
time: 835ms
memory: 14220kb
input:
4400 3954 1000000007 0 1 0 1306 1000000007 0 1 0 3774 1000000007 0 1 0 3345 1000000007 0 1 0 891 1000000007 0 1 0 2462 1000000007 0 1 0 237 1000000007 0 1 0 26 1000000007 0 1 0 2510 1000000007 0 1 0 637 1000000007 0 1 0 3250 1000000007 0 1 0 3447 1000000007 0 1 0 1222 1000000007 0 1 0 133 1000000007...
output:
440618338 378292891 979368645 915766295 343598158 80867595 161627927 517387931 396936703 42785642 945720545 764273281 186237656 635777911 164064906 548455037 991964184 468137124 561243246 118562285 856945294 642467240 23673926 808943705 897417615 462422554 656411244 204288121 997894281 244685651 762...
result:
ok 4400 numbers
Test #3:
score: -100
Wrong Answer
time: 855ms
memory: 101468kb
input:
1019 338 1863833207 1820742817 1507924477 1822273457 770 1386304741 1088481071 1216187083 170973217 597 1604266739 620750027 196415899 456280997 105 1008587891 184044403 24836083 926135599 357 1165127407 440925347 1103369747 912263123 82 1639766993 263045351 631010551 1412721139 928 1715915153 25383...
output:
825784992 359233716 241333768 -95778628 -1160944690 709041469 884303187 1601268067 1494095864 -1336071427 671306112 161694464 769449714 -1559540734 1186117842 -1715047713 1088779437 99856958 696307447 881114795 -1103626203 -68695181 1595128302 1139824478 -344886724 1022623810 406713700 380826273 821...
result:
wrong answer 1st numbers differ - expected: '1532578211', found: '825784992'