QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#505249 | #6333. Festivals in JOI Kingdom 2 | hos_lyric | 100 ✓ | 2645ms | 7084kb | C++14 | 9.6kb | 2024-08-04 23:28:59 | 2024-08-04 23:28:59 |
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 long long 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;
constexpr int LIM_INV = 100'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;
}
}
}
namespace exper {
int N;
int tot;
vector<int> freq;
vector<int> L, R;
void check() {
int opt = 0, grd = 0;
{
int l = 2*N;
for (int i = N; --i >= 0; ) if (R[i] < l) {
++opt;
l = L[i];
}
}
{
int r = -1;
for (int i = 0; i < N; ++i) if (r < L[i]) {
++grd;
r = R[i];
}
}
if (opt == grd) {
if(N==4&&opt==2){
for(int x=0;x<2*N;++x)cerr<<x<<" ";
cerr<<endl;
for(int i=0;i<N;++i)cerr<<string(2*L[i],' ')<<'|'<<string(2*(R[i]-L[i])-1,'-')<<'|'<<endl;
cerr<<endl;
}
++tot;
++freq[opt];
}
}
void dfs(int i, int yet) {
if (i == N) {
check();
} else {
L[i] = __builtin_ctz(yet);
for (R[i] = L[i] + 1; R[i] < 2*N; ++R[i]) if (yet & 1 << R[i]) {
dfs(i + 1, yet ^ 1 << L[i] ^ 1 << R[i]);
}
}
}
void run() {
for (N = 1; N <= 8; ++N) {
tot = 0;
freq.assign(N + 1, 0);
L.resize(N);
R.resize(N);
dfs(0, (1 << (2*N)) - 1);
cerr << N << ": " << tot << " " << freq << endl;
}
}
} // exper
/*
1: 1 [0, 1]
2: 3 [0, 2, 1]
3: 13 [0, 6, 6, 1]
4: 77 [0, 24, 40, 12, 1]
5: 587 [0, 120, 312, 134, 20, 1]
6: 5501 [0, 720, 2820, 1598, 332, 30, 1]
7: 61251 [0, 5040, 29088, 20888, 5502, 690, 42, 1]
8: 790103 [0, 40320, 337680, 300096, 95622, 15052, 1276, 56, 1]
*/
/*
min cost flow potential
+------------+ +-----------+
| | | |
v | v |
m <- m -> ... -> m-1 <- m-1 ... 1 -> ... -> 0 <- 0
leftmost i: i-th right-end of right-end greedy solution
how many ways to insert left-end? (right-end: +1)
+--------+ +------+ +---+
| | | | | |
--v--------@---------@------@--
+--------+ +------+ +---+
| | | | | |
--v---@---------@---------@----
| | | | | |
+-+ +------+ +------+
+1 +2 +2 +1
m := (solution size)
choose u[m-1], ..., u[0] \in {0, 1}
~~> 1+u[m-1], 1+u[m-1]+u[m-2], ..., 1+u[1]+u[0], 0+u[0]
*2 for u[i+1] = 1 && u[i] = 1
*/
Mint slow(int N) {
constexpr int MAX_N = 3010;
// constexpr int MAX_N = 310;
static Mint dp[MAX_N][2 * MAX_N][2];
// static Mint dp[MAX_N][MAX_N][2 * MAX_N][2];
// for(int m=0;m<=N;++m)
for (int n = 0; n <= N; ++n) for (int k = 0; k <= 2*N + 1; ++k) for (int u = 0; u < 2; ++u) {
dp[n][k][u] = 0;
// dp[m][n][k][u] = 0;
}
for (int u = 0; u < 2; ++u) {
dp[u][u + 1][u] += 1;
// dp[0][u][u + 1][u] += 1;
}
// for(int m=0;m<N;++m)
for (int n = 0; n <= N; ++n) for (int k = 1; k <= 2*N + 1; ++k) for (int u = 0; u < 2; ++u) if (dp[n][k][u]) {
// cerr<<N<<"; "<<n<<" "<<k<<" "<<u<<endl;
assert(k == 2 * n + 1 - u);
// for (int n = 0; n <= N; ++n) for (int k = 1; k <= 2*N + 1; ++k) for (int u = 0; u < 2; ++u) if (dp[m][n][k][u]) {
for (int v = 0; v < 2; ++v) for (int a = 0; n + 1 + v + a <= N; ++a) {
dp[n + 1 + v + a][k + a + 1 + u + v + a + 1][v]
// dp[m + 1][n + 1 + v + a][k + a + 1 + u + v + a + 1][v]
+= dp[n][k][u]
// += dp[m][n][k][u]
* ((u && v) ? 2 : 1)
* (fac[k + a - 1] * invFac[k - 1])
* (fac[u + v + a] * invFac[u + v] * invFac[a])
;
}
}
Mint ans = 0;
// vector<Mint>anss(N+1,0);
// for(int m=0;m<=N;++m)
for (int k = 0; k <= 2*N + 1; ++k) {
ans += dp[N][k][0];
// ans += dp[m][N][k][0];
// anss[m] += dp[m][N][k][0];
}
// cerr<<"anss = "<<anss<<endl;
return ans;
}
/*
(n, u) -> (n+a+1+v, v)
((u&&v)?2:1) * ((2n-u+a)! / (2n-u)!) * binom(a+u+v, u+v)
*/
Mint slow2(int N) {
vector<Mint> F[2];
for (int u = 0; u < 2; ++u) {
F[u].assign(N + 1, 0);
F[u][u] += 1;
}
for (int n = 0; n < N; ++n) {
const Mint f0 = F[0][n] * invFac[2 * n];
for (int a = 0; n + a + 1 <= N; ++a) F[0][n + a + 1] += f0 * fac[2 * n + a];
for (int a = 0; n + a + 2 <= N; ++a) F[1][n + a + 2] += f0 * fac[2 * n + a] * (a + 1);
if (2 * n - 1 >= 0) {
const Mint f1 = F[1][n] * invFac[2 * n - 1];
for (int a = 0; n + a + 1 <= N; ++a) F[0][n + a + 1] += f1 * fac[2 * n - 1 + a] * (a + 1);
for (int a = 0; n + a + 2 <= N; ++a) F[1][n + a + 2] += f1 * fac[2 * n - 1 + a] * (a + 1) * (a + 2);
}
}
return F[0][N];
}
int main() {
Mint::setM(1'000'000'007);
prepare();
// exper::run();
// for (int N = 1; N <= 8; ++N) cerr << N << ": " << slow(N) << endl;
int N, MO;
for (; ~scanf("%d%d", &N, &MO); ) {
Mint::setM(MO);
prepare();
Mint ans = slow2(N);
Mint all = 1;
for (int i = 0; i < N; ++i) all *= (2 * i + 1);
ans = all - ans;
printf("%u\n", ans.x);
}
return 0;
}
詳細信息
Subtask #1:
score: 5
Accepted
Test #1:
score: 5
Accepted
time: 3ms
memory: 7068kb
input:
1 194903119
output:
0
result:
ok 1 number(s): "0"
Test #2:
score: 5
Accepted
time: 0ms
memory: 6852kb
input:
2 933234047
output:
0
result:
ok 1 number(s): "0"
Test #3:
score: 5
Accepted
time: 2ms
memory: 4892kb
input:
3 277793111
output:
2
result:
ok 1 number(s): "2"
Test #4:
score: 5
Accepted
time: 2ms
memory: 5240kb
input:
4 355321177
output:
28
result:
ok 1 number(s): "28"
Test #5:
score: 5
Accepted
time: 2ms
memory: 4948kb
input:
5 306636893
output:
358
result:
ok 1 number(s): "358"
Subtask #2:
score: 5
Accepted
Dependency #1:
100%
Accepted
Test #6:
score: 5
Accepted
time: 2ms
memory: 4940kb
input:
8 361605653
output:
1236922
result:
ok 1 number(s): "1236922"
Test #7:
score: 5
Accepted
time: 0ms
memory: 4940kb
input:
8 995512643
output:
1236922
result:
ok 1 number(s): "1236922"
Test #8:
score: 5
Accepted
time: 2ms
memory: 5928kb
input:
8 101102801
output:
1236922
result:
ok 1 number(s): "1236922"
Test #9:
score: 5
Accepted
time: 2ms
memory: 4944kb
input:
6 458322727
output:
4894
result:
ok 1 number(s): "4894"
Test #10:
score: 5
Accepted
time: 2ms
memory: 5240kb
input:
7 721691819
output:
73884
result:
ok 1 number(s): "73884"
Test #11:
score: 5
Accepted
time: 2ms
memory: 6376kb
input:
7 370629137
output:
73884
result:
ok 1 number(s): "73884"
Subtask #3:
score: 27
Accepted
Dependency #1:
100%
Accepted
Dependency #2:
100%
Accepted
Test #12:
score: 27
Accepted
time: 3ms
memory: 6836kb
input:
30 779092367
output:
686412377
result:
ok 1 number(s): "686412377"
Test #13:
score: 27
Accepted
time: 0ms
memory: 4892kb
input:
29 963995171
output:
128570082
result:
ok 1 number(s): "128570082"
Test #14:
score: 27
Accepted
time: 3ms
memory: 5224kb
input:
18 666092701
output:
185922458
result:
ok 1 number(s): "185922458"
Test #15:
score: 27
Accepted
time: 2ms
memory: 6760kb
input:
14 671243719
output:
623913899
result:
ok 1 number(s): "623913899"
Subtask #4:
score: 14
Accepted
Dependency #1:
100%
Accepted
Dependency #2:
100%
Accepted
Dependency #3:
100%
Accepted
Test #16:
score: 14
Accepted
time: 0ms
memory: 6524kb
input:
300 463478027
output:
89265245
result:
ok 1 number(s): "89265245"
Test #17:
score: 14
Accepted
time: 3ms
memory: 6464kb
input:
296 567610679
output:
406342763
result:
ok 1 number(s): "406342763"
Test #18:
score: 14
Accepted
time: 3ms
memory: 6652kb
input:
297 609090359
output:
128986577
result:
ok 1 number(s): "128986577"
Test #19:
score: 14
Accepted
time: 2ms
memory: 6668kb
input:
48 759569383
output:
635573072
result:
ok 1 number(s): "635573072"
Test #20:
score: 14
Accepted
time: 3ms
memory: 6676kb
input:
99 298873033
output:
285340078
result:
ok 1 number(s): "285340078"
Subtask #5:
score: 36
Accepted
Dependency #1:
100%
Accepted
Dependency #2:
100%
Accepted
Dependency #3:
100%
Accepted
Dependency #4:
100%
Accepted
Test #21:
score: 36
Accepted
time: 62ms
memory: 5256kb
input:
3000 752129633
output:
33058561
result:
ok 1 number(s): "33058561"
Test #22:
score: 36
Accepted
time: 62ms
memory: 7068kb
input:
2993 491173567
output:
343277625
result:
ok 1 number(s): "343277625"
Test #23:
score: 36
Accepted
time: 62ms
memory: 5252kb
input:
2993 783095563
output:
776085006
result:
ok 1 number(s): "776085006"
Test #24:
score: 36
Accepted
time: 3ms
memory: 6732kb
input:
327 399783431
output:
163535283
result:
ok 1 number(s): "163535283"
Test #25:
score: 36
Accepted
time: 13ms
memory: 6232kb
input:
1233 857060207
output:
422139845
result:
ok 1 number(s): "422139845"
Test #26:
score: 36
Accepted
time: 11ms
memory: 6464kb
input:
1114 600227447
output:
598509129
result:
ok 1 number(s): "598509129"
Subtask #6:
score: 13
Accepted
Dependency #1:
100%
Accepted
Dependency #2:
100%
Accepted
Dependency #3:
100%
Accepted
Dependency #4:
100%
Accepted
Dependency #5:
100%
Accepted
Test #27:
score: 13
Accepted
time: 2645ms
memory: 7084kb
input:
20000 221054167
output:
39809956
result:
ok 1 number(s): "39809956"
Test #28:
score: 13
Accepted
time: 2644ms
memory: 5352kb
input:
19997 823974001
output:
267537750
result:
ok 1 number(s): "267537750"
Test #29:
score: 13
Accepted
time: 2640ms
memory: 6944kb
input:
19991 501689843
output:
16527909
result:
ok 1 number(s): "16527909"
Test #30:
score: 13
Accepted
time: 1358ms
memory: 5308kb
input:
14344 925452091
output:
212324779
result:
ok 1 number(s): "212324779"
Test #31:
score: 13
Accepted
time: 249ms
memory: 6492kb
input:
6098 507350869
output:
310480789
result:
ok 1 number(s): "310480789"
Test #32:
score: 13
Accepted
time: 207ms
memory: 5008kb
input:
5564 406069759
output:
105694337
result:
ok 1 number(s): "105694337"
Extra Test:
score: 0
Extra Test Passed