题目连接:E - Distinct Adjacent (atcoder.jp)
这种求领边染色问题可以用二维表示
状态:
dp [i] [0/1] 代表第 i 个的选择和 1 号不同和相同
转移方程:
dp[i][0] = (dp[i - 1][0] * (m - 2) + dp[i - 1][1] * (m - 1)) % mod;
dp[i][1] = dp[i - 1][0] % mod;
属性:dp[n][0];
#include<bits/stdc++.h> using namespace std; using ull = unsigned long long; using ll = long long; using PII = pair<int,int>; #define IOS ios::sync_with_stdio(false),cin.tie(0) #define endl "\n" #define pb push_back const int N=1e6+10; const int INF=0x3f3f3f3f; const int mod = 998244353; ll dp[N][2]; int main() { int n, m; cin >> n >> m; dp[1][1] = m; for(int i = 2; i <= n; i ++) { dp[i][0] = (dp[i - 1][0] * (m - 2) + dp[i - 1][1] * (m - 1)) % mod; dp[i][1] = dp[i - 1][0] % mod; } cout << dp[n][0] << endl; return 0; }
相似的题目链接:D - Poisonous Full-Course (atcoder.jp)
标签:状态,const,一维,int,long,数组,using,dp,mod From: https://www.cnblogs.com/ZouYua/p/17569677.html