QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#75983 | #5031. 核 | alpha1022 | 100 ✓ | 497ms | 199224kb | C++17 | 2.4kb | 2023-02-06 21:56:45 | 2023-02-06 21:56:48 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
using ull = unsigned long long;
using L = __uint128_t;
int mod;
void add(int &x, int y) { if ((x += y - mod) < 0) x += mod; }
void sub(int &x, int y) { if ((x -= y) < 0) x += mod; }
int neg(int x) { return x ? mod - x : 0; }
struct FastMod {
ull b, m;
FastMod(ull b) : b(b), m(ull((L(1) << 64) / b)) {}
ull operator()(ull a) {
ull q = (ull)((L(m) * a) >> 64);
ull r = a - q * b; // can be proven that 0 <= r < 2*b
return r >= b ? r - b : r;
}
} R(2);
int mpow(int a, int b) {
int ret = 1;
for (; b; b >>= 1) {
if (b & 1) ret = R((ull)ret * a);
a = R((ull)a * a);
}
return ret;
}
const int N = 1e7;
const int BRUTE_N2_LIMIT = 50;
int n, q, qinv;
int pw[N + 5], qk[N + 5], qfac[N + 5], qifac[N + 5];
int f[N + 5];
int bsPw[32769], gsPw[32768];
int query(int n) { return R((ull)bsPw[n & 32767] * gsPw[n >> 15]); }
int ans;
int main() {
scanf("%d%d%d", &n, &q, &mod), R = FastMod(mod), qinv = mpow(q, mod - 2);
bsPw[0] = gsPw[0] = 1;
for (int i = 1; i <= (1 << 15); ++i) add(bsPw[i] = bsPw[i - 1], bsPw[i - 1]), add(bsPw[i], bsPw[i - 1]);
for (int i = 1; i < (1 << 15); ++i) gsPw[i] = R((ull)gsPw[i - 1] * bsPw[1 << 15]);
pw[0] = 1;
for (int i = 1; i <= n; ++i) pw[i] = R((ull)pw[i - 1] * q);
qfac[0] = 1;
for (int i = 1; i <= n; ++i) qfac[i] = R((ull)qfac[i - 1] * (qk[i] = pw[i] - 1));
qifac[n] = mpow(qfac[n], mod - 2);
for (int i = n; i; --i) qifac[i - 1] = R((ull)qifac[i] * qk[i]);
f[0] = 1;
for (int i = 1; i <= min(BRUTE_N2_LIMIT, n); ++i)
f[i] = R((ull)f[i - 1] * (pw[n] - pw[n - i] + mod));
for (int i = 1; i <= min(BRUTE_N2_LIMIT, n); ++i)
for (int j = 0; j < i; ++j) f[i] = R(f[i] + (mod - f[j]) * R(R((ull)qfac[i] * qifac[j]) * qifac[i - j]));
for (int i = BRUTE_N2_LIMIT + 1; i <= n; ++i) {
int coe1 = qinv;
add(coe1, pw[n]), sub(coe1, pw[i - 1]), sub(coe1, pw[i - 2]), sub(coe1, pw[n - i]);
int coe2 = R((ull)(pw[n - 1] - pw[i - 3] + mod) * qk[i - 1]);
f[i] = R((ull)coe1 * f[i - 1] + (ull)coe2 * f[i - 2]);
}
int t = 1;
for (int i = 1; i <= n; ++i) t = (ull)t * q % (mod - 1);
for (int i = 0, c = 1; i <= n; ++i)
ans = R(ans + R(query(c) * R(qifac[i] * R((ull)qifac[n - i] * f[n - i])))),
c = (ull)c * t % (mod - 1);
ans = R((ull)ans * qfac[n]);
printf("%d\n", ans);
}
詳細信息
Subtask #1:
score: 10
Accepted
Test #1:
score: 10
Accepted
time: 5ms
memory: 12020kb
input:
1 2 540053233
output:
9
result:
ok 1 number(s): "9"
Test #2:
score: 0
Accepted
time: 1ms
memory: 12084kb
input:
2 2 156542707
output:
43046970
result:
ok 1 number(s): "43046970"
Test #3:
score: 0
Accepted
time: 4ms
memory: 12020kb
input:
1 2 186225229
output:
9
result:
ok 1 number(s): "9"
Test #4:
score: 0
Accepted
time: 1ms
memory: 12092kb
input:
3 3 109884329
output:
100602209
result:
ok 1 number(s): "100602209"
Test #5:
score: 0
Accepted
time: 4ms
memory: 12116kb
input:
1 2 144802297
output:
9
result:
ok 1 number(s): "9"
Subtask #2:
score: 20
Accepted
Dependency #1:
100%
Accepted
Test #6:
score: 20
Accepted
time: 4ms
memory: 12076kb
input:
20 21992843 328859143
output:
110137213
result:
ok 1 number(s): "110137213"
Test #7:
score: 0
Accepted
time: 5ms
memory: 14108kb
input:
22 332524739 654888401
output:
410922781
result:
ok 1 number(s): "410922781"
Test #8:
score: 0
Accepted
time: 3ms
memory: 12080kb
input:
26 302215049 566649113
output:
221720840
result:
ok 1 number(s): "221720840"
Test #9:
score: 0
Accepted
time: 0ms
memory: 12016kb
input:
15 111009527 722130737
output:
648834664
result:
ok 1 number(s): "648834664"
Test #10:
score: 0
Accepted
time: 1ms
memory: 12020kb
input:
82 110032063 394529383
output:
111730592
result:
ok 1 number(s): "111730592"
Test #11:
score: 0
Accepted
time: 0ms
memory: 12176kb
input:
9 11172911 259650437
output:
68381774
result:
ok 1 number(s): "68381774"
Test #12:
score: 0
Accepted
time: 6ms
memory: 11976kb
input:
86 12016027 354886243
output:
263687778
result:
ok 1 number(s): "263687778"
Test #13:
score: 0
Accepted
time: 2ms
memory: 12172kb
input:
91 273689959 454097881
output:
114436127
result:
ok 1 number(s): "114436127"
Test #14:
score: 0
Accepted
time: 1ms
memory: 12088kb
input:
73 148878967 694206977
output:
176215101
result:
ok 1 number(s): "176215101"
Test #15:
score: 0
Accepted
time: 1ms
memory: 12028kb
input:
45 205982233 227598247
output:
156769598
result:
ok 1 number(s): "156769598"
Subtask #3:
score: 10
Accepted
Dependency #2:
100%
Accepted
Test #16:
score: 10
Accepted
time: 1ms
memory: 12192kb
input:
2778 122825869 147297463
output:
43419574
result:
ok 1 number(s): "43419574"
Test #17:
score: 0
Accepted
time: 3ms
memory: 12084kb
input:
289 7729669 589652893
output:
552952137
result:
ok 1 number(s): "552952137"
Test #18:
score: 0
Accepted
time: 1ms
memory: 11984kb
input:
2281 35651417 203950963
output:
21659018
result:
ok 1 number(s): "21659018"
Test #19:
score: 0
Accepted
time: 1ms
memory: 12076kb
input:
1684 258745639 373223677
output:
355596229
result:
ok 1 number(s): "355596229"
Test #20:
score: 0
Accepted
time: 2ms
memory: 12100kb
input:
2107 86850989 455823859
output:
245960059
result:
ok 1 number(s): "245960059"
Test #21:
score: 0
Accepted
time: 2ms
memory: 12048kb
input:
1323 43290799 791120419
output:
509649562
result:
ok 1 number(s): "509649562"
Test #22:
score: 0
Accepted
time: 1ms
memory: 12028kb
input:
2401 34064903 185314627
output:
70571452
result:
ok 1 number(s): "70571452"
Test #23:
score: 0
Accepted
time: 2ms
memory: 12100kb
input:
1073 82288187 564447959
output:
168200843
result:
ok 1 number(s): "168200843"
Test #24:
score: 0
Accepted
time: 1ms
memory: 12116kb
input:
1926 29995039 129122281
output:
60921463
result:
ok 1 number(s): "60921463"
Test #25:
score: 0
Accepted
time: 1ms
memory: 14068kb
input:
3000 66915659 765705179
output:
222619979
result:
ok 1 number(s): "222619979"
Subtask #4:
score: 20
Accepted
Test #26:
score: 20
Accepted
time: 60ms
memory: 33636kb
input:
998818 198334853 998244353
output:
153251445
result:
ok 1 number(s): "153251445"
Test #27:
score: 0
Accepted
time: 45ms
memory: 34084kb
input:
914379 128814383 998244353
output:
477606145
result:
ok 1 number(s): "477606145"
Test #28:
score: 0
Accepted
time: 49ms
memory: 33576kb
input:
944474 478445339 998244353
output:
174204073
result:
ok 1 number(s): "174204073"
Test #29:
score: 0
Accepted
time: 39ms
memory: 32328kb
input:
948637 711592127 998244353
output:
178256114
result:
ok 1 number(s): "178256114"
Test #30:
score: 0
Accepted
time: 49ms
memory: 32116kb
input:
927564 14465663 998244353
output:
315244613
result:
ok 1 number(s): "315244613"
Test #31:
score: 0
Accepted
time: 44ms
memory: 33200kb
input:
934615 392799073 998244353
output:
892700270
result:
ok 1 number(s): "892700270"
Test #32:
score: 0
Accepted
time: 42ms
memory: 24404kb
input:
917196 124972031 998244353
output:
782017412
result:
ok 1 number(s): "782017412"
Test #33:
score: 0
Accepted
time: 53ms
memory: 31088kb
input:
957149 392606173 998244353
output:
159348443
result:
ok 1 number(s): "159348443"
Test #34:
score: 0
Accepted
time: 47ms
memory: 30680kb
input:
997042 184649453 998244353
output:
464643024
result:
ok 1 number(s): "464643024"
Test #35:
score: 0
Accepted
time: 49ms
memory: 30948kb
input:
953353 14071961 998244353
output:
391688875
result:
ok 1 number(s): "391688875"
Subtask #5:
score: 10
Accepted
Test #36:
score: 10
Accepted
time: 476ms
memory: 198120kb
input:
9909956 720431399 720431401
output:
86883659
result:
ok 1 number(s): "86883659"
Test #37:
score: 0
Accepted
time: 456ms
memory: 198624kb
input:
9924163 267052829 267052831
output:
75754681
result:
ok 1 number(s): "75754681"
Test #38:
score: 0
Accepted
time: 471ms
memory: 198980kb
input:
9967885 197873129 197873131
output:
16653739
result:
ok 1 number(s): "16653739"
Test #39:
score: 0
Accepted
time: 437ms
memory: 198644kb
input:
9952642 101872151 101872153
output:
0
result:
ok 1 number(s): "0"
Test #40:
score: 0
Accepted
time: 451ms
memory: 198848kb
input:
9955909 167874431 167874433
output:
130012020
result:
ok 1 number(s): "130012020"
Test #41:
score: 0
Accepted
time: 456ms
memory: 199176kb
input:
9994785 399509567 399509569
output:
153324498
result:
ok 1 number(s): "153324498"
Test #42:
score: 0
Accepted
time: 478ms
memory: 198848kb
input:
9954011 108819131 108819133
output:
101671540
result:
ok 1 number(s): "101671540"
Test #43:
score: 0
Accepted
time: 459ms
memory: 199224kb
input:
9997570 213315827 213315829
output:
57441081
result:
ok 1 number(s): "57441081"
Test #44:
score: 0
Accepted
time: 469ms
memory: 199180kb
input:
9995867 113028299 113028301
output:
67837072
result:
ok 1 number(s): "67837072"
Test #45:
score: 0
Accepted
time: 455ms
memory: 198524kb
input:
9909335 247275617 247275619
output:
202966817
result:
ok 1 number(s): "202966817"
Subtask #6:
score: 30
Accepted
Dependency #1:
100%
Accepted
Dependency #2:
100%
Accepted
Dependency #3:
100%
Accepted
Dependency #4:
100%
Accepted
Dependency #5:
100%
Accepted
Test #46:
score: 30
Accepted
time: 468ms
memory: 198684kb
input:
9921815 38466881 725310841
output:
601117286
result:
ok 1 number(s): "601117286"
Test #47:
score: 0
Accepted
time: 463ms
memory: 198040kb
input:
9919464 4830599 747345523
output:
168521454
result:
ok 1 number(s): "168521454"
Test #48:
score: 0
Accepted
time: 471ms
memory: 199000kb
input:
9981374 3616373 154722097
output:
2696288
result:
ok 1 number(s): "2696288"
Test #49:
score: 0
Accepted
time: 476ms
memory: 198348kb
input:
9906664 12433457 558159149
output:
538699014
result:
ok 1 number(s): "538699014"
Test #50:
score: 0
Accepted
time: 488ms
memory: 199132kb
input:
9985736 46853 410275823
output:
258567756
result:
ok 1 number(s): "258567756"
Test #51:
score: 0
Accepted
time: 476ms
memory: 198944kb
input:
9962926 33790087 203505083
output:
40932778
result:
ok 1 number(s): "40932778"
Test #52:
score: 0
Accepted
time: 466ms
memory: 198388kb
input:
9903735 146658401 157137433
output:
154493145
result:
ok 1 number(s): "154493145"
Test #53:
score: 0
Accepted
time: 460ms
memory: 198472kb
input:
9913516 105010771 110717611
output:
67979325
result:
ok 1 number(s): "67979325"
Test #54:
score: 0
Accepted
time: 449ms
memory: 198568kb
input:
9953517 268142489 675913921
output:
523115756
result:
ok 1 number(s): "523115756"
Test #55:
score: 0
Accepted
time: 487ms
memory: 198952kb
input:
9981005 11993207 114120883
output:
7261617
result:
ok 1 number(s): "7261617"
Test #56:
score: 0
Accepted
time: 476ms
memory: 198748kb
input:
9945956 36522077 168104303
output:
82398556
result:
ok 1 number(s): "82398556"
Test #57:
score: 0
Accepted
time: 497ms
memory: 199024kb
input:
9967933 15301477 352827883
output:
242773007
result:
ok 1 number(s): "242773007"
Test #58:
score: 0
Accepted
time: 474ms
memory: 198800kb
input:
9911781 83845891 360130933
output:
158254305
result:
ok 1 number(s): "158254305"
Test #59:
score: 0
Accepted
time: 464ms
memory: 198584kb
input:
9916390 100404191 108138473
output:
103346432
result:
ok 1 number(s): "103346432"
Test #60:
score: 0
Accepted
time: 440ms
memory: 199008kb
input:
9974438 7828049 430399297
output:
76675277
result:
ok 1 number(s): "76675277"