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(DP染色)的更多相关文章

  1. Codeforces Gym 100015B Ball Painting 找规律

    Ball Painting 题目连接: http://codeforces.com/gym/100015/attachments Description There are 2N white ball ...

  2. 【Gym 100015B】Ball Painting

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

  3. AtCoder Grand Contest 012 B - Splatter Painting(dp)

    Time limit : 2sec / Memory limit : 256MB Score : 700 points Problem Statement Squid loves painting v ...

  4. HDU 4362 Dragon Ball 贪心DP

    Dragon Ball Problem Description   Sean has got a Treasure map which shows when and where the dragon ...

  5. Codeforces Gym 100231L Intervals 数位DP

    Intervals 题目连接: http://codeforces.com/gym/100231/attachments Description Start with an integer, N0, ...

  6. G - Surf Gym - 100819S -逆向背包DP

    G - Surf Gym - 100819S 思路 :有点类似 逆向背包DP , 因为这些事件发生后是对后面的时间有影响. 所以,我们 进行逆向DP,具体 见代码实现. #include<bit ...

  7. Gym 102056I - Misunderstood … Missing - [DP][The 2018 ICPC Asia-East Continent Final Problem I]

    题目链接:https://codeforces.com/gym/102056/problem/I Warm sunshine, cool wind and a fine day, while the ...

  8. 【AtCoder】AGC022 F - Leftmost Ball 计数DP

    [题目]F - Leftmost Ball [题意]给定n种颜色的球各k个,每次以任意顺序排列所有球并将每种颜色最左端的球染成颜色0,求有多少种不同的颜色排列.n,k<=2000. [算法]计数 ...

  9. Gym - 101615J Grid Coloring DP 2017-2018 ACM-ICPC Pacific Northwest Regional Contest (Div. 1)

    题目传送门 题目大意: 给出n*m的网格,有红蓝两种颜色,每个格子都必须被染色,当一个格子被染成蓝色后,这个格子左上方的一块都必须被染成蓝色,问最后的方案数量. 思路: 按照题目条件,如果有一个格子被 ...

随机推荐

  1. java算法----排序----(5)归并排序

    package log; import java.util.Arrays; public class Test4 { /** * java算法---归并排序 * * @param args */ pu ...

  2. CF908D New Year and Arbitrary Arrangement 期望、DP

    题目传送门 题意:给出正整数$pa,pb,k$,最开始你有一个空串,每一次你有$\frac{pa}{pa + pb}$的概率向串最后放一个$a$,有$\frac{pb}{pa + pb}$的概率向串最 ...

  3. 大数据处理过程核心技术ETL详细介绍

    架构挑战 1.对现有数据库管理技术的挑战. 2.经典数据库技术并没有考虑数据的多类别(variety).SQL(结构化数据查询语言),在设计的一开始是没有考虑到非结构化数据的存储问题. 3.实时性技术 ...

  4. java 代码获取视频时长

    package test; import it.sauronsoftware.jave.Encoder; import it.sauronsoftware.jave.MultimediaInfo; i ...

  5. 《DISTRIBUTED SYSTEMS Concepts and Design》读书笔记 一

    第二章 系统模型 描述分布式系统的三种模型 Physical models : 用机器,网络,硬件等语言去描述整个系统. Architectural models : 用计算.计算任务.计算单元等语言 ...

  6. GeForce Experience关闭自动更新

    GeForce Experience驱动更新很烦,而且有时更新后就打不开了,找到种方法关闭更新 1.安装并登陆 2.打开 C:\ProgramData\NVIDIA Corporation 3.进入D ...

  7. Week2 关于代码规范的一些认识

    代码规范 我觉得代码规范是有必要的,而对于以下的四个观点我要提出自己的反驳: 这些规范都是官僚制度下产生的浪费大家的编程时间.影响人们开发效率, 浪费时间的东西 首先应该明白,什么是“编码规范”?它不 ...

  8. 语音笔记:CTC

    CTC全称,Connectionist temporal classification,可以理解为基于神经网络的时序类分类.语音识别中声学模型的训练属于监督学习,需要知道每一帧对应的label才能进行 ...

  9. SqlDataAdapter简单介绍 (转)

    From:  http://blog.sobnb.com/u/92/5532.html 一.特点介绍 1.表示用于填充 DataSet 和更新 SQL Server 数据库的一组数据命令和一个数据库连 ...

  10. poj 1723 SOLDIERS 带权中位数

    题目 http://poj.org/problem?id=1723 题解 带权中位数类型的题目~ 可以先考虑降维,最后集合的y坐标,明显是y坐标的中位数的位置,容易求出y方向的贡献res_y.比较麻烦 ...