博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU 5151 Sit sit sit
阅读量:4672 次
发布时间:2019-06-09

本文共 1627 字,大约阅读时间需要 5 分钟。


n个人依次坐n把椅子,一个人不会做满足以下条件的椅子

1.这把椅子左右都有椅子
2.这把椅子左右椅子上都有人
3.这把椅子左右椅子颜色不同
问坐满的方案数

当l==r时,根据是否合法返回0或1

其他情况枚举当前坐的椅子位置k
\(dp[l][r]=dp[l][k-1]*dp[k+1][r]*C(k-l,r-l)\)
(左右人坐下的顺序可以交替)

#include 
#include
#include
using namespace std;typedef long long LL;const int mod = 1000000007;int n, col[105];int f[105][105];int fact[105], inv[105];void ModAdd(int & x, int y) { x += y; if(x >= mod) x -= mod;}int C(int x, int y) { return (LL)fact[y] * inv[x] % mod * inv[y - x] % mod;}int power(int x, int y) { int res = 1; while(y) { if(y & 1) res = (LL) res * x % mod; x = (LL) x * x % mod; y >>= 1; } return res;}int DP(int l, int r) { int lcol = col[l - 1], rcol = col[r + 1]; if(l == r) { if(lcol == -1 || rcol == -1) return 1; if(lcol != rcol) return 0; return 1; } int & res = f[l][r]; if(res != -1) return res; res = 0; ModAdd(res, DP(l + 1, r)); ModAdd(res, DP(l, r - 1)); for(int i = l + 1; i < r; i++) { ModAdd(res, (LL)DP(l, i - 1) * DP(i + 1, r) % mod * C(i - l, r - l) % mod); } return res;}int main() { fact[0] = 1; for(int i = 1; i <= 100; i++) fact[i] = (LL) fact[i - 1] * i % mod; inv[100] = power(fact[100], mod - 2); for(int i = 99; i >= 0; i--) inv[i] = (LL) inv[i + 1] * (i + 1) % mod; while(scanf("%d", &n) != EOF) { for(int i = 1; i <= n; i++) scanf("%d", &col[i]); memset(f, -1, sizeof(f)); col[0] = col[n + 1] = -1; printf("%d\n", DP(1, n)); } return 0;}

转载于:https://www.cnblogs.com/ljzalc1022/p/9064502.html

你可能感兴趣的文章
[js]DOM 篇
查看>>
C# 观察者模式
查看>>
SQLite(二)高级操作
查看>>
iOS开发之oc(二十)--Foundation(5)NSDictionary
查看>>
初入RFID技术
查看>>
电暖器选购指南(包括暖风机)
查看>>
各类常犯的错误总结
查看>>
mac打包python3程序
查看>>
Manacher's algorithm: 最长回文子串算法
查看>>
算法题003 斐波那契(Fibonacci)数列
查看>>
MySQL索引背后的数据结构及算法原理
查看>>
CSS定位 position
查看>>
冒泡排序
查看>>
es7新特性 includes用法
查看>>
block,inline和inline-block
查看>>
SQA
查看>>
Spring+Struts集成(方案一)
查看>>
在Windows 7中安装、配置和使用IIS7和ASP
查看>>
商业信息敏感、安全处理(口令、数字证书-U盾-密保卡、指纹识别-虹膜识别)...
查看>>
数据库设计的三大范式通俗解释
查看>>