QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#354979 | #8325. 重塑时光 | skittles1412# | 50 | 1120ms | 3868kb | C++17 | 9.1kb | 2024-03-16 10:09:51 | 2024-07-04 03:29:50 |
Judging History
answer
#include "bits/extc++.h"
using namespace std;
template <typename T, typename... U>
void dbgh(const T& t, const U&... u) {
cerr << t;
((cerr << " | " << u), ...);
cerr << endl;
}
#ifdef DEBUG
#define dbg(...) \
cerr << "L" << __LINE__ << " [" << #__VA_ARGS__ << "]" \
<< ": "; \
dbgh(__VA_ARGS__)
#else
#define cerr \
if (false) \
cerr
#define dbg(...)
#endif
#define endl "\n"
#define long int64_t
#define sz(x) int(std::size(x))
template <typename T>
ostream& operator<<(ostream& out, const vector<T>& arr) {
out << "[";
for (int i = 0; i < sz(arr); i++) {
if (i) {
out << ", ";
}
out << arr[i];
}
return out << "]";
}
bool on(int mask, int bit) {
return (mask >> bit) & 1;
}
struct mint {
static constexpr int MOD = 1e9 + 7;
static int norm(long x) {
x %= MOD;
if (x < 0) {
x += MOD;
}
return x;
}
static mint pow(mint base, long exp) {
mint ans = 1;
while (exp) {
if (exp & 1) {
ans *= base;
}
base *= base;
exp >>= 1;
}
return ans;
}
static mint inv(mint x) {
return mint::pow(x, MOD - 2);
}
int x;
mint() : x(0) {}
mint(long x) : x(norm(x)) {}
friend ostream& operator<<(ostream& out, const mint& m) {
return out << m.x;
}
static mint from_unchecked(int x) {
mint m;
m.x = x;
return m;
}
mint operator-() const {
if (!x) {
return {};
}
return mint::from_unchecked(MOD - x);
}
mint& operator+=(const mint& m) {
x += m.x;
if (x >= MOD) {
x -= MOD;
}
return *this;
}
mint& operator-=(const mint& m) {
return *this += -m;
}
mint& operator*=(const mint& m) {
x = int(long(x) * m.x % MOD);
return *this;
}
mint& operator/=(const mint& m) {
return *this *= mint::inv(m);
}
friend mint operator+(mint a, const mint& b) {
return a += b;
}
friend mint operator-(mint a, const mint& b) {
return a -= b;
}
friend mint operator*(mint a, const mint& b) {
return a *= b;
}
friend mint operator/(mint a, const mint& b) {
return a /= b;
}
};
struct Partition {
static constexpr int MAXN = 17;
vector<vector<int>> memo[MAXN][MAXN];
vector<vector<int>> dp(int n, int m) {
if (!n) {
return {{}};
} else if (n < m) {
return {};
}
auto& ans = memo[n][m];
if (sz(ans)) {
return ans;
}
for (int i = m; i <= n; i++) {
auto cur = dp(n - i, i);
for (auto& a : cur) {
a.insert(a.begin(), i);
}
ans.insert(ans.end(), begin(cur), end(cur));
}
return ans;
}
vector<vector<int>> solve(int n) {
return dp(n, 1);
}
} P;
struct M {
static constexpr int MAXN = 100;
mint fact[MAXN], ifact[MAXN];
M() {
fact[0] = ifact[0] = 1;
for (int i = 1; i < MAXN; i++) {
fact[i] = fact[i - 1] * i;
}
ifact[MAXN - 1] = mint::inv(fact[MAXN - 1]);
for (int i = MAXN - 2; i >= 1; i--) {
ifact[i] = (i + 1) * ifact[i + 1];
}
}
mint choose(int n, int k) const {
return fact[n] * ifact[k] * ifact[n - k];
}
mint choose2(int n, int k) const {
if (n < 0 || k < 0 || k > n) {
return 0;
}
return choose(n, k);
}
} M;
namespace s1 {
bool solve(int, int, const vector<pair<int, int>>& edges) {
if (!sz(edges)) {
cout << 1 << endl;
return true;
}
return false;
}
} // namespace s1
namespace s2 {
bool solve(int n, int kv, const vector<pair<int, int>>& edges) {
if (kv) {
return false;
}
vector<int> graph[n];
for (auto& [u, v] : edges) {
graph[u].push_back(v);
}
mint dp[1 << n] {};
dp[(1 << n) - 1] = 1;
for (int mask = (1 << n) - 2; mask >= 0; mask--) {
for (int i = 0; i < n; i++) {
if (on(mask, i)) {
continue;
}
bool bad = false;
for (auto& v : graph[i]) {
bad |= on(mask, v);
}
if (!bad) {
dp[mask] += dp[mask | (1 << i)];
}
}
}
cout << dp[0] / M.fact[n] << endl;
return true;
}
} // namespace s2
namespace s3 {
constexpr int MAXN = 20;
vector<int> graph[MAXN];
int vis[MAXN];
bool has_cyc;
void dfs(int u) {
if (vis[u]) {
if (vis[u] == 1) {
has_cyc = true;
}
return;
}
vis[u] = 1;
for (auto& v : graph[u]) {
dfs(v);
}
vis[u] = 2;
}
bool solve(int n, int kv, const vector<pair<int, int>>& edges) {
if (n > 9) {
return false;
}
mint ans = 0;
for (auto& part : P.solve(n)) {
int perm[n];
iota(perm, perm + n, 0);
do {
int id[n];
vector<int> bounds {0};
for (auto& a : part) {
int cl = bounds.back(), cr = cl + a;
for (int i = cl; i < cr; i++) {
id[perm[i]] = sz(bounds) - 1;
}
bounds.push_back(cr);
}
bool bad = false;
for (int i = 0; i < sz(part) - 1; i++) {
bad |= part[i] == part[i + 1] &&
perm[bounds[i]] > perm[bounds[i + 1]];
}
if (bad) {
continue;
}
fill(vis, vis + n, false);
for (int i = 0; i < n; i++) {
graph[i].clear();
}
has_cyc = false;
int iperm[n];
for (int i = 0; i < n; i++) {
iperm[perm[i]] = i;
}
for (auto& [u, v] : edges) {
if (id[u] == id[v]) {
has_cyc |= iperm[u] > iperm[v];
continue;
}
graph[id[u]].push_back(id[v]);
}
for (int i = 0; i < sz(part); i++) {
dfs(i);
}
if (has_cyc) {
continue;
}
int a = sz(part), ck = kv;
mint cans = M.fact[a];
ck -= a - 1;
cans *= M.choose2(ck + a, a);
ans += cans;
} while (next_permutation(perm, perm + n));
}
cout << ans / (M.fact[n] * M.choose(n + kv, kv)) << endl;
return true;
}
} // namespace s3
namespace s4 {
bool solve(int n, int kv, const vector<pair<int, int>>& edges) {
if (sz(edges) != n * (n - 1) / 2) {
return false;
}
mint ans = 0;
for (auto& part : P.solve(n)) {
mint ways = M.fact[sz(part)];
map<int, int> cnt;
for (auto& a : part) {
cnt[a]++;
}
for (auto& [_k, v] : cnt) {
ways /= M.fact[v];
}
int a = sz(part), ck = kv;
mint cans = ways * M.fact[a];
ck -= a - 1;
cans *= M.choose2(ck + a, a);
ans += cans;
}
cout << ans / (M.fact[n] * M.choose(n + kv, kv)) << endl;
return true;
}
} // namespace s4
namespace s5 {
bool solve(int n, int kv, const vector<pair<int, int>>& edges) {
if (sz(edges) != n - 1) {
return false;
}
for (auto& [u, v] : edges) {
if (u) {
return false;
}
}
mint ans = 0;
auto get_ways = [&](int a) -> mint {
int ck = kv;
mint cans = M.fact[a];
ck -= a - 1;
return cans * M.choose2(ck + a, a);
};
for (auto& part : P.solve(n - 1)) {
mint ways = M.fact[n - 1];
map<int, int> cnt;
for (auto& a : part) {
cnt[a]++;
}
for (auto& [_k, v] : cnt) {
ways /= M.fact[v];
}
ans += ways * get_ways(sz(part)) * sz(part);
ans += ways * get_ways(sz(part) + 1);
}
cout << ans / (M.fact[n] * M.choose(n + kv, kv)) << endl;
return true;
}
} // namespace s5
void solve() {
int n, m, kv;
cin >> n >> m >> kv;
vector<pair<int, int>> edges(m);
for (auto& [u, v] : edges) {
cin >> u >> v;
u--;
v--;
}
bool solved = s1::solve(n, kv, edges) || s2::solve(n, kv, edges) ||
s3::solve(n, kv, edges) || s4::solve(n, kv, edges) ||
s5::solve(n, kv, edges);
assert(solved);
}
int main() {
cin.tie(nullptr);
cin.exceptions(ios::failbit);
ios_base::sync_with_stdio(false);
solve();
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 5
Accepted
time: 0ms
memory: 3612kb
input:
3 2 0 1 2 1 3
output:
333333336
result:
ok single line: '333333336'
Test #2:
score: 5
Accepted
time: 1ms
memory: 3764kb
input:
5 7 5 1 4 2 3 1 2 4 5 2 5 2 4 1 5
output:
895039689
result:
ok single line: '895039689'
Test #3:
score: 5
Accepted
time: 0ms
memory: 3568kb
input:
13 12 13 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 1 10 1 11 1 12 1 13
output:
76923078
result:
ok single line: '76923078'
Test #4:
score: 5
Accepted
time: 1ms
memory: 3588kb
input:
14 13 14 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 1 10 1 11 1 12 1 13 1 14
output:
535714290
result:
ok single line: '535714290'
Test #5:
score: 5
Accepted
time: 2ms
memory: 3568kb
input:
14 13 0 2 9 1 2 1 3 3 8 6 11 2 7 1 5 5 12 2 13 3 14 3 10 3 6 2 4
output:
700595243
result:
ok single line: '700595243'
Test #6:
score: 0
Runtime Error
input:
14 13 14 9 11 2 9 5 10 4 6 10 13 2 3 2 7 4 12 2 4 3 8 7 14 3 5 1 2
output:
result:
Test #7:
score: 5
Accepted
time: 0ms
memory: 3544kb
input:
13 0 13
output:
1
result:
ok single line: '1'
Test #8:
score: 5
Accepted
time: 1ms
memory: 3828kb
input:
14 91 14 3 4 2 10 3 10 1 13 6 8 5 6 10 13 7 8 4 9 4 7 3 7 13 14 2 12 1 3 6 9 9 14 1 10 2 9 7 11 9 11 3 12 8 10 4 13 5 9 11 12 5 14 8 12 8 13 5 13 1 5 6 11 9 13 2 5 1 14 7 14 4 14 3 5 5 11 6 12 1 2 7 10 1 4 1 8 6 10 3 8 6 13 10 14 7 12 10 11 2 13 8 11 11 13 6 7 10 12 3 14 4 5 1 6 2 14 5 10 8 14 4 8 1...
output:
4362623
result:
ok single line: '4362623'
Test #9:
score: 5
Accepted
time: 1120ms
memory: 3648kb
input:
9 15 9 1 2 3 5 2 8 4 8 1 4 2 5 2 7 4 5 1 9 6 8 6 9 1 3 3 7 5 9 5 8
output:
426526937
result:
ok single line: '426526937'
Test #10:
score: 5
Accepted
time: 1112ms
memory: 3628kb
input:
9 14 9 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 4 5 6 8 4 7 6 7 3 8 4 6
output:
214820829
result:
ok single line: '214820829'
Test #11:
score: 5
Accepted
time: 1ms
memory: 3868kb
input:
13 68 0 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 1 10 1 11 1 12 1 13 2 5 2 6 2 7 2 8 2 9 2 10 2 11 2 12 2 13 3 4 3 5 3 6 3 7 3 8 3 9 3 10 3 11 3 12 3 13 4 5 4 6 4 7 4 8 4 9 4 10 4 11 4 12 4 13 5 6 5 7 5 8 5 9 5 10 5 11 5 12 5 13 6 7 6 8 6 9 6 10 6 11 6 12 6 13 7 8 7 10 7 11 7 12 7 13 8 13 9 10 9 11 9 12 9 13...
output:
65784302
result:
ok single line: '65784302'
Test #12:
score: 0
Runtime Error
input:
13 39 13 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 1 10 1 11 1 12 1 13 3 4 3 5 3 6 3 7 3 8 3 9 3 10 3 11 3 12 3 13 4 8 4 9 4 10 4 12 4 13 5 6 5 7 5 10 5 11 5 13 6 7 6 11 6 13 8 9 8 12 8 13 11 13
output:
result:
Test #13:
score: 0
Runtime Error
input:
14 11 14 2 13 4 11 4 14 5 14 6 9 6 14 7 11 9 14 10 12 10 14 12 14
output:
result:
Test #14:
score: 0
Runtime Error
input:
14 46 14 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 1 10 1 11 1 12 1 13 1 14 2 9 2 10 2 11 2 13 2 14 3 4 3 6 3 10 3 11 3 12 3 13 3 14 4 6 4 10 4 11 4 12 4 13 4 14 5 7 5 8 5 9 5 11 5 13 5 14 7 8 7 9 7 14 10 11 10 13 10 14 11 13 11 14 13 14
output:
result:
Test #15:
score: 0
Runtime Error
input:
14 70 14 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 1 10 1 11 1 12 1 13 1 14 2 3 2 4 2 5 2 7 2 8 2 9 2 10 2 11 2 12 2 13 2 14 3 11 3 13 3 14 4 5 4 7 4 8 4 10 4 11 4 12 4 13 4 14 5 7 5 8 5 10 5 12 5 13 5 14 6 7 6 8 6 9 6 10 6 11 6 12 6 13 6 14 7 8 7 10 7 12 7 13 7 14 8 10 8 12 8 13 8 14 9 11 9 12 9 13 9 14 10 1...
output:
result:
Test #16:
score: 0
Runtime Error
input:
14 69 14 1 4 1 5 1 6 1 7 1 8 1 9 1 10 1 11 1 12 1 13 1 14 2 3 2 5 2 6 2 7 2 9 2 10 2 11 2 12 2 13 2 14 3 5 3 6 3 7 3 9 3 10 3 11 3 12 3 13 3 14 4 7 4 8 4 9 4 10 4 11 4 12 4 13 4 14 5 6 5 7 5 9 5 10 5 11 5 12 5 13 5 14 6 7 6 9 6 10 6 11 6 12 6 13 6 14 7 9 7 10 7 11 7 12 7 13 7 14 8 12 8 13 8 14 9 10 ...
output:
result:
Test #17:
score: 0
Runtime Error
input:
14 15 14 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 1 10 1 11 1 12 1 13 1 14 4 6 5 9
output:
result:
Test #18:
score: 0
Runtime Error
input:
15 17 15 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 1 10 1 11 1 12 1 13 1 14 1 15 3 13 3 15 13 15
output:
result:
Test #19:
score: 0
Runtime Error
input:
15 17 15 1 2 1 3 1 4 1 5 1 6 1 7 1 12 1 15 7 12 7 15 8 15 9 15 10 15 11 15 12 15 13 15 14 15
output:
result:
Test #20:
score: 0
Runtime Error
input:
15 16 15 1 2 1 3 1 10 2 10 3 10 4 5 4 6 4 7 4 8 4 9 4 10 4 11 4 12 4 13 4 14 4 15