QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#333536 | #8211. Enumerating Substrings | nhuang685 | AC ✓ | 118ms | 34680kb | C++20 | 5.9kb | 2024-02-20 06:46:08 | 2024-02-20 06:46:08 |
Judging History
answer
/**
* @file qoj8211-1.cpp
* @author n685
* @brief
* @date 2024-02-19
*
*
*/
#include <bits/stdc++.h>
#ifdef LOCAL
#include "dd/debug.h"
#else
#define dbg(...) 42
#define dbgR(...) 4242
#define dbgP(...) 420
#define dbgRP(...) 420420
void nline() {}
#endif
template <class T> constexpr std::pair<T, T> exEucl(T a, T b) {
if (a < b) {
// auto [x, y] = exEucl(b, a);
T x, y;
std::tie(x, y) = exEucl(b, a);
return {y, x};
}
if (b == 0) {
assert(a == 1);
return {1, 0};
}
// auto [x, y] = exEucl(b, a % b);
T x, y;
std::tie(x, y) = exEucl(b, a % b);
return {y, x - (a / b) * y};
}
template <
class T, class U,
typename std::enable_if<std::is_integral<U>::value, bool>::type = true>
constexpr T binpow(T a, U b) {
// 0^0 = 1
T res = 1;
while (b > 0) {
if (b % 2 == 1) {
res *= a;
}
a *= a;
b /= 2;
}
return res;
}
template <class Md, class V = int64_t> struct Mod {
using T = typename std::decay<decltype(Md::value)>::type;
T val = 0;
template <class U> static constexpr T normalize(U val) {
if (val <= -Md::value || Md::value <= val) {
val %= Md::value;
}
if (val < 0) {
val += Md::value;
}
return static_cast<T>(val);
}
constexpr Mod() : val(0) {}
template <class U, typename std::enable_if<std::is_integral<U>::value,
bool>::type = true>
constexpr Mod(U _val) {
val = normalize(_val);
}
// addition
constexpr Mod &operator+=(Mod b) {
val += b.val;
if (val >= Md::value) {
val -= Md::value;
}
return *this;
}
friend constexpr Mod operator+(Mod a, Mod b) { return (a += b); }
constexpr Mod &operator++() { return (*this += 1); }
constexpr Mod operator++(int) {
Mod res = *this;
++(*this);
return res;
}
// subtraction
constexpr Mod &operator-=(Mod b) {
val -= b.val;
if (val < 0) {
val += Md::value;
}
return *this;
}
friend constexpr Mod operator-(Mod a, Mod b) { return (a -= b); }
constexpr Mod &operator--() { return (*this -= 1); }
constexpr Mod operator--(int) {
Mod res = *this;
--(*this);
return res;
}
// multiplication
constexpr Mod &operator*=(Mod b) {
val = static_cast<T>(static_cast<V>(val) * b.val % Md::value);
return *this;
}
friend constexpr Mod operator*(Mod a, Mod b) { return (a *= b); }
template <class U> constexpr Mod binpow(U b) const {
return ::binpow(*this, b);
}
constexpr Mod inv() const {
return Mod(exEucl(static_cast<V>(val), static_cast<V>(Md::value)).first);
// return binpow(Md::value - 2);
}
// comparison
constexpr bool operator==(Mod b) const { return (val == b.val); }
// constexpr auto operator<=>(const Mod &b) const = default;
constexpr bool operator!=(Mod b) const { return (val != b.val); }
constexpr bool operator<(Mod b) const { return (val < b.val); }
constexpr bool operator>(Mod b) const { return (val > b.val); }
constexpr bool operator<=(Mod b) const { return (val <= b.val); }
constexpr bool operator>=(Mod b) const { return (val >= b.val); }
// io
friend std::istream &operator>>(std::istream &in, Mod &a) {
V v;
in >> v;
a = Mod(v);
return in;
}
friend std::ostream &operator<<(std::ostream &out, const Mod &a) {
out << a.val;
return out;
}
// conversion
explicit constexpr operator T() const { return val; }
constexpr const T &operator()() const { return val; }
constexpr Mod operator-() const { return Mod(-val); }
};
constexpr int MOD = (int)1e9 + 7;
using Mint = Mod<std::integral_constant<std::decay<decltype(MOD)>::type, MOD>>;
std::vector<Mint> fac, ifac;
void init(int n) {
fac.assign(n + 1, 0);
ifac.assign(n + 1, 0);
fac[0] = 1;
for (int i = 1; i <= n; ++i) {
fac[i] = fac[i - 1] * i;
}
ifac.back() = fac.back().inv();
for (int i = n - 1; i >= 0; --i) {
ifac[i] = ifac[i + 1] * (i + 1);
}
}
Mint C(int n, int k) {
if (n < k || n < 0 || k < 0) {
return 0;
}
return fac[n] * ifac[n - k] * ifac[k];
}
Mint P(int n, int k) {
if (n < k || n < 0 || k < 0) {
return 0;
}
return fac[n] * ifac[n - k];
}
int main() {
#ifndef LOCAL
std::cin.tie(nullptr)->sync_with_stdio(false);
#endif
int n, m;
int64_t k;
std::cin >> n >> m >> k;
init(n);
std::vector<Mint> pk(n + 1);
pk[0] = 1;
for (int i = 1; i <= n; ++i) {
pk[i] = pk[i - 1] * k;
}
std::vector<Mint> kp(n + 1);
kp[0] = 1;
for (int i = 1; i <= n; ++i) {
kp[i] = kp[i - 1] * (k - i + 1);
}
auto ck = [&](int b) -> Mint {
if (b < 0 || k < b) {
return 0;
}
return kp[b] * ifac[b];
};
std::vector<std::vector<Mint>> dp(
m + 1, std::vector<Mint>(std::min<int64_t>(m, k) + 1));
dp[0][0] = 1;
for (int i = 1; i <= m; ++i) {
for (int j = 1; j <= (int)std::min<int64_t>(i, k); ++j) {
dp[i][j] = dp[i - 1][j - 1] * i;
if (i > 1) {
dp[i][j] += dp[i - 2][j - 1] * C(i, 2);
}
}
dp[i].resize(m + 1);
}
std::vector<Mint> num(m / 2 + 1);
for (int i = 1; i <= m / 2; ++i) {
int j = m - 2 * i;
Mint prod = 1;
for (int l = 0; l < i; ++l) {
prod *= k - l;
}
for (int d = 0; d <= j; ++d) {
num[i] += prod * dp[j][d] * ck(d);
if (k - d == 0) {
break;
}
prod *= Mint(k - d).inv() * (k - d - i);
}
}
Mint ans = 0;
{
Mint sum = 0;
for (int j = 1; j <= m; ++j) {
sum += dp[m][j] * ck(j);
}
for (int j = 1; j <= m / 2; ++j) {
sum -= num[j];
}
ans += sum * (n - m + 1) * pk[n - m];
}
for (int i = 1; i <= m / 2; ++i) {
int j = m - 2 * i;
for (int sz = m, mul = 1; sz <= n; sz += i + j, mul *= -1) {
ans += num[i] * mul * (n - sz + 1) * pk[n - sz];
}
}
std::cout << ans << '\n';
}
这程序好像有点Bug,我给组数据试试?
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3784kb
input:
4 2 3
output:
228
result:
ok 1 number(s): "228"
Test #2:
score: 0
Accepted
time: 107ms
memory: 34680kb
input:
999999 1999 12345678
output:
52352722
result:
ok 1 number(s): "52352722"
Test #3:
score: 0
Accepted
time: 0ms
memory: 3508kb
input:
7 4 2
output:
182
result:
ok 1 number(s): "182"
Test #4:
score: 0
Accepted
time: 0ms
memory: 3852kb
input:
4 3 4
output:
480
result:
ok 1 number(s): "480"
Test #5:
score: 0
Accepted
time: 0ms
memory: 3508kb
input:
3 1 1
output:
3
result:
ok 1 number(s): "3"
Test #6:
score: 0
Accepted
time: 0ms
memory: 3564kb
input:
5 5 1
output:
0
result:
ok 1 number(s): "0"
Test #7:
score: 0
Accepted
time: 0ms
memory: 3856kb
input:
7 4 3
output:
5784
result:
ok 1 number(s): "5784"
Test #8:
score: 0
Accepted
time: 0ms
memory: 3788kb
input:
5 2 4
output:
3932
result:
ok 1 number(s): "3932"
Test #9:
score: 0
Accepted
time: 0ms
memory: 3624kb
input:
8 2 2
output:
1522
result:
ok 1 number(s): "1522"
Test #10:
score: 0
Accepted
time: 0ms
memory: 3624kb
input:
8 1 2
output:
2048
result:
ok 1 number(s): "2048"
Test #11:
score: 0
Accepted
time: 0ms
memory: 3628kb
input:
7 5 3
output:
2430
result:
ok 1 number(s): "2430"
Test #12:
score: 0
Accepted
time: 0ms
memory: 3564kb
input:
10 4 3
output:
272004
result:
ok 1 number(s): "272004"
Test #13:
score: 0
Accepted
time: 4ms
memory: 15284kb
input:
675978 614 2
output:
0
result:
ok 1 number(s): "0"
Test #14:
score: 0
Accepted
time: 3ms
memory: 7100kb
input:
244613 38 1
output:
0
result:
ok 1 number(s): "0"
Test #15:
score: 0
Accepted
time: 5ms
memory: 14536kb
input:
186293 1462 1
output:
0
result:
ok 1 number(s): "0"
Test #16:
score: 0
Accepted
time: 2ms
memory: 6508kb
input:
24867 886 1
output:
0
result:
ok 1 number(s): "0"
Test #17:
score: 0
Accepted
time: 25ms
memory: 22540kb
input:
976164 1014 2
output:
0
result:
ok 1 number(s): "0"
Test #18:
score: 0
Accepted
time: 5ms
memory: 5984kb
input:
179356 2 716844809
output:
577866092
result:
ok 1 number(s): "577866092"
Test #19:
score: 0
Accepted
time: 18ms
memory: 12740kb
input:
621001 130 310625363
output:
892869197
result:
ok 1 number(s): "892869197"
Test #20:
score: 0
Accepted
time: 24ms
memory: 16512kb
input:
678862 850 754662812
output:
582264789
result:
ok 1 number(s): "582264789"
Test #21:
score: 0
Accepted
time: 42ms
memory: 17064kb
input:
650845 978 348443366
output:
825425732
result:
ok 1 number(s): "825425732"
Test #22:
score: 0
Accepted
time: 20ms
memory: 14328kb
input:
669914 402 87448112
output:
318098088
result:
ok 1 number(s): "318098088"
Test #23:
score: 0
Accepted
time: 37ms
memory: 19940kb
input:
998593 530 681228665
output:
408255654
result:
ok 1 number(s): "408255654"
Test #24:
score: 0
Accepted
time: 113ms
memory: 24060kb
input:
369361 1954 125266115
output:
509912384
result:
ok 1 number(s): "509912384"
Test #25:
score: 0
Accepted
time: 84ms
memory: 24836kb
input:
900226 1378 424079373
output:
406320917
result:
ok 1 number(s): "406320917"
Test #26:
score: 0
Accepted
time: 65ms
memory: 17464kb
input:
334887 1506 17859926
output:
503264679
result:
ok 1 number(s): "503264679"
Test #27:
score: 0
Accepted
time: 29ms
memory: 19032kb
input:
936048 544 53978328
output:
548647866
result:
ok 1 number(s): "548647866"
Test #28:
score: 0
Accepted
time: 53ms
memory: 11788kb
input:
152789 1264 792983073
output:
839541707
result:
ok 1 number(s): "839541707"
Test #29:
score: 0
Accepted
time: 68ms
memory: 22000kb
input:
714493 1392 91796331
output:
721071046
result:
ok 1 number(s): "721071046"
Test #30:
score: 0
Accepted
time: 30ms
memory: 10068kb
input:
269571 816 830801077
output:
330064211
result:
ok 1 number(s): "330064211"
Test #31:
score: 0
Accepted
time: 46ms
memory: 19872kb
input:
845120 944 424581630
output:
348960190
result:
ok 1 number(s): "348960190"
Test #32:
score: 0
Accepted
time: 15ms
memory: 12044kb
input:
533990 368 163586376
output:
522092095
result:
ok 1 number(s): "522092095"
Test #33:
score: 0
Accepted
time: 117ms
memory: 18712kb
input:
181707 1792 462399634
output:
373795106
result:
ok 1 number(s): "373795106"
Test #34:
score: 0
Accepted
time: 118ms
memory: 24196kb
input:
417349 1920 761212891
output:
587051329
result:
ok 1 number(s): "587051329"
Test #35:
score: 0
Accepted
time: 59ms
memory: 18356kb
input:
526583 1344 500217637
output:
108767800
result:
ok 1 number(s): "108767800"
Test #36:
score: 0
Accepted
time: 40ms
memory: 18952kb
input:
867054 769 93998191
output:
239123369
result:
ok 1 number(s): "239123369"
Extra Test:
score: 0
Extra Test Passed