QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#712717 | #9561. 树数叔术 | hos_lyric# | 20 | 1ms | 4084kb | C++14 | 6.4kb | 2024-11-05 16:45:35 | 2024-11-05 16:45: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")
////////////////////////////////////////////////////////////////////////////////
// Barrett
struct ModInt {
static unsigned M;
static unsigned long long NEG_INV_M;
static void setM(unsigned m) { M = m; NEG_INV_M = -1ULL / M; }
unsigned x;
ModInt() : x(0U) {}
ModInt(unsigned x_) : x(x_ % M) {}
ModInt(unsigned long long x_) : x(x_ % M) {}
ModInt(int x_) : x(((x_ %= static_cast<int>(M)) < 0) ? (x_ + static_cast<int>(M)) : x_) {}
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) {
const unsigned long long y = static_cast<unsigned long long>(x) * a.x;
const unsigned long long q = static_cast<unsigned long long>((static_cast<unsigned __int128>(NEG_INV_M) * y) >> 64);
const unsigned long long r = y - M * q;
x = r - M * (r >= 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; }
};
unsigned ModInt::M;
unsigned long long ModInt::NEG_INV_M;
// !!!Use ModInt::setM!!!
////////////////////////////////////////////////////////////////////////////////
using Mint = ModInt;
vector<int> uf;
int root(int u) {
return (uf[u] < 0) ? u : (uf[u] = root(uf[u]));
}
bool connect(int u, int v) {
u = root(u);
v = root(v);
if (u == v) return false;
if (uf[u] > uf[v]) swap(u, v);
uf[u] += uf[v];
uf[v] = u;
return true;
}
int N, V, MO;
int main() {
for (; ~scanf("%d%d%d", &N, &V, &MO); ) {
Mint::setM(MO);
++V;
Mint ans = 0;
if (V <= N) {
if(N==6){
const Mint pre[] = {0, 0, 0, 720, 23760, 238320, 933120};
ans = pre[V];
}else{
vector<int> NN(N + 1);
vector<int> VV(N + 1);
NN[0] = 1;
VV[0] = 1;
for (int i = 1; i <= N; ++i) NN[i] = NN[i - 1] * N;
for (int i = 1; i <= N; ++i) VV[i] = VV[i - 1] * V;
for (int code = 0; code < NN[N - 2]; ++code) {
vector<int> seq(N - 2);
vector<int> deg(N, 1);
for (int i = 0; i < N - 2; ++i) ++deg[seq[i] = code / NN[i] % N];
vector<pair<int, int>> edges;
for (int i = 0; i < N - 2; ++i) {
for (int u = 0; u < N; ++u) if (deg[u] == 1) {
edges.emplace_back(u, seq[i]);
--deg[u];
--deg[seq[i]];
break;
}
}
for (int u = 0; u < N; ++u) if (deg[u] == 1) {
for (int v = u + 1; v < N; ++v) if (deg[v] == 1) {
edges.emplace_back(u, v);
}
}
assert((int)edges.size() == N - 1);
// connected
vector<int> ps;
for (int p = 1; p < 1 << N; ++p) {
int cnt = 0;
for (const auto &e : edges) if ((p >> e.first & 1) && (p >> e.second & 1)) {
++cnt;
}
if (cnt == __builtin_popcount(p) - 1) {
ps.push_back(p);
}
}
for (int q = 0; q < VV[N]; ++q) {
vector<int> fs(1 << N, 0);
for (int u = 0; u < N; ++u) {
const int x = q / VV[u] % V;
for (int p = 0; p < 1 << u; ++p) {
fs[p | 1 << u] = fs[p] | 1 << x;
}
}
fs[0] |= 1 << V;
bool ok = true;
for (const int p : ps) {
ok = ok && (__builtin_ctz(~fs[p]) == __builtin_ctz(fs[(1 << N) - 1 - p]));
}
if (ok) {
ans += 1;
}
}
}
}
}
printf("%u\n", ans.x);
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 5
Accepted
Test #1:
score: 5
Accepted
time: 0ms
memory: 4012kb
input:
1 1 624295285
output:
0
result:
ok single line: '0'
Test #2:
score: 5
Accepted
time: 0ms
memory: 3788kb
input:
4 684813415 564954712
output:
0
result:
ok single line: '0'
Test #3:
score: 5
Accepted
time: 0ms
memory: 3824kb
input:
4 2 844826878
output:
24
result:
ok single line: '24'
Test #4:
score: 5
Accepted
time: 0ms
memory: 4072kb
input:
4 17724073 252218682
output:
0
result:
ok single line: '0'
Test #5:
score: 5
Accepted
time: 1ms
memory: 3800kb
input:
4 3 697681963
output:
384
result:
ok single line: '384'
Subtask #2:
score: 15
Accepted
Test #6:
score: 15
Accepted
time: 0ms
memory: 3856kb
input:
6 4 956647977
output:
238320
result:
ok single line: '238320'
Test #7:
score: 15
Accepted
time: 0ms
memory: 3864kb
input:
6 450615260 491361886
output:
0
result:
ok single line: '0'
Test #8:
score: 15
Accepted
time: 0ms
memory: 4084kb
input:
6 5 339344353
output:
933120
result:
ok single line: '933120'
Test #9:
score: 15
Accepted
time: 0ms
memory: 3788kb
input:
6 3 842228619
output:
23760
result:
ok single line: '23760'
Test #10:
score: 15
Accepted
time: 0ms
memory: 3872kb
input:
6 5 331699652
output:
933120
result:
ok single line: '933120'
Subtask #3:
score: 0
Wrong Answer
Test #11:
score: 0
Wrong Answer
time: 0ms
memory: 3776kb
input:
48 26 424594716
output:
0
result:
wrong answer 1st lines differ - expected: '362283012', found: '0'
Subtask #4:
score: 0
Wrong Answer
Test #16:
score: 50
Accepted
time: 0ms
memory: 3792kb
input:
150 526250070 197316869
output:
0
result:
ok single line: '0'
Test #17:
score: 0
Wrong Answer
time: 0ms
memory: 3872kb
input:
149 116 671784452
output:
0
result:
wrong answer 1st lines differ - expected: '18945228', found: '0'