1 solutions
-
-2
原题:qoj 779 弱化版
考虑直接对差绝对值为 的相邻元素个数容斥。注意到我们可以把元素按照 分成若干等价类,等价类之间是不会出现差绝对值为 的元素的;等价类内部,注意到差绝对值为 的相邻元素必然构成若干段连续段,且长度 的连续段还会有额外的两倍贡献系数(因为可以选择升序还是降序)。
预处理 表示 个元素,钦定 个连续段的方案数,由于 只有两个有效取值( 和 ),可以暴力枚举长度 的连续段数量然后插板法。然后背包算出 表示总共钦定了 个连续段的答案,则对 的贡献是 。这部分复杂度是 ,也是 的。
题外话:原题给出了对于 的所有 ,求出答案的 (不需要 GF)和 (需要 GF,不需要 FFT)的做法,感兴趣的同学可以自行阅读原题题解。考虑到本场比赛 T4 难度较高,没有设置这两个做法的部分分。
#include <bits/stdc++.h> using namespace std; // #define USE_INT_128 #define il inline #define mkp make_pair #define fi first #define se second #define ssz(x) (signed((x).size())) #define beg2ed(x) (x).begin(), (x).end() #define _loop_i_t(j, k) make_signed_t<decltype((j) - (k))> #define For(i, j, k) for (_loop_i_t(j, k) i = (j); i <= (k); ++i) #define ForDown(i, j, k) for (_loop_i_t(j, k) i = (j); i >= decltype(i)(k); --i) #define eb emplace_back #ifndef ONLINE_JUDGE #define FileIO(filename) \ freopen(filename ".in", "r", stdin); \ freopen(filename ".out", "w", stdout) #else #define FileIO(filename) void(0) #endif using ll = long long; using uint = unsigned int; using ull = unsigned long long; using db = double; using ldb = long double; using pii = pair<int, int>; using pll = pair<ll, ll>; #ifdef USE_INT_128 using lll = __int128_t; using ulll = __uint128_t; #endif // clang-format off constexpr il ll qpow(ll x, ull y, ll mod){ ll ans = 1; x %= mod; while (y) { if (y & 1) (ans *= x) %= mod; (x *= x) %= mod; y >>= 1; } return ans; } template<typename T> constexpr il void cmin(T &x, const T &y){ x = min(x, y); } template<typename T> constexpr il void cmax(T &x, const T &y){ x = max(x, y); } // clang-format on // File head end namespace { constexpr int MAXN = 5005; int n, K, M, C[MAXN][MAXN], fac[MAXN], f[2][MAXN], g[2][MAXN], pw2[MAXN]; il void add(int &x, int y) { ((x += y) >= M) ? (x -= M) : 0; } int calc() { memset(f[1], 0, (n + 1) * sizeof(int)), f[1][0] = 1; memset(g, 0, sizeof g); For(i, 1, n / K - 1) { For(j, 0, i - 1) { g[0][i] = (g[0][i] + ll(C[n / K - i - 1][i - j - 1]) * C[i][j] % M * pw2[i - j]) % M; } } g[0][n / K] = 1; For(i, 1, n / K) { For(j, 0, i - 1) { g[1][i] = (g[1][i] + ll(C[n / K - i][i - j - 1]) * C[i][j] % M * pw2[i - j]) % M; } } g[1][n / K + 1] = 1; For(i, 0, K - 1) { int cur = i & 1, pre = cur ^ 1; memset(f[cur], 0, (n + 1) * sizeof(int)); int c = n / K + (i < n % K); For(j, 0, n) if (f[pre][j]) { ll val = f[pre][j]; For(k, 1, c) add( f[cur][j + k], (((c - k) & 1) ? (M - g[i < n % K][k]) : g[i < n % K][k]) * val % M); } // For(j, 0, n) if (f[cur][j]) cerr << i << ' ' << j << ' ' << f[cur][j] // << '\n'; } ll Ans = 0; For(i, 1, n)(Ans += ll(f[(K - 1) & 1][i]) * fac[i]) %= M; return Ans; } void Main() { FileIO("counting"); cin.tie(nullptr)->sync_with_stdio(false); cin >> n >> K >> M; fac[0] = pw2[0] = 1; For(i, 1, n) fac[i] = fac[i - 1] * ll(i) % M, pw2[i] = pw2[i - 1] * 2ll % M; C[0][0] = 1; For(i, 1, n) { C[i][0] = C[i][i] = 1; For(j, 1, i - 1) C[i][j] = (C[i - 1][j - 1] + C[i - 1][j]) % M; } cout << calc() << '\n'; } } // namespace signed main() { return Main(), 0; }
- 1
Information
- ID
- 157
- Time
- 1000ms
- Memory
- 512MiB
- Difficulty
- 10
- Tags
- (None)
- # Submissions
- 5
- Accepted
- 1
- Uploaded By