LightOJ - 1246 Colorful Board(DP+组合数)
http://lightoj.com/volume_showproblem.php?problem=1246
题意
有个(M+1)*(N+1)的棋盘,用k种颜色给它涂色,要求曼哈顿距离为奇数的格子之间不能涂相同的颜色,每个格子都必须有颜色,问可行的方案数。
分析
经一波分析,根据曼哈顿距离为奇数这一信息,可以将棋盘分为两部分,也就是相邻格子不能有相同颜色。一种颜色只能在一个部分中出现。现在考虑对一个部分的格子操作,
dp[i][j]表示i个格子选择用了j种颜色的方案数,于是可以得到这样的递推式:dp[i][j]=dp[i-1][j-1]*j+dp[i-1][j]*j。得到dp数组后还不够,需要枚举两个部分使用的颜色数,两层循环,其中选择颜色的方案数则用组合数来算。
#include<iostream>
#include<cmath>
#include<cstring>
#include<queue>
#include<vector>
#include<cstdio>
#include<algorithm>
#include<map>
#include<set>
#define rep(i,e) for(int i=0;i<(e);i++)
#define rep1(i,e) for(int i=1;i<=(e);i++)
#define repx(i,x,e) for(int i=(x);i<=(e);i++)
#define X first
#define Y second
#define PB push_back
#define MP make_pair
#define mset(var,val) memset(var,val,sizeof(var))
#define scd(a) scanf("%d",&a)
#define scdd(a,b) scanf("%d%d",&a,&b)
#define scddd(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define pd(a) printf("%d\n",a)
#define scl(a) scanf("%lld",&a)
#define scll(a,b) scanf("%lld%lld",&a,&b)
#define sclll(a,b,c) scanf("%lld%lld%lld",&a,&b,&c)
#define IOS ios::sync_with_stdio(false);cin.tie(0)
using namespace std;
typedef long long ll;
template <class T>
void test(T a){cout<<a<<endl;}
template <class T,class T2>
void test(T a,T2 b){cout<<a<<" "<<b<<endl;}
template <class T,class T2,class T3>
void test(T a,T2 b,T3 c){cout<<a<<" "<<b<<" "<<c<<endl;}
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3fll;
const ll mod = 1e9+;
int T;
void testcase(){
printf("Case %d: ",++T);
}
const int MAXN = 3e5+;
const int MAXM = ; ll dp[][],C[][];
void init(){
mset(C,);
for(int i=;i<;i++){
C[i][]=;
for(int j=;j<=i;j++){
C[i][j]=(C[i-][j]+C[i-][j-])%mod;
}
}
for(int i=;i<;i++){
dp[i][]=;
for(int j=;j<;j++){
dp[i][j]=(dp[i-][j]*j+dp[i-][j-]*j)%mod; }
}
} int main() {
#ifdef LOCAL
freopen("in.txt","r",stdin);
#endif // LOCAL
init();
int t;
scd(t);
T=;
while(t--){
int m,n,k;
scddd(m,n,k);
m++,n++;
ll ans=;
if(m==n&&m==) ans=k;
else{
int n1=(n+)/*((m+)/)+n/*(m/);
int n2=m*n-n1;
for(int i=;i<k;i++){
for(int j=;i+j<=k;j++){
ans=(ans+(C[k][i]*C[k-i][j])%mod*((dp[n1][i]*dp[n2][j])%mod))%mod;
}
}
}
testcase();
cout<<ans<<endl;
}
return ;
}
LightOJ - 1246 Colorful Board(DP+组合数)的更多相关文章
- LightOJ - 1246 - Colorful Board(DP)
链接: https://vjudge.net/problem/LightOJ-1246 题意: You are given a rectangular board. You are asked to ...
- 1246 - Colorful Board
1246 - Colorful Board PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB ...
- noj 2033 一页书的书 [ dp + 组合数 ]
传送门 一页书的书 时间限制(普通/Java) : 1000 MS/ 3000 MS 运行内存限制 : 65536 KByte总提交 : 53 测试通过 : 1 ...
- 【区间dp+组合数+数学期望】Expression
https://www.bnuoj.com/v3/contest_show.php?cid=9148#problem/I [题意] 给定n个操作数和n-1个操作符,组成一个数学式子.每次可以选择两个相 ...
- lightoj 1095 - Arrange the Numbers(dp+组合数)
题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1095 题解:其实是一道简单的组合数只要推导一下错排就行了.在这里就推导一下错排 ...
- hdu 3944 DP? 组合数取模(Lucas定理+预处理+帕斯卡公式优化)
DP? Problem Description Figure 1 shows the Yang Hui Triangle. We number the row from top to bottom 0 ...
- Contest 20140708 testB dp 组合数
testB 输入文件: testB.in 输出文件testB.out 时限3000ms 问题描述: 定义这样一个序列(a1,b1),(a2,b2),…,(ak,bk)如果这个序列是方序列的话必须满足 ...
- lightoj 1032 二进制的dp
题目链接:http://lightoj.com/volume_showproblem.php?problem=1032 #include <cstdio> #include <cst ...
- HDU 5396 Expression(DP+组合数)(详解)
题目大意: 给你一个n然后是n个数. 然后是n-1个操作符,操作符是插入在两个数字之间的. 由于你不同的运算顺序,会产生不同的结果. 比如: 1 + 1 * 2 有两种 (1+1)*2 或者 ...
随机推荐
- hive桶表
创建桶表,提高查询速度, 下免.tom'jerry'scott如果他们经过hash计算,得到的hash值一样,则放到桶一个表中. 创建桶表 指明桶的分桶条件,以sname分桶;分为5个桶
- super 使用以及原理
用super也很久了,但是一直没有关注过他的原理.最近开始越来越多关注python更底层的实现和奇技淫巧.看到该方法越发使用得多所以也研究了一波 平时单继承可能是我们遇到最多的情况.无非就是类似情况. ...
- python2 高级编程
第一 正则表达式 1. 正则表达式意义 正则表达式是高级文本匹配模式,为搜索,匹配,替换提供了基础 2. 搜索与匹配的区别 搜索指在字符串任何部分查找 匹配指在字符串起始处查找 3. 编写正则表达式 ...
- jquery 選擇器
jquery 選擇器有: 元素選擇器: $("p")選擇所有<p> $("p.intro")選擇所有class=“intro”的<p> ...
- window.onload和document.ready
浏览器解析大致有以下几个步骤: (1) 解析HTML结构 (2) 加载外部脚本和样式 (3) 解析并执行脚本代码 (4) 构造HTML DOM 模型 (5) ...
- UVa - 10341
Solve the equation:p ∗ e ^−x + q ∗ sin(x) + r ∗ cos(x) + s ∗ tan(x) + t ∗ x ^2 + u = 02 + u = 0where ...
- BAI度 内部资料!Python_Threads多线程
基本介绍 runable运行sleeping等待dead销毁(run方法执行完成或执行时抛出异常) 类继承threading.Thread线程的状态 函数介绍 在__init__里调用threadin ...
- React Native——组件FlatList
属性 添加头部组件 ListHeaderComponent属性用来给FlatList添加头部组件 简单使用: //ES6之前写法 _header = function () { return ( &l ...
- 【POJ1741】Tree
题目大意:给定一棵 N 个节点的无根树,边有边权,统计树上边权和不大于 K 的路径数. 对于每条树上路径,对于每一个点来说,该路径只有经过该点和不经过该点两种情况,对于不经过该点的情况,可以转化成是否 ...
- apache加载模块的说明
转: apache加载模块的说明 2017年04月11日 15:23:35 刚子狂想 阅读数:1432 LoadModule auth_basic_module modules/mod_auth_ ...