QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#267847 | #3274. 随机游走 | KHIN | 20 | 1ms | 3684kb | C++14 | 1.6kb | 2023-11-27 19:34:15 | 2023-11-27 19:34:16 |
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;
assert(x);
long res(1);
while (x != 1)
res = p - res * (p / x) % p, x = p % x;
return res;
}
inline long s0(long const n, long c)
{ return (c %= p) ? 1l * (1 - pow(c, n)) % p * inv(1 - c) % p : 1; }
inline long s1(long const n, long c) {
if (!(c %= p)) return 1;
long const v0((1 - pow(c, n)) * inv(1 - c) % p);
long const v1(n * pow(c, n) % p);
return (v0 - v1) * inv(1 - c) % p;
}
inline void tr(long& a, long& b, long l, long r, long c) {
if (l == r) return;
// cerr << __func__ << ' ' << a << ' ' << b << ' ';
// cerr << l << ' ' << r << ' ' << c << endl;
b = (b + 1l * a * c % p * r % p * s0(r - l, c + 1)) % p;
b = (b - 1l * a * c % p * 1 % p * s1(r - l, c + 1)) % p;
a = 1l * a * pow((c + 1) % p, 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, n - m, n, 1);
tr(a, b, 1, n - m, 0);
} else {
long const q((m - (n - 2)) / (n - 1));
long const r((m - (n - 2)) % (n - 1));
long const p(n - r);
tr(a, b, p, n, q + 2);
tr(a, b, 2, p, q + 1);
tr(a, b, 1, 2, q + 0);
}
cout << (b = (b + (n - 1) + p) % p) << endl;
}
}
詳細信息
Subtask #1:
score: 0
Runtime Error
Test #1:
score: 0
Runtime Error
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
result:
Subtask #2:
score: 0
Skipped
Dependency #1:
0%
Subtask #3:
score: 0
Skipped
Dependency #2:
0%
Subtask #4:
score: 20
Accepted
Test #12:
score: 20
Accepted
time: 0ms
memory: 3592kb
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: 3556kb
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: 3592kb
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: 0ms
memory: 3588kb
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: 0ms
memory: 3604kb
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: 3596kb
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: 3684kb
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: 3532kb
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: 1ms
memory: 3684kb
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: 0
Runtime Error
Test #21:
score: 0
Runtime Error
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:
result:
Subtask #6:
score: 0
Skipped
Dependency #1:
0%