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;
}

最新文章

  1. DataTable汇总
  2. 【人在江湖飘,哪有不带刀】神器Jumony
  3. 【转载】非线性分析中的ansys跟踪显示
  4. 一种少见的跨目录写webshell方法
  5. 项目中empty遇到的一个问题
  6. 最长上升子序列O(nlogn)算法详解
  7. 为UITextView添加与UITextField一样的边框——UITextField默认边框颜色、宽度、圆角
  8. mysql查询结果中文显示成了问号
  9. S2SH商用后台权限系统第三讲
  10. cocos2d-x3.0 ListView
  11. java Map实现的cache manager
  12. HTML基础总结&lt;标题&gt;
  13. Android事件总线EventBus详解
  14. PHP如何搭建百度Ueditor富文本编辑器
  15. Delphi处理数据网格DBGrid的编辑框 获取还没有提交到数据集的字段文本
  16. 持久化和公平分发.py
  17. mapper.xml文件,sql语句参数为list
  18. 将当前的Ubuntu系统封装成为可以安装(发布)的iso镜像
  19. scala语法
  20. ActiveMQ 集群和主从

热门文章

  1. 怎样设置cookie的到期时间
  2. 关于如何查看 MySQL 信息、查看Oracle 版本
  3. NET如何使用ELinq-实现增删改查
  4. PropertiesUtils(普遍做法)
  5. configure.ac:91: error: possibly undefined macro: AC_SEARCH_LIBS
  6. IDEA乱码总结和处理
  7. 简单服务器通信 模型socketserver
  8. go爬虫之爬取豆瓣电影
  9. redis—django-redis
  10. Ubuntu安装Python 3.6之编译安装+使用PPA源安装