QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#320993 | #8211. Enumerating Substrings | ucup-team087# | AC ✓ | 30ms | 30644kb | C++14 | 6.0kb | 2024-02-04 01:46:35 | 2024-02-04 01:46:35 |
Judging History
answer
#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
using Int = long long;
template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; };
template <class T> ostream &operator<<(ostream &os, const vector<T> &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; }
template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; }
template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; }
template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; }
#define COLOR(s) ("\x1b[" s "m")
////////////////////////////////////////////////////////////////////////////////
template <unsigned M_> struct ModInt {
static constexpr unsigned M = M_;
unsigned x;
constexpr ModInt() : x(0U) {}
constexpr ModInt(unsigned x_) : x(x_ % M) {}
constexpr ModInt(unsigned long long x_) : x(x_ % M) {}
constexpr ModInt(int x_) : x(((x_ %= static_cast<int>(M)) < 0) ? (x_ + static_cast<int>(M)) : x_) {}
constexpr ModInt(long long x_) : x(((x_ %= static_cast<long long>(M)) < 0) ? (x_ + static_cast<long long>(M)) : x_) {}
ModInt &operator+=(const ModInt &a) { x = ((x += a.x) >= M) ? (x - M) : x; return *this; }
ModInt &operator-=(const ModInt &a) { x = ((x -= a.x) >= M) ? (x + M) : x; return *this; }
ModInt &operator*=(const ModInt &a) { x = (static_cast<unsigned long long>(x) * a.x) % M; return *this; }
ModInt &operator/=(const ModInt &a) { return (*this *= a.inv()); }
ModInt pow(long long e) const {
if (e < 0) return inv().pow(-e);
ModInt a = *this, b = 1U; for (; e; e >>= 1) { if (e & 1) b *= a; a *= a; } return b;
}
ModInt inv() const {
unsigned a = M, b = x; int y = 0, z = 1;
for (; b; ) { const unsigned q = a / b; const unsigned c = a - q * b; a = b; b = c; const int w = y - static_cast<int>(q) * z; y = z; z = w; }
assert(a == 1U); return ModInt(y);
}
ModInt operator+() const { return *this; }
ModInt operator-() const { ModInt a; a.x = x ? (M - x) : 0U; return a; }
ModInt operator+(const ModInt &a) const { return (ModInt(*this) += a); }
ModInt operator-(const ModInt &a) const { return (ModInt(*this) -= a); }
ModInt operator*(const ModInt &a) const { return (ModInt(*this) *= a); }
ModInt operator/(const ModInt &a) const { return (ModInt(*this) /= a); }
template <class T> friend ModInt operator+(T a, const ModInt &b) { return (ModInt(a) += b); }
template <class T> friend ModInt operator-(T a, const ModInt &b) { return (ModInt(a) -= b); }
template <class T> friend ModInt operator*(T a, const ModInt &b) { return (ModInt(a) *= b); }
template <class T> friend ModInt operator/(T a, const ModInt &b) { return (ModInt(a) /= b); }
explicit operator bool() const { return x; }
bool operator==(const ModInt &a) const { return (x == a.x); }
bool operator!=(const ModInt &a) const { return (x != a.x); }
friend std::ostream &operator<<(std::ostream &os, const ModInt &a) { return os << a.x; }
};
////////////////////////////////////////////////////////////////////////////////
constexpr unsigned MO = 1000000007;
using Mint = ModInt<MO>;
constexpr int LIM_INV = 2'000'010;
Mint inv[LIM_INV], fac[LIM_INV], invFac[LIM_INV];
void prepare() {
inv[1] = 1;
for (int i = 2; i < LIM_INV; ++i) {
inv[i] = -((Mint::M / i) * inv[Mint::M % i]);
}
fac[0] = invFac[0] = 1;
for (int i = 1; i < LIM_INV; ++i) {
fac[i] = fac[i - 1] * i;
invFac[i] = invFac[i - 1] * inv[i];
}
}
Mint binom(Int n, Int k) {
if (n < 0) {
if (k >= 0) {
return ((k & 1) ? -1 : +1) * binom(-n + k - 1, k);
} else if (n - k >= 0) {
return (((n - k) & 1) ? -1 : +1) * binom(-k - 1, n - k);
} else {
return 0;
}
} else {
if (0 <= k && k <= n) {
assert(n < LIM_INV);
return fac[n] * invFac[k] * invFac[n - k];
} else {
return 0;
}
}
}
int main() {
prepare();
int N, M, K;
for (; ~scanf("%d%d%d", &N, &M, &K); ) {
// x^M / (1-Kx)^2
vector<Mint> hs(N + 1, 0);
hs[M] = 1;
for (int i = 1; i <= N; ++i) hs[i] += hs[i - 1] * K;
for (int i = 1; i <= N; ++i) hs[i] += hs[i - 1] * K;
// cerr<<"hs = "<<hs<<endl;
vector<Mint> invTwo(M + 1);
invTwo[0] = 1;
for (int i = 1; i <= M; ++i) {
invTwo[i] = invTwo[i - 1] * inv[2];
}
vector<Mint> under(M + 1);
under[0] = 1;
for (int i = 0; i < M; ++i) {
under[i + 1] = under[i] * Mint(K - i);
}
Mint all0 = 0;
for (int b = 0; b <= M/2; ++b) {
const int a = M - 2*b;
all0 += under[a + b] * invFac[a] * invFac[b] * fac[a + 2*b] * invTwo[b];
}
vector<Mint> ways(M/2 + 1, 0);
Mint way0 = all0;
for (int m = 1; m <= M/2; ++m) {
for (int b = 0; b <= (M-2*m)/2; ++b) {
const int a = (M-2*m) - 2*b;
ways[m] += under[m + a + b] * invFac[a] * invFac[b] * fac[a + 2*b] * invTwo[b];
}
way0 -= ways[m];
}
// cerr<<"under = "<<under<<endl;
// cerr<<"all0 = "<<all0<<endl;
// cerr<<"ways = "<<ways<<endl;
// cerr<<"way0 = "<<way0<<endl;
Mint ans = 0;
ans += way0 * hs[N];
for (int m = 1; m <= M/2; ++m) {
// * 1 / (1+x^(M-m))
Mint sum = 0;
for (int j = 0; (M-m) * j <= N; ++j) {
sum += (j&1?-1:+1) * hs[N - (M-m) * j];
}
ans += ways[m] * sum;
}
printf("%u\n", ans.x);
}
return 0;
}
这程序好像有点Bug,我给组数据试试?
详细
Test #1:
score: 100
Accepted
time: 13ms
memory: 27260kb
input:
4 2 3
output:
228
result:
ok 1 number(s): "228"
Test #2:
score: 0
Accepted
time: 30ms
memory: 30536kb
input:
999999 1999 12345678
output:
52352722
result:
ok 1 number(s): "52352722"
Test #3:
score: 0
Accepted
time: 12ms
memory: 27264kb
input:
7 4 2
output:
182
result:
ok 1 number(s): "182"
Test #4:
score: 0
Accepted
time: 20ms
memory: 27232kb
input:
4 3 4
output:
480
result:
ok 1 number(s): "480"
Test #5:
score: 0
Accepted
time: 10ms
memory: 27264kb
input:
3 1 1
output:
3
result:
ok 1 number(s): "3"
Test #6:
score: 0
Accepted
time: 16ms
memory: 27208kb
input:
5 5 1
output:
0
result:
ok 1 number(s): "0"
Test #7:
score: 0
Accepted
time: 16ms
memory: 27152kb
input:
7 4 3
output:
5784
result:
ok 1 number(s): "5784"
Test #8:
score: 0
Accepted
time: 16ms
memory: 27204kb
input:
5 2 4
output:
3932
result:
ok 1 number(s): "3932"
Test #9:
score: 0
Accepted
time: 16ms
memory: 27548kb
input:
8 2 2
output:
1522
result:
ok 1 number(s): "1522"
Test #10:
score: 0
Accepted
time: 20ms
memory: 27232kb
input:
8 1 2
output:
2048
result:
ok 1 number(s): "2048"
Test #11:
score: 0
Accepted
time: 20ms
memory: 27560kb
input:
7 5 3
output:
2430
result:
ok 1 number(s): "2430"
Test #12:
score: 0
Accepted
time: 12ms
memory: 27208kb
input:
10 4 3
output:
272004
result:
ok 1 number(s): "272004"
Test #13:
score: 0
Accepted
time: 26ms
memory: 29456kb
input:
675978 614 2
output:
0
result:
ok 1 number(s): "0"
Test #14:
score: 0
Accepted
time: 15ms
memory: 27668kb
input:
244613 38 1
output:
0
result:
ok 1 number(s): "0"
Test #15:
score: 0
Accepted
time: 20ms
memory: 27344kb
input:
186293 1462 1
output:
0
result:
ok 1 number(s): "0"
Test #16:
score: 0
Accepted
time: 21ms
memory: 27328kb
input:
24867 886 1
output:
0
result:
ok 1 number(s): "0"
Test #17:
score: 0
Accepted
time: 27ms
memory: 30512kb
input:
976164 1014 2
output:
0
result:
ok 1 number(s): "0"
Test #18:
score: 0
Accepted
time: 23ms
memory: 27784kb
input:
179356 2 716844809
output:
577866092
result:
ok 1 number(s): "577866092"
Test #19:
score: 0
Accepted
time: 22ms
memory: 29200kb
input:
621001 130 310625363
output:
892869197
result:
ok 1 number(s): "892869197"
Test #20:
score: 0
Accepted
time: 26ms
memory: 29436kb
input:
678862 850 754662812
output:
582264789
result:
ok 1 number(s): "582264789"
Test #21:
score: 0
Accepted
time: 30ms
memory: 29152kb
input:
650845 978 348443366
output:
825425732
result:
ok 1 number(s): "825425732"
Test #22:
score: 0
Accepted
time: 25ms
memory: 29508kb
input:
669914 402 87448112
output:
318098088
result:
ok 1 number(s): "318098088"
Test #23:
score: 0
Accepted
time: 22ms
memory: 30644kb
input:
998593 530 681228665
output:
408255654
result:
ok 1 number(s): "408255654"
Test #24:
score: 0
Accepted
time: 27ms
memory: 27972kb
input:
369361 1954 125266115
output:
509912384
result:
ok 1 number(s): "509912384"
Test #25:
score: 0
Accepted
time: 30ms
memory: 30192kb
input:
900226 1378 424079373
output:
406320917
result:
ok 1 number(s): "406320917"
Test #26:
score: 0
Accepted
time: 23ms
memory: 28028kb
input:
334887 1506 17859926
output:
503264679
result:
ok 1 number(s): "503264679"
Test #27:
score: 0
Accepted
time: 29ms
memory: 30372kb
input:
936048 544 53978328
output:
548647866
result:
ok 1 number(s): "548647866"
Test #28:
score: 0
Accepted
time: 23ms
memory: 27568kb
input:
152789 1264 792983073
output:
839541707
result:
ok 1 number(s): "839541707"
Test #29:
score: 0
Accepted
time: 28ms
memory: 29356kb
input:
714493 1392 91796331
output:
721071046
result:
ok 1 number(s): "721071046"
Test #30:
score: 0
Accepted
time: 21ms
memory: 27784kb
input:
269571 816 830801077
output:
330064211
result:
ok 1 number(s): "330064211"
Test #31:
score: 0
Accepted
time: 28ms
memory: 29856kb
input:
845120 944 424581630
output:
348960190
result:
ok 1 number(s): "348960190"
Test #32:
score: 0
Accepted
time: 20ms
memory: 28664kb
input:
533990 368 163586376
output:
522092095
result:
ok 1 number(s): "522092095"
Test #33:
score: 0
Accepted
time: 16ms
memory: 27348kb
input:
181707 1792 462399634
output:
373795106
result:
ok 1 number(s): "373795106"
Test #34:
score: 0
Accepted
time: 24ms
memory: 28312kb
input:
417349 1920 761212891
output:
587051329
result:
ok 1 number(s): "587051329"
Test #35:
score: 0
Accepted
time: 26ms
memory: 28576kb
input:
526583 1344 500217637
output:
108767800
result:
ok 1 number(s): "108767800"
Test #36:
score: 0
Accepted
time: 26ms
memory: 30284kb
input:
867054 769 93998191
output:
239123369
result:
ok 1 number(s): "239123369"
Extra Test:
score: 0
Extra Test Passed