QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#775329 | #8211. Enumerating Substrings | caijianhong | AC ✓ | 32ms | 23440kb | C++23 | 3.6kb | 2024-11-23 15:30:20 | 2024-11-23 15:30:21 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#define debug(...) fprintf(stderr, ##__VA_ARGS__)
#else
#define endl "\n"
#define debug(...) void(0)
#endif
using LL = long long;
template <unsigned umod>
struct modint {/*{{{*/
static constexpr int mod = umod;
unsigned v;
modint() = default;
template <class T, enable_if_t<is_integral<T>::value>* = nullptr> modint(const T& _y) :
v((unsigned)(_y % mod + (is_signed<T>() && _y < 0 ? mod : 0))) {}
modint& operator+=(const modint& rhs) { v += rhs.v; if (v >= umod) v -= umod; return *this; }
modint& operator-=(const modint& rhs) { v -= rhs.v; if (v >= umod) v += umod; return *this; }
modint& operator*=(const modint& rhs) { v = (unsigned)(1ull * v * rhs.v % umod); return *this; }
modint& operator/=(const modint& rhs) { assert(rhs.v); return *this *= qpow(rhs, mod - 2); }
friend int raw(const modint& self) { return self.v; }
friend ostream& operator<<(ostream& os, const modint& self) { return os << raw(self); }
friend modint operator+(modint lhs, const modint& rhs) { return lhs += rhs; }
friend modint operator-(modint lhs, const modint& rhs) { return lhs -= rhs; }
friend modint operator*(modint lhs, const modint& rhs) { return lhs *= rhs; }
friend modint operator/(modint lhs, const modint& rhs) { return lhs /= rhs; }
friend modint qpow(modint a, int b) {
assert(b >= 0);
modint r = 1;
while (b) {
if (b & 1) r *= a;
if (b >>= 1) a *= a;
}
return r;
}
};/*}}}*/
using mint = modint<int(1e9 + 7)>;
template <int N>
struct C_prime {
mint fac[N + 10], ifac[N + 10];
C_prime() {
fac[0] = 1;
for (int i = 1; i <= N; i++) fac[i] = fac[i - 1] * i;
ifac[N] = 1 / fac[N];
for (int i = N; i >= 1; i--) ifac[i - 1] = ifac[i] * i;
}
mint operator()(int n, int m) const { return n >= m ? fac[n] * ifac[m] * ifac[n - m] : 0; }
mint inv(int x) const { return ifac[x] * fac[x - 1]; }
};
C_prime<2000010> binom;
struct underpow {
vector<mint> kd, ikd;
underpow() = default;
underpow(int m, int k) : kd(m + 1), ikd(m + 1) {
// kd[i] = k^\underline i
kd[0] = 1;
for (int i = 1; i <= m; i++) kd[i] = kd[i - 1] * (k - i + 1);
ikd[m] = 1 / kd[m];
for (int i = m; i >= 1; i--) ikd[i - 1] = ikd[i] * (k - i + 1);
}
};
int n, m, K;
mint qpK[1000010], iqp2[4010];
void init() {
qpK[0] = 1;
for (int i = 1; i <= n; i++) qpK[i] = qpK[i - 1] * K;
mint iz = binom.inv(2);
iqp2[0] = 1;
for (int i = 1; i <= 4000; i++) iqp2[i] = iqp2[i - 1] * iz;
}
mint calc(int m, int k) {
underpow udp(min(m, k), k);
mint res = 0;
for (int i = max(m - k, 0); i <= min(k, m / 2); i++) {
res += udp.kd[i] * binom.ifac[i] * iqp2[i] * udp.kd[m - i] * udp.ikd[i] * binom.ifac[m - 2 * i];
// debug("i = %d, res += %d * %d * %d\n", i, raw(udp.kd[i] * binom.ifac[i]), raw(iqp2[i]), raw(udp.kd[m - i] * udp.ikd[i] * binom.ifac[m - 2 * i]));
}
debug("calc(%d, %d) = %d\n", m, k, raw(res * binom.fac[m]));
return res * binom.fac[m];
}
mint coeS(int m) { return (n - m + 1) * qpK[n - m]; }
int main() {
#ifndef LOCAL
#ifdef NF
freopen("lake.in", "r", stdin);
freopen("lake.out", "w", stdout);
#endif
cin.tie(nullptr)->sync_with_stdio(false);
#endif
cin >> n >> m >> K;
init();
mint ans = calc(m, K) * coeS(m);
debug("ans0 = %d\n", raw(ans));
underpow udp(min(m, K), K);
for (int i = 1; i <= min(K, m / 2); i++) {
mint coeP = udp.kd[i] * calc(m - 2 * i, K - i);
for (int j = 2; i + j * (m - i) <= n; j++) {
ans += (j & 1 ? +1 : -1) * coeP * coeS(i + j * (m - i));
}
}
cout << ans << endl;
return 0;
}
这程序好像有点Bug,我给组数据试试?
詳細信息
Test #1:
score: 100
Accepted
time: 14ms
memory: 20292kb
input:
4 2 3
output:
228
result:
ok 1 number(s): "228"
Test #2:
score: 0
Accepted
time: 32ms
memory: 23404kb
input:
999999 1999 12345678
output:
52352722
result:
ok 1 number(s): "52352722"
Test #3:
score: 0
Accepted
time: 17ms
memory: 19700kb
input:
7 4 2
output:
182
result:
ok 1 number(s): "182"
Test #4:
score: 0
Accepted
time: 18ms
memory: 20320kb
input:
4 3 4
output:
480
result:
ok 1 number(s): "480"
Test #5:
score: 0
Accepted
time: 17ms
memory: 19308kb
input:
3 1 1
output:
3
result:
ok 1 number(s): "3"
Test #6:
score: 0
Accepted
time: 17ms
memory: 20780kb
input:
5 5 1
output:
0
result:
ok 1 number(s): "0"
Test #7:
score: 0
Accepted
time: 10ms
memory: 19596kb
input:
7 4 3
output:
5784
result:
ok 1 number(s): "5784"
Test #8:
score: 0
Accepted
time: 14ms
memory: 20500kb
input:
5 2 4
output:
3932
result:
ok 1 number(s): "3932"
Test #9:
score: 0
Accepted
time: 8ms
memory: 20444kb
input:
8 2 2
output:
1522
result:
ok 1 number(s): "1522"
Test #10:
score: 0
Accepted
time: 13ms
memory: 21116kb
input:
8 1 2
output:
2048
result:
ok 1 number(s): "2048"
Test #11:
score: 0
Accepted
time: 13ms
memory: 20936kb
input:
7 5 3
output:
2430
result:
ok 1 number(s): "2430"
Test #12:
score: 0
Accepted
time: 14ms
memory: 19524kb
input:
10 4 3
output:
272004
result:
ok 1 number(s): "272004"
Test #13:
score: 0
Accepted
time: 16ms
memory: 23152kb
input:
675978 614 2
output:
0
result:
ok 1 number(s): "0"
Test #14:
score: 0
Accepted
time: 15ms
memory: 20724kb
input:
244613 38 1
output:
0
result:
ok 1 number(s): "0"
Test #15:
score: 0
Accepted
time: 15ms
memory: 22212kb
input:
186293 1462 1
output:
0
result:
ok 1 number(s): "0"
Test #16:
score: 0
Accepted
time: 17ms
memory: 19392kb
input:
24867 886 1
output:
0
result:
ok 1 number(s): "0"
Test #17:
score: 0
Accepted
time: 21ms
memory: 23068kb
input:
976164 1014 2
output:
0
result:
ok 1 number(s): "0"
Test #18:
score: 0
Accepted
time: 18ms
memory: 20372kb
input:
179356 2 716844809
output:
577866092
result:
ok 1 number(s): "577866092"
Test #19:
score: 0
Accepted
time: 19ms
memory: 23036kb
input:
621001 130 310625363
output:
892869197
result:
ok 1 number(s): "892869197"
Test #20:
score: 0
Accepted
time: 20ms
memory: 23396kb
input:
678862 850 754662812
output:
582264789
result:
ok 1 number(s): "582264789"
Test #21:
score: 0
Accepted
time: 22ms
memory: 23164kb
input:
650845 978 348443366
output:
825425732
result:
ok 1 number(s): "825425732"
Test #22:
score: 0
Accepted
time: 16ms
memory: 23136kb
input:
669914 402 87448112
output:
318098088
result:
ok 1 number(s): "318098088"
Test #23:
score: 0
Accepted
time: 19ms
memory: 23360kb
input:
998593 530 681228665
output:
408255654
result:
ok 1 number(s): "408255654"
Test #24:
score: 0
Accepted
time: 25ms
memory: 23152kb
input:
369361 1954 125266115
output:
509912384
result:
ok 1 number(s): "509912384"
Test #25:
score: 0
Accepted
time: 26ms
memory: 23100kb
input:
900226 1378 424079373
output:
406320917
result:
ok 1 number(s): "406320917"
Test #26:
score: 0
Accepted
time: 26ms
memory: 23168kb
input:
334887 1506 17859926
output:
503264679
result:
ok 1 number(s): "503264679"
Test #27:
score: 0
Accepted
time: 27ms
memory: 23440kb
input:
936048 544 53978328
output:
548647866
result:
ok 1 number(s): "548647866"
Test #28:
score: 0
Accepted
time: 16ms
memory: 23404kb
input:
152789 1264 792983073
output:
839541707
result:
ok 1 number(s): "839541707"
Test #29:
score: 0
Accepted
time: 28ms
memory: 23216kb
input:
714493 1392 91796331
output:
721071046
result:
ok 1 number(s): "721071046"
Test #30:
score: 0
Accepted
time: 22ms
memory: 23144kb
input:
269571 816 830801077
output:
330064211
result:
ok 1 number(s): "330064211"
Test #31:
score: 0
Accepted
time: 28ms
memory: 23360kb
input:
845120 944 424581630
output:
348960190
result:
ok 1 number(s): "348960190"
Test #32:
score: 0
Accepted
time: 17ms
memory: 23432kb
input:
533990 368 163586376
output:
522092095
result:
ok 1 number(s): "522092095"
Test #33:
score: 0
Accepted
time: 24ms
memory: 20968kb
input:
181707 1792 462399634
output:
373795106
result:
ok 1 number(s): "373795106"
Test #34:
score: 0
Accepted
time: 31ms
memory: 23048kb
input:
417349 1920 761212891
output:
587051329
result:
ok 1 number(s): "587051329"
Test #35:
score: 0
Accepted
time: 22ms
memory: 23216kb
input:
526583 1344 500217637
output:
108767800
result:
ok 1 number(s): "108767800"
Test #36:
score: 0
Accepted
time: 23ms
memory: 23156kb
input:
867054 769 93998191
output:
239123369
result:
ok 1 number(s): "239123369"
Extra Test:
score: 0
Extra Test Passed