E - Distance to Different:
题目描述:
思路解析:
我们思考一个事,如果 x x y y就等于 2 1 1 2,如果x x y y前面和后面都有元素那么就会变成 1 1 1 1,能实现此形式的有很多,那么如果中间元素连续块长度都等于2,对于答案是没有意义的。因为等于2的情况可以使用块长度为1的来实现,这样可以省下更多数字来得到更多的答案,所有中间长度块一定大于2或者为1,因为x x y y可以用x y x y来实现
那我们思考 x x x y y y x x x 和 x x x y y y z z z作为中间部分有啥区别 b1 =[1 2 1 1 2 1 1 2 1] b2 = [1 2 1 1 2 1 1 2 1] 是没有区别的,只要中间部分连续块长度的出现形式相同,每个连续块是啥数字对于这种情况并没有影响。那问题就变成了将整个数组分为k块,并且中间部分的块长度大于2.这个问题的答案就可以使用dp实现了。
代码实现:
#include <bits/stdc++.h>
using i64 = long long;
template<class T>
constexpr T power(T a, i64 b) {
T res {1};
for (; b; b /= 2, a *= a) {
if (b % 2) {
res *= a;
}
}
return res;
}
constexpr i64 mul(i64 a, i64 b, i64 p) {
i64 res = a * b - i64(1.L * a * b / p) * p;
res %= p;
if (res < 0) {
res += p;
}
return res;
}
template<i64 P>
struct MInt {
i64 x;
constexpr MInt() : x {0} {}
constexpr MInt(i64 x) : x {norm(x % getMod())} {}
static i64 Mod;
constexpr static i64 getMod() {
if (P > 0) {
return P;
} else {
return Mod;
}
}
constexpr static void setMod(i64 Mod_) {
Mod = Mod_;
}
constexpr i64 norm(i64 x) const {
if (x < 0) {
x += getMod();
}
if (x >= getMod()) {
x -= getMod();
}
return x;
}
constexpr i64 val() const {
return x;
}
constexpr MInt operator-() const {
MInt res;
res.x = norm(getMod() - x);
return res;
}
constexpr MInt inv() const {
return power(*this, getMod() - 2);
}
constexpr MInt &operator*=(MInt rhs) & {
if (getMod() < (1ULL << 31)) {
x = x * rhs.x % int(getMod());
} else {
x = mul(x, rhs.x, getMod());
}
return *this;
}
constexpr MInt &operator+=(MInt rhs) & {
x = norm(x + rhs.x);
return *this;
}
constexpr MInt &operator-=(MInt rhs) & {
x = norm(x - rhs.x);
return *this;
}
constexpr MInt &operator/=(MInt rhs) & {
return *this *= rhs.inv();
}
friend constexpr MInt operator*(MInt lhs, MInt rhs) {
MInt res = lhs;
res *= rhs;
return res;
}
friend constexpr MInt operator+(MInt lhs, MInt rhs) {
MInt res = lhs;
res += rhs;
return res;
}
friend constexpr MInt operator-(MInt lhs, MInt rhs) {
MInt res = lhs;
res -= rhs;
return res;
}
friend constexpr MInt operator/(MInt lhs, MInt rhs) {
MInt res = lhs;
res /= rhs;
return res;
}
friend constexpr std::istream &operator>>(std::istream &is, MInt &a) {
i64 v;
is >> v;
a = MInt(v);
return is;
}
friend constexpr std::ostream &operator<<(std::ostream &os, const MInt &a) {
return os << a.val();
}
friend constexpr bool operator==(MInt lhs, MInt rhs) {
return lhs.val() == rhs.val();
}
friend constexpr bool operator!=(MInt lhs, MInt rhs) {
return lhs.val() != rhs.val();
}
friend constexpr bool operator<(MInt lhs, MInt rhs) {
return lhs.val() < rhs.val();
}
};
template<>
i64 MInt<0>::Mod = 998244353;
constexpr int P = 998244353;
using Z = MInt<P>;
void solve() {
int n, k;
std::cin >> n >> k;
std::vector dp(n+1, std::vector (k+1, Z(0)));
dp[0][0] = 1;
std::vector<Z> sum(k+1);
sum[0] = 1;
for(int i = 1; i <= n; i++){
for(int j = 0; j <= k; j++){
int nj = std::min(j+1, k);
dp[i][nj] += sum[j];
if (i > 2 && i != n){
dp[i][nj] -= dp[i - 2][j];
}
}
for(int j = 0; j <= k; j++){
sum[j] += dp[i][j];
}
}
std::cout << dp[n][k] << "\n";
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int t = 1;
//std::cin >> t;
while (t--) {
solve();
}
return 0;
}