Codeforces 1051 D.Bicolorings(DP)

Codeforces 1051 D.Bicolorings

题意:一个2×n的方格纸,用黑白给格子涂色,要求分出k个连通块,求方案数。

思路:用0,1表示黑白,则第i列可以涂00,01,10,11,(可以分别用0,1,2,3表示),于是定义dp[i][j][k]:涂到第i列分为j个连通块且第i列涂法为k的方案数,则有了代码中的转移式,共16种转移类型。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<map>
#include<queue>
#include<string>
#include<vector>
#include<cmath>
#include<climits>
#include<functional>
#include<set>
#define dd(x) cout<<#x<<" = "<<x<<" "
#define de(x) cout<<#x<<" = "<<x<<endl
#define fi first
#define se second
#define mp make_pair
#define pb push_back
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
typedef vector<int> V;
typedef map<int,int> M;
typedef queue<int> Q;
typedef priority_queue<int> BQ;
typedef priority_queue<int,vector<int>,greater<int> > SQ;
const int maxn=1e3+10,INF=0x3f3f3f3f,mod=998244353;
int dp[maxn][maxn<<1][5];//0->00, 1->01, 2->10, 3->11
inline int add(int a,int b)
{
  a+=b;
  if (a>=mod)
    a-=mod;
  return a;
}
int main()
{
    int n,k;
    scanf("%d%d",&n,&k);
    dp[1][1][0]=dp[1][2][1]=dp[1][2][2]=dp[1][1][3]=1;
    for (int i=2;i<=n;++i)
    {
      for (int j=1;j<=2*i;++j)
      {
        dp[i][j][0]=add(dp[i][j][0],add(dp[i-1][j-1][3],add(dp[i-1][j][2],add(dp[i-1][j][1],dp[i-1][j][0]))));
        dp[i][j][1]=add(dp[i][j][1],add(dp[i-1][j-1][0],add(dp[i-1][j][1],add(j>=2?dp[i-1][j-2][2]:0,dp[i-1][j-1][3]))));
        dp[i][j][2]=add(dp[i][j][2],add(dp[i-1][j-1][0],add(dp[i-1][j][2],add(j>=2?dp[i-1][j-2][1]:0,dp[i-1][j-1][3]))));
        dp[i][j][3]=add(dp[i][j][3],add(dp[i-1][j-1][0],add(dp[i-1][j][1],add(dp[i-1][j][2],dp[i-1][j][3]))));
      }
    }
    printf("%d",add(dp[n][k][0],add(dp[n][k][1],add(dp[n][k][2],dp[n][k][3]))));
    return 0;
}

原文地址:https://www.cnblogs.com/orangee/p/9751102.html

时间: 2024-08-02 21:56:23

Codeforces 1051 D.Bicolorings(DP)的相关文章

Codeforces Gym101341K:Competitions(DP)

http://codeforces.com/gym/101341/problem/K 题意:给出n个区间,每个区间有一个l, r, w,代表区间左端点右端点和区间的权值,现在可以选取一些区间,要求选择的区间不相交,问最大的权和可以是多少,如果权和相同,则选区间长度最短的.要要求输出区间个数和选了哪些区间. 思路:把区间按照右端点排序后,就可以维护从左往右,到p[i].r这个点的时候,已经选择的最大权和是多少,最小长度是多少,区间个数是多少. 因为可以二分找右端点小于等于当前区间的左端点的某个区间

Codeforces 543C Remembering Strings(DP)

题意比较麻烦 见题目链接 Solution: 非常值得注意的一点是题目给出的范围只有20,而众所周知字母表里有26个字母.于是显然对一个字母进行变换后是不影响到其它字符串的. 20的范围恰好又是常见状压DP的范围,所有状态压缩后用DP[sta]代表对应位的字符串已经满足要求的最小花费. 转移的时候,对一个字符串,逐列判断使它满足条件的最小花费,记录使用这个策略对sta的影响. 即对同一列有两种情况,直接变换该字符串的这一位,或者变换这一列的sum-1个有相同字符的位置(去掉代价最大的). #in

Codeforces 543D Road Improvement(DP)

题目链接 Solution 比较明显的树形DP模型. 首先可以先用一次DFS求出以1为根时,sum[i](以i为子树的根时,满足要求的子树的个数). 考虑将根从i变换到它的儿子j时,sum[i]产生的变化. 在变化前sum[i]不为0时,可以用求逆元的方法求出新的sum[i]. sum[i]为0时,就需要遍历i的新的儿子. 官方的题解给出了一个比较好的做法是预处理i的儿子的前缀积,和后缀积.使用的时候只要去除相应的儿子. #include <bits/stdc++.h> #define LL

Codeforces 682B New Skateboard(DP)

题目大概说给一个数字组成的字符串问有几个子串其代表的数字(可以有前导0)能被4整除. dp[i][m]表示字符串0...i中mod 4为m的后缀的个数 通过在i-1添加str[i]字符转移,或者以str[i]为新后缀的开头转移 1 #include<cstdio> 2 #include<cstring> 3 using namespace std; 4 char str[333333]; 5 long long d[333333][4]; 6 int main(){ 7 scanf

codeforces 489F Special Matrices(DP)

传送门:点击打开链接 题目大意: 给定一个n*n的01矩阵的前m行,要求求出有多少种构造方案使得:每一行,每一列的1的个数都是2 解题思路: 既然已经给定了前m行,那么就相当于告诉了我们有哪几列,还能放2个1,1个1,和不能再放1了. 注意到,这个时候列之间是可以交换的,那么就可以做了. 定义dp[i][j][k]表示在第i行剩j个位置可以放2个1,剩k个位置可以放1个1. 因为下一行一定要放2个1,那么就有3种转移状态: 1:全部选的是放2个的.那么就转移到了dp[i+1][j-2][k+2]

codeforces#983 B. XOR-pyramid (dp)

参考博客:https://www.01hai.com/note/av137952. 题意:首先定义 (b代表一个数组) 给出一个区间,l,r,求它最大的连续子序列的函数值 分析: 定义dp[x][y]为选取x到y这段区间时的函数值 观察发现dp[x][y]=dp[x+1][y]^dp[x][y-1] 代码: #include<bits/stdc++.h> #define ll long long #define pa pair<int,int> using namespace st

Codeforces 803E--Roma and Poker (DP)

原题链接:http://codeforces.com/problemset/problem/803/E 题意:给一个n长度的字符串,其中'?'可以替换成'D'.'W'.'L'中的任意一种,'D'等价于0, 'W'等价于1.'L'等价于-1.输出所有'?'被替换掉后,W和L的数目之差为k,且任意一个[1, i]的子串中W和L数目之差不能等于k. 思路:用DP做.定义bool dp[i][j]代表前i个字符W和L数目之差为j, -k<=j<=k(在数组中范围为[0, 2*k]),那么当str[i]

Codeforces Round #260 (Div. 1) A. Boredom (DP)

题目链接:http://codeforces.com/problemset/problem/455/A A. Boredom time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Alex doesn't like boredom. That's why whenever he gets bored, he comes up with

Codeforces118D Caesar&#39;s Legions(DP)

题目 Source http://codeforces.com/problemset/problem/118/D Description Gaius Julius Caesar, a famous general, loved to line up his soldiers. Overall the army had n1 footmen and n2 horsemen. Caesar thought that an arrangement is not beautiful if somewhe