「杂题乱刷」AT_abc307_e

链接(at)

链接(luogu)

dp 板子。

不难看出,可以设两个状态:

  • \(dp_{i,0}\) 表示第 \(i-1\) 位颜色与第 \(1\) 位颜色不同且前 \(i\) 位每相邻两位的颜色均不同的方案数。

  • \(dp_{i,1}\) 表示第 \(i-1\) 位颜色与第 \(1\) 位颜色相同且前 \(i\) 位每相邻两位的颜色均不同的方案数。

然后容易得出两个 dp 式子:

  • \(dp_{i,0} = dp_{i-1,0} \times (m-2) + dp_{i-1,1} \times (m-1)\)

  • \(dp_{i,1} = dp_{i-1,0}\)

最后 \(dp_{n,0}\) 即为答案。

代码:

点击查看代码
#include<bits/stdc++.h>
using namespace std;
#define map unordered_map
#define forl(i,a,b) for(register long long i=a;i<=b;i++)
#define forr(i,a,b) for(register long long i=a;i>=b;i--)
#define lc(x) x<<1
#define rc(x) x<<1|1
#define cin(x) scanf("%lld",&x)
#define cout(x) printf("%lld",x)
#define lowbit(x) x&-x
#define pb push_back
#define pf push_front
#define IOS ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define endl '\n'
#define QwQ return 0;
#define ll long long
ll n,m,mod=998244353,dp[1000010][5];
int main()
{
	IOS;
	cin>>n>>m;
	dp[1][1]=m;
	forl(i,2,n)
		dp[i][0]=((dp[i-1][0]%mod)*((m-2)%mod)+(dp[i-1][1]%mod)*((m-1)%mod))%mod,dp[i][1]=dp[i-1][0];
	cout<<dp[n][0];
	QwQ;
}
posted @ 2024-01-26 23:20  wangmarui  阅读(4)  评论(0编辑  收藏  举报