QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#267822 | #3274. 随机游走 | KHIN | 50 | 288ms | 3688kb | C++17 | 1.7kb | 2023-11-27 19:06:21 | 2023-11-27 19:06:22 |
Judging History
answer
# include <bits/stdc++.h>
using namespace std;
long n, m, p;
inline long pow(long a, long n) {
long r(1);
while (n) {
r = r * (n & 1 ? a : 1) % p;
a = a * a % p, n >>= 1;
}
return r;
}
inline long inv(long x) {
x = (x % p + p) % p;
if (!x) return 0;
long res(1);
while (x != 1)
res = p - res * (p / x) % p, x = p % x;
return res;
}
inline long s(long const n, long const c) {
if (c == 1) return n * (n + 1) / 2 % p;
long const v0((c - pow(c, n + 1)) * inv(1 - c) % p);
long const v1(n * pow(c, n + 1) % p);
// cerr << __func__ << ' ' << n << ' ' << c << ' ';
// cerr << (v0 - v1) * inv(1 - c) % p << endl;
return (v0 - v1) * inv(1 - c) % p;
}
inline void tr(long& a, long& b, long l, long r, long c) {
// cerr << __func__ << ' ' << a << ' ' << b << ' ';
// cerr << l << ' ' << r << ' ' << c << endl;
b = (b + a * c % p * pow(c + 1, l - 1) % p * s(r - 1, inv(c + 1))) % p;
b = (b - a * c % p * pow(c + 1, l - 1) % p * s(l - 1, inv(c + 1))) % p;
a = a * pow(inv(c + 1), r - l) % p;
// cerr << "--->" << ' ' << a << ' ' << b << endl;
}
int main() {
long t;
cin >> t;
for (long i(1); i <= t; ++i) {
cin >> n >> m >> p;
if (n == 1) {
cout << 0 << endl;
continue;
}
long a(1), b(0);
if (m < n - 1) {
tr(a, b, 1, n - m, 0);
tr(a, b, n - m, n, 1);
} else {
long const q((m - (n - 2)) / (n - 1) % p);
long const r((m - (n - 2)) % (n - 1) % p);
long const p(n - r);
tr(a, b, 1, 2, q + 0);
tr(a, b, 2, p, q + 1);
tr(a, b, p, n, q + 2);
}
b = (b + (n - 1) * a) % p;
// cerr << a << ' ' << b << endl;
cout << (b + p) * inv(a) % p << endl;
}
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 10
Accepted
Test #1:
score: 10
Accepted
time: 1ms
memory: 3596kb
input:
10 1 4 13 5 5 13 4 1 5 5 4 19 5 3 13 4 3 5 5 5 3 5 0 13 5 3 11 5 2 11
output:
0 9 1 14 9 0 0 4 0 3
result:
ok 10 numbers
Test #2:
score: 0
Accepted
time: 1ms
memory: 3628kb
input:
10 2 1 83 1 5 13 5 5 79 5 2 13 5 3 73 5 2 2 5 5 3 2 5 31 2 5 31 5 4 19
output:
2 0 48 1 22 0 0 6 6 14
result:
ok 10 numbers
Test #3:
score: 0
Accepted
time: 0ms
memory: 3532kb
input:
10 5 3 881 4 3 857 5 1 709 5 2 991 4 4 523 2 1 709 3 4 613 3 3 109 5 2 983 2 0 461
output:
22 15 8 14 21 2 12 9 14 1
result:
ok 10 numbers
Subtask #2:
score: 10
Accepted
Dependency #1:
100%
Accepted
Subtask #3:
score: 0
Wrong Answer
Dependency #2:
100%
Accepted
Test #1:
score: 25
Accepted
time: 0ms
memory: 3600kb
input:
10 1 4 13 5 5 13 4 1 5 5 4 19 5 3 13 4 3 5 5 5 3 5 0 13 5 3 11 5 2 11
output:
0 9 1 14 9 0 0 4 0 3
result:
ok 10 numbers
Test #2:
score: 0
Accepted
time: 0ms
memory: 3684kb
input:
10 2 1 83 1 5 13 5 5 79 5 2 13 5 3 73 5 2 2 5 5 3 2 5 31 2 5 31 5 4 19
output:
2 0 48 1 22 0 0 6 6 14
result:
ok 10 numbers
Test #3:
score: 0
Accepted
time: 0ms
memory: 3676kb
input:
10 5 3 881 4 3 857 5 1 709 5 2 991 4 4 523 2 1 709 3 4 613 3 3 109 5 2 983 2 0 461
output:
22 15 8 14 21 2 12 9 14 1
result:
ok 10 numbers
Test #4:
score: 0
Accepted
time: 0ms
memory: 3600kb
input:
10 2 95 487 4 100 601 3 83 311 5 34 821 2 17 211 5 99 499 4 99 233 5 63 883 5 98 41 2 100 491
output:
96 216 294 79 18 95 186 825 8 101
result:
ok 10 numbers
Test #5:
score: 0
Accepted
time: 1ms
memory: 3680kb
input:
10 5 100 1153 5 100 6367 5 99 9137 2 100 1217 5 99 4463 5 100 2441 2 100 4783 5 98 4289 3 100 2477 3 100 907
output:
245 4123 828 101 2452 1727 101 2624 175 838
result:
ok 10 numbers
Test #6:
score: 0
Accepted
time: 0ms
memory: 3600kb
input:
10 5 98 73897 5 43 67679 4 56 25457 5 41 27943 5 25 81931 5 98 71881 5 34 75161 5 87 56237 5 53 20113 5 88 67523
output:
70617 20892 8020 17580 3208 8816 9110 55445 4114 22492
result:
ok 10 numbers
Test #7:
score: 0
Accepted
time: 1ms
memory: 3588kb
input:
10 5 99 817646801 5 98 874706501 5 99 709025203 5 33 557309839 5 58 997029919 4 77 665716297 5 98 872663633 2 100 406914323 5 99 91695431 5 77 351306607
output:
457678 440102 457678 8210 61712 19710 440102 101 457678 176862
result:
ok 10 numbers
Test #8:
score: -25
Wrong Answer
time: 0ms
memory: 3596kb
input:
10 5 15 23 5 70 17 5 51 71 5 99 71 5 98 11 3 82 71 5 100 43 4 49 5 5 50 3 4 100 89
output:
11 0 2 12 3 31 2 1 0 33
result:
wrong answer 2nd numbers differ - expected: '14', found: '0'
Subtask #4:
score: 20
Accepted
Test #12:
score: 20
Accepted
time: 0ms
memory: 3600kb
input:
3 50 1176 996401303 32 2987 155037041 50 2988 151905847
output:
648908787 66401579 38490965
result:
ok 3 number(s): "648908787 66401579 38490965"
Test #13:
score: 0
Accepted
time: 0ms
memory: 3628kb
input:
3 29 2998 246545267 36 2974 478356289 50 2988 26040349
output:
56503230 47120338 17627948
result:
ok 3 number(s): "56503230 47120338 17627948"
Test #14:
score: 0
Accepted
time: 0ms
memory: 3688kb
input:
3 50 786 47167339 6 1927 749106907 50 2988 256035071
output:
11176882 251691451 148363749
result:
ok 3 number(s): "11176882 251691451 148363749"
Test #15:
score: 0
Accepted
time: 1ms
memory: 3556kb
input:
3 26 2183 384727177 30 261 964145569 50 2157 202689667
output:
44694850 83010044 86414622
result:
ok 3 number(s): "44694850 83010044 86414622"
Test #16:
score: 0
Accepted
time: 1ms
memory: 3672kb
input:
3 50 2987 113062769 50 2991 421297439 33 1497 159643999
output:
74284538 54636736 28803039
result:
ok 3 number(s): "74284538 54636736 28803039"
Test #17:
score: 0
Accepted
time: 0ms
memory: 3600kb
input:
3 20 2658 631506563 42 2993 985976993 50 2991 125834273
output:
463245633 350919151 103501536
result:
ok 3 number(s): "463245633 350919151 103501536"
Test #18:
score: 0
Accepted
time: 0ms
memory: 3664kb
input:
3 50 1666 935697701 50 2991 97880953 50 2989 379438277
output:
884846091 60999820 181024193
result:
ok 3 number(s): "884846091 60999820 181024193"
Test #19:
score: 0
Accepted
time: 0ms
memory: 3600kb
input:
3 6 821 460911571 50 1962 394748467 24 667 288131461
output:
265269934 191749401 30141866
result:
ok 3 number(s): "265269934 191749401 30141866"
Test #20:
score: 0
Accepted
time: 0ms
memory: 3620kb
input:
3 28 2996 4639 50 2989 5503 22 860 8317
output:
1707 4223 5631
result:
ok 3 number(s): "1707 4223 5631"
Subtask #5:
score: 10
Accepted
Test #21:
score: 10
Accepted
time: 286ms
memory: 3620kb
input:
100000 131041815 82425709 973269833 1000000000 133485914 914586097 1000000000 656456127 710371681 997582513 565181843 562372439 408132767 155729069 769555159 1000000000 273473664 948428059 1000000000 316183756 256824341 1000000000 545457176 316221341 1000000000 543611855 725299283 1000000000 2768170...
output:
170639233 354369375 437567644 510243471 474848560 683948739 33009378 221152396 105920857 235598916 161992388 59427021 31554528 76060516 176637795 218796785 781847957 359069190 45082252 154665745 340449824 792541264 629971088 226324230 11077269 42982894 109551810 205443463 314317005 817673349 1268759...
result:
ok 100000 numbers
Test #22:
score: 0
Accepted
time: 288ms
memory: 3616kb
input:
100000 1000000000 502051416 761 1000000000 317253303 7079 261914492 28544766 7109 1000000000 53098857 4523 1000000000 626990095 1511 1000000000 595867001 9941 1000000000 134705841 509 986311533 78215730 7213 829350538 787063677 9241 436495419 125319672 1303 989338444 679402972 9397 1000000000 173031...
output:
250 1512 1089 1203 538 5788 190 12 255 712 2274 1728 2543 292 312 1852 6568 784 1433 1908 200 2101 1177 2365 2989 1746 5384 765 7090 1864 4356 188 524 5623 2865 3188 174 1506 4888 281 1112 1792 4396 931 321 93 65 107 6980 899 1847 4012 346 4648 1536 4521 2264 5847 1182 5441 12 3148 7587 195 4614 328...
result:
ok 100000 numbers
Test #23:
score: 0
Accepted
time: 275ms
memory: 3588kb
input:
100000 702852278 555025604 611297 1000000000 318514972 242863 1000000000 22120024 742243 1000000000 975426496 249211 1000000000 267029366 511559 1000000000 327097680 860077 1000000000 823476501 456293 1000000000 730941227 702179 381858613 120644232 425279 445734908 320741873 113467 842079348 1233863...
output:
86315 16589 175584 22626 346882 324726 265525 287298 285360 11423 54114 118519 83977 213991 133972 451439 276699 721426 238834 247513 312761 227632 105550 264309 315865 244154 161747 60552 584353 32428 321816 132040 119685 655226 939668 298727 55273 412243 24001 32786 104763 411248 529986 305454 359...
result:
ok 100000 numbers
Subtask #6:
score: 0
Skipped
Dependency #1:
100%
Accepted
Dependency #2:
100%
Accepted
Dependency #3:
0%