ATB279G At Most 2 Colors 学习笔记
Luogu Link
题意简述
现在有一行 \(N\) 个格子和 \(C\) 种颜色。每一个格子都需涂 \(C\) 种颜色中的一种,并且任意相邻的 \(K\) 个格子最多有两种不同的颜色。
问有多少种染色方案。答案对 \(998244353\) 取模。
\(2\le K\le N\le 10^6,1\le C\le 10^9\)。
做法解析
看到一行上相邻格子这种玩意不难往连续段DP方向想。设 \(f_{i,j}\) 表示前 \(i\) 个格子中,\([j,i]\) 颜色相同,\(A_{j-1},A_j\) 相异的合法方案数。
代码实现
#include <bits/stdc++.h>
using namespace std;
namespace obasic{typedef long long lolo;template <typename _T>void readi(_T &x){_T k=1;x=0;char ch=getchar();for(;!isdigit(ch);ch=getchar())if(ch=='-')k=-1;for(;isdigit(ch);ch=getchar())x=(x<<3)+(x<<1)+ch-'0';x*=k;return;}template <typename _T>void writi(_T x){if(x<0)putchar('-'),x=-x;if(x>9)writi(x/10);putchar(x%10+'0');}
};
using namespace obasic;
const int MaxN=1e6+5,Mod=998244353;
lolo N,K,C,f[MaxN],g[MaxN];
int main(){readi(N),readi(K),readi(C);f[1]=g[1]=C;for(int i=2;i<=N;i++){lolo x=max(i-K+1,1ll);f[i]=(g[i-1]-g[x]+g[x]*(C-1)%Mod+Mod)%Mod;g[i]=(g[i-1]+f[i])%Mod;}writi(g[N]);return 0;
}
反思总结
暂略。