There are 2N white balls on a table in two rows, making a nice 2-by-N rectangle. Jon has a big paint bucket
full of black paint. (Don’t ask why.) He wants to paint all the balls black, but he would like to have some
math fun while doing it. (Again, don’t ask why.) First, he computed the number of different ways to paint
all the balls black. In no time, he figured out that the answer is (2N)! and thought it was too easy. So, he
introduced some rules to make the problem more interesting.
• The first ball that Jon paints can be any one of the 2N balls.
• After that, each subsequent ball he paints must be adjacent to some black ball (that was already
painted). Two balls are assumed to be adjacent if they are next to each other horizontally, vertically,
or diagonally.
Jon was quite satisfied with the rules, so he started counting the number of ways to paint all the balls
according to them. Can you write a program to find the answer faster than Jon?
B.1 Input
The input consists of multiple test cases. Each test case consists of a single line containing an integer N,
where 1 ≤ N ≤ 1,000. The input terminates with a line with N = 0. For example:
1
2
3
0
B.2 Output
For each test case, print out a single line that contains the number of possible ways that Jon can paint all
the 2N balls according to his rules. The number can become very big, so print out the number modulo
1,000,000,007. For example, the correct output for the sample input above would be:
2
24
480

题意

给你两行n列的2*n个球,一开始你随意选一个涂黑色,接着必须在黑色球相邻的球里选择一个再涂黑色,可以斜着相邻,求涂完2n个球有多少种涂法。

分析

递推没办法,只能动态规划,f[i][j]表示,染色长度为i的两行矩阵,染了j个球的方案数,染色长度就是指这连续的i列每列至少有一个球染色了,按照规则可知染色的球一定在一个染色矩阵里,就是不会有隔了一列的染了色的球。

状态转移方程:

f[i][j]+=f[i][j-1]*(2*i-(j-1)) 表示在i列里选择没有染色的球2*i-(j-1)进行染色,他们肯定可以染色。

f[i][j]+=f[i-1][j-1]*4 表示它可以从少一列的染色矩阵的外部左边或者右边共四个球里选一个染色后,获得的i列染色矩阵。

代码

#include<stdio.h>
#define N 1005
#define M 1000000007
long long dp[N][N],n;
int main(){
for(int i=;i<=;i++)
for(int j=i;j<=*i;j++)
if(i==)dp[i][j]=;
else dp[i][j] = dp[i][j-] * (*i-j+) %M + dp[i-][j-]* %M; while(scanf("%I64d",&n)&&n)
printf("%I64d\n",dp[n][*n]);
return ;
}

【Gym 100015B】Ball Painting的更多相关文章

  1. 【Gym 100015B】Ball Painting(DP染色)

    题 There are 2N white balls on a table in two rows, making a nice 2-by-N rectangle. Jon has a big pai ...

  2. 【 Gym 101116K 】Mixing Bowls(dfs)

    BUPT2017 wintertraining(15) #4H Gym - 101116K 题意 给定一个菜谱,大写的单词代表混合物,小写的代表基础原料.每个混合物由其它混合物或基础原料组成,不会间接 ...

  3. 【 Gym - 101124E 】Dance Party (数学)

    BUPT2017 wintertraining(15) #4G Gym - 101124 E.Dance Party 题意 有c种颜色,每个颜色最多分配给两个人,有M个男士,F个女士,求至少一对男士同 ...

  4. 【Gym - 101124A】The Baguette Master (数学,几何)

    BUPT2017 wintertraining(15) #4F Gym - 101124A 题意 给定画框宽度,画的四边和一个对角线长度,求画框外沿周长. 题解 过顶点做画框的垂线,每个角都得到两个全 ...

  5. 【 Gym - 101138K 】 The World of Trains (DP)

    BUPT2017 wintertraining(15) #4E Gym - 101138K 题意 N节车厢的火车,每节车厢容量是1~K,那么有\(K^N\)种火车. 求选择D个连续的且容量相同的车厢的 ...

  6. 【 Gym - 101138J 】Valentina and the Gift Tree(树链剖分)

    BUPT2017 wintertraining(15) 4 D Gym - 101138J 数据 题意 n个节点的一棵树,每个节点的权值为g,q个询问,树上的节点U-V,求U到V的路径的最大子段和. ...

  7. 【 Gym - 101138F 】GukiZ Height (数学)

    BUPT2017 wintertraining(15) #4 C Gym - 101138F 题意 初始高度0,目标值h,第i天目标值会下降i,当前高度会改变a[i%n],求高度不小于目标值的最早的时 ...

  8. 【 Gym - 101138D 】Strange Queries (莫队算法)

    BUPT2017 wintertraining(15) #4B Gym - 101138D 题意 a数组大小为n.(1 ≤ n ≤ 50 000) (1 ≤ q ≤ 50 000)(1 ≤ ai ≤  ...

  9. 【Gym - 101164I】Cubes(dfs,剪枝)

    BUPT2017 wintertraining(15) #4 A - I.Cubes Gym - 101164I 题意 将n拆成最少个立方数相加的形式. 题解 根据n的范围,立方数最大不超过400的立 ...

随机推荐

  1. Java虚拟机详解01----初识JVM

    主要内容如下: JVM的概念 JVM发展历史 JVM种类 Java语言规范 JVM规范 一.JVM的概念: JVM: Java Virtual Machine,意为Java虚拟机. 虚拟机: 指通过软 ...

  2. Unity使用 UnityVS+VS2013 调试脚本

    好消息:UnityVS免费啦 好消息:微软收购了UnityVS公司,UnityVS免费啦 下载地址:https://visualstudiogallery.msdn.microsoft.com/sit ...

  3. [3D跑酷] GUIManager UI管理

    UI元素更新及界面跳转 继上篇日志<Unity开发之 GUIClickEventReceiver>,再谈一下我们如何管理游戏中的UI元素更新及界面跳转 UI绑定 图一:Inspector面 ...

  4. java 21 - 15 新IO流 NIO

    1:JDK4  新IO要了解的类 Buffer(缓冲),Channer(通道) 2:JDK7  要了解的新IO类 Path:与平台无关的路径. Paths:包含了返回Path的静态方法. public ...

  5. String类详解(1)

    首先String是一个类. 1,实例化String类方法. 1)直接赋值:String name="haha"; 2)通过关键字:String name=new String(&q ...

  6. CSS规则的执行顺序(转)

    你对CSS规则的执行顺序是否了解,这里和大家分享一下,若两条规则具有相同的权值.起源及特殊性,那在样式表中最后出现的规则优先. 1.CSS规则之特殊性 首先来看一下这个例子将会发生的情形: <s ...

  7. Css 常用属性

    1. overflow:hidden和zoom:1 verflow:hidden;的作用 1. 隐藏溢出 :2.消除浮动 <style type="text/css"> ...

  8. Android View.onMeasure方法的理解

    View在屏幕上显示出来要先经过measure(计算)和layout(布局).1.什么时候调用onMeasure方法? 当控件的父元素正要放置该控件时调用.父元素会问子控件一个问题,“你想要用多大地方 ...

  9. 学习node.js 第4篇 建立一个最小的web聊天系统

    我们生活在一个实时的世界里,有什么比聊天更加实时吗?那就让我们先写一个基于TCP 的聊天服务器吧,并且支持Telnet 连接.这很容易,而且能够完全用Node来编写.首先,我们需要在Node 中包含T ...

  10. LUA __call

    1. ev={} . functin ev.__call() . print "called from ev" . end . . setmetatable(ev, ev) . . ...