QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#321162 | #8211. Enumerating Substrings | ucup-team008# | AC ✓ | 1418ms | 18996kb | C++17 | 8.5kb | 2024-02-04 05:43:14 | 2024-02-04 05:43:14 |
Judging History
answer
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <unordered_set>
#include <vector>
using namespace std;
// BEGIN NO SAD
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define trav(a, x) for(auto& a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
typedef vector<int> vi;
#define f first
#define s second
#define derr if(1) cerr
void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ", "; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? ", " : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#define debug(x...) cerr << "\e[91m"<<__func__<<":"<<__LINE__<<" [" << #x << "] = ["; _print(x); cerr << "\e[39m" << flush;
// END NO SAD
template<class Fun>
class y_combinator_result {
Fun fun_;
public:
template<class T>
explicit y_combinator_result(T &&fun): fun_(std::forward<T>(fun)) {}
template<class ...Args>
decltype(auto) operator()(Args &&...args) {
return fun_(std::ref(*this), std::forward<Args>(args)...);
}
};
template<class Fun>
decltype(auto) y_combinator(Fun &&fun) {
return y_combinator_result<std::decay_t<Fun>>(std::forward<Fun>(fun));
}
template<class T>
bool updmin(T& a, T b) {
if(b < a) {
a = b;
return true;
}
return false;
}
template<class T>
bool updmax(T& a, T b) {
if(b > a) {
a = b;
return true;
}
return false;
}
typedef int64_t ll;
struct barrett_reduction {
unsigned mod;
uint64_t div;
barrett_reduction(unsigned m) : mod(m), div(-1LLU / m) {}
unsigned operator()(uint64_t a) const {
#ifdef __SIZEOF_INT128__
uint64_t q = uint64_t(__uint128_t(div) * a >> 64);
uint64_t r = a - q * mod;
return unsigned(r < mod ? r : r - mod);
#endif
return unsigned(a % mod);
}
};
template<const int &MOD, const barrett_reduction &barrett>
struct _b_int {
int val;
_b_int(int64_t v = 0) {
if (v < 0) v = v % MOD + MOD;
if (v >= MOD) v %= MOD;
val = int(v);
}
_b_int(uint64_t v) {
if (v >= uint64_t(MOD)) v %= MOD;
val = int(v);
}
_b_int(int v) : _b_int(int64_t(v)) {}
_b_int(unsigned v) : _b_int(uint64_t(v)) {}
static int inv_mod(int a, int m = MOD) {
// https://en.wikipedia.org/wiki/Extended_Euclidean_algorithm#Example
int g = m, r = a, x = 0, y = 1;
while (r != 0) {
int q = g / r;
g %= r; swap(g, r);
x -= q * y; swap(x, y);
}
return x < 0 ? x + m : x;
}
explicit operator int() const { return val; }
explicit operator unsigned() const { return val; }
explicit operator int64_t() const { return val; }
explicit operator uint64_t() const { return val; }
explicit operator double() const { return val; }
explicit operator long double() const { return val; }
_b_int& operator+=(const _b_int &other) {
val -= MOD - other.val;
if (val < 0) val += MOD;
return *this;
}
_b_int& operator-=(const _b_int &other) {
val -= other.val;
if (val < 0) val += MOD;
return *this;
}
static unsigned fast_mod(uint64_t x) {
#if !defined(_WIN32) || defined(_WIN64)
return barrett(x);
#endif
// Optimized mod for Codeforces 32-bit machines.
// x must be less than 2^32 * MOD for this to work, so that x / MOD fits in an unsigned 32-bit int.
unsigned x_high = unsigned(x >> 32), x_low = unsigned(x);
unsigned quot, rem;
asm("divl %4\n"
: "=a" (quot), "=d" (rem)
: "d" (x_high), "a" (x_low), "r" (MOD));
return rem;
}
_b_int& operator*=(const _b_int &other) {
val = fast_mod(uint64_t(val) * other.val);
return *this;
}
_b_int& operator/=(const _b_int &other) {
return *this *= other.inv();
}
friend _b_int operator+(const _b_int &a, const _b_int &b) { return _b_int(a) += b; }
friend _b_int operator-(const _b_int &a, const _b_int &b) { return _b_int(a) -= b; }
friend _b_int operator*(const _b_int &a, const _b_int &b) { return _b_int(a) *= b; }
friend _b_int operator/(const _b_int &a, const _b_int &b) { return _b_int(a) /= b; }
_b_int& operator++() {
val = val == MOD - 1 ? 0 : val + 1;
return *this;
}
_b_int& operator--() {
val = val == 0 ? MOD - 1 : val - 1;
return *this;
}
_b_int operator++(int) { _b_int before = *this; ++*this; return before; }
_b_int operator--(int) { _b_int before = *this; --*this; return before; }
_b_int operator-() const {
return val == 0 ? 0 : MOD - val;
}
friend bool operator==(const _b_int &a, const _b_int &b) { return a.val == b.val; }
friend bool operator!=(const _b_int &a, const _b_int &b) { return a.val != b.val; }
friend bool operator<(const _b_int &a, const _b_int &b) { return a.val < b.val; }
friend bool operator>(const _b_int &a, const _b_int &b) { return a.val > b.val; }
friend bool operator<=(const _b_int &a, const _b_int &b) { return a.val <= b.val; }
friend bool operator>=(const _b_int &a, const _b_int &b) { return a.val >= b.val; }
_b_int inv() const {
return inv_mod(val);
}
_b_int pow(int64_t p) const {
if (p < 0)
return inv().pow(-p);
_b_int a = *this, result = 1;
while (p > 0) {
if (p & 1)
result *= a;
p >>= 1;
if (p > 0)
a *= a;
}
return result;
}
friend ostream& operator<<(ostream &os, const _b_int &m) {
return os << m.val;
}
friend istream& operator>>(istream &is, _b_int &m) {
int64_t x;
is >> x;
m = x;
return is;
}
};
int MOD = int(1e9) + 7;
barrett_reduction barrett(MOD);
using mnum = _b_int<MOD, barrett>;
void solve() {
int n, m, k;
cin >> n >> m >> k;
vector<vector<mnum>> wordcnt(m+1);
for(int i = 0; i <= m; i++) wordcnt[i].resize(m+1);
wordcnt[0][0] = 1;
for(int i = 0; i < m; i++) {
for(int j = 0; j <= i; j++) {
int once = 2*j-i;
int zero = k-j;
if(zero > 0) wordcnt[i+1][j+1] += wordcnt[i][j] * zero;
if(once > 0) wordcnt[i+1][j] += wordcnt[i][j] * once;
}
}
mnum ret = 0;
mnum total = 0;
for(int i = 0; i <= m; i++) {
mnum scale = wordcnt[m][i];
total += scale;
}
for(int innerlen = 0; innerlen < m; innerlen++) {
if(m%2 != innerlen%2) continue;
int blen = (m-innerlen)/2;
mnum distinctwords = 0;
for(int d = 0; d <= innerlen; d++) {
mnum curr = wordcnt[innerlen][d];
for(int i = 0; i < blen; i++) curr *= (k-d-i);
distinctwords += curr;
}
total -= distinctwords;
mnum inner = 0;
for(int a = 1; true; a++) {
int candlen = (a+1) * blen + a * innerlen;
if(candlen > n) break;
mnum scale = (n-candlen+1) * mnum(k).pow(n-candlen);
if(a%2) inner += scale;
else inner -= scale;
}
ret += inner * distinctwords;
}
// no border
mnum scale = (n-m+1) * mnum(k).pow(n-m);
scale *= total;
ret += scale;
cout << ret << "\n";
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
solve();
}
这程序好像有点Bug,我给组数据试试?
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3740kb
input:
4 2 3
output:
228
result:
ok 1 number(s): "228"
Test #2:
score: 0
Accepted
time: 1418ms
memory: 18996kb
input:
999999 1999 12345678
output:
52352722
result:
ok 1 number(s): "52352722"
Test #3:
score: 0
Accepted
time: 0ms
memory: 3600kb
input:
7 4 2
output:
182
result:
ok 1 number(s): "182"
Test #4:
score: 0
Accepted
time: 0ms
memory: 3612kb
input:
4 3 4
output:
480
result:
ok 1 number(s): "480"
Test #5:
score: 0
Accepted
time: 0ms
memory: 3616kb
input:
3 1 1
output:
3
result:
ok 1 number(s): "3"
Test #6:
score: 0
Accepted
time: 0ms
memory: 3744kb
input:
5 5 1
output:
0
result:
ok 1 number(s): "0"
Test #7:
score: 0
Accepted
time: 1ms
memory: 3548kb
input:
7 4 3
output:
5784
result:
ok 1 number(s): "5784"
Test #8:
score: 0
Accepted
time: 0ms
memory: 3532kb
input:
5 2 4
output:
3932
result:
ok 1 number(s): "3932"
Test #9:
score: 0
Accepted
time: 0ms
memory: 3816kb
input:
8 2 2
output:
1522
result:
ok 1 number(s): "1522"
Test #10:
score: 0
Accepted
time: 0ms
memory: 3528kb
input:
8 1 2
output:
2048
result:
ok 1 number(s): "2048"
Test #11:
score: 0
Accepted
time: 0ms
memory: 3600kb
input:
7 5 3
output:
2430
result:
ok 1 number(s): "2430"
Test #12:
score: 0
Accepted
time: 0ms
memory: 3536kb
input:
10 4 3
output:
272004
result:
ok 1 number(s): "272004"
Test #13:
score: 0
Accepted
time: 91ms
memory: 4648kb
input:
675978 614 2
output:
0
result:
ok 1 number(s): "0"
Test #14:
score: 0
Accepted
time: 14ms
memory: 3532kb
input:
244613 38 1
output:
0
result:
ok 1 number(s): "0"
Test #15:
score: 0
Accepted
time: 575ms
memory: 11584kb
input:
186293 1462 1
output:
0
result:
ok 1 number(s): "0"
Test #16:
score: 0
Accepted
time: 127ms
memory: 6248kb
input:
24867 886 1
output:
0
result:
ok 1 number(s): "0"
Test #17:
score: 0
Accepted
time: 249ms
memory: 7312kb
input:
976164 1014 2
output:
0
result:
ok 1 number(s): "0"
Test #18:
score: 0
Accepted
time: 14ms
memory: 3548kb
input:
179356 2 716844809
output:
577866092
result:
ok 1 number(s): "577866092"
Test #19:
score: 0
Accepted
time: 38ms
memory: 3600kb
input:
621001 130 310625363
output:
892869197
result:
ok 1 number(s): "892869197"
Test #20:
score: 0
Accepted
time: 147ms
memory: 6000kb
input:
678862 850 754662812
output:
582264789
result:
ok 1 number(s): "582264789"
Test #21:
score: 0
Accepted
time: 200ms
memory: 7048kb
input:
650845 978 348443366
output:
825425732
result:
ok 1 number(s): "825425732"
Test #22:
score: 0
Accepted
time: 52ms
memory: 3932kb
input:
669914 402 87448112
output:
318098088
result:
ok 1 number(s): "318098088"
Test #23:
score: 0
Accepted
time: 88ms
memory: 4080kb
input:
998593 530 681228665
output:
408255654
result:
ok 1 number(s): "408255654"
Test #24:
score: 0
Accepted
time: 1287ms
memory: 18128kb
input:
369361 1954 125266115
output:
509912384
result:
ok 1 number(s): "509912384"
Test #25:
score: 0
Accepted
time: 498ms
memory: 10736kb
input:
900226 1378 424079373
output:
406320917
result:
ok 1 number(s): "406320917"
Test #26:
score: 0
Accepted
time: 593ms
memory: 12016kb
input:
334887 1506 17859926
output:
503264679
result:
ok 1 number(s): "503264679"
Test #27:
score: 0
Accepted
time: 89ms
memory: 4416kb
input:
936048 544 53978328
output:
548647866
result:
ok 1 number(s): "548647866"
Test #28:
score: 0
Accepted
time: 352ms
memory: 9356kb
input:
152789 1264 792983073
output:
839541707
result:
ok 1 number(s): "839541707"
Test #29:
score: 0
Accepted
time: 499ms
memory: 10688kb
input:
714493 1392 91796331
output:
721071046
result:
ok 1 number(s): "721071046"
Test #30:
score: 0
Accepted
time: 105ms
memory: 5680kb
input:
269571 816 830801077
output:
330064211
result:
ok 1 number(s): "330064211"
Test #31:
score: 0
Accepted
time: 196ms
memory: 6732kb
input:
845120 944 424581630
output:
348960190
result:
ok 1 number(s): "348960190"
Test #32:
score: 0
Accepted
time: 41ms
memory: 3932kb
input:
533990 368 163586376
output:
522092095
result:
ok 1 number(s): "522092095"
Test #33:
score: 0
Accepted
time: 989ms
memory: 15696kb
input:
181707 1792 462399634
output:
373795106
result:
ok 1 number(s): "373795106"
Test #34:
score: 0
Accepted
time: 1221ms
memory: 17616kb
input:
417349 1920 761212891
output:
587051329
result:
ok 1 number(s): "587051329"
Test #35:
score: 0
Accepted
time: 442ms
memory: 10280kb
input:
526583 1344 500217637
output:
108767800
result:
ok 1 number(s): "108767800"
Test #36:
score: 0
Accepted
time: 129ms
memory: 5464kb
input:
867054 769 93998191
output:
239123369
result:
ok 1 number(s): "239123369"
Extra Test:
score: 0
Extra Test Passed