【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 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染色)的更多相关文章
- Codeforces Gym 100015B Ball Painting 找规律
Ball Painting 题目连接: http://codeforces.com/gym/100015/attachments Description There are 2N white ball ...
- 【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 ...
- AtCoder Grand Contest 012 B - Splatter Painting(dp)
Time limit : 2sec / Memory limit : 256MB Score : 700 points Problem Statement Squid loves painting v ...
- HDU 4362 Dragon Ball 贪心DP
Dragon Ball Problem Description Sean has got a Treasure map which shows when and where the dragon ...
- Codeforces Gym 100231L Intervals 数位DP
Intervals 题目连接: http://codeforces.com/gym/100231/attachments Description Start with an integer, N0, ...
- G - Surf Gym - 100819S -逆向背包DP
G - Surf Gym - 100819S 思路 :有点类似 逆向背包DP , 因为这些事件发生后是对后面的时间有影响. 所以,我们 进行逆向DP,具体 见代码实现. #include<bit ...
- 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 ...
- 【AtCoder】AGC022 F - Leftmost Ball 计数DP
[题目]F - Leftmost Ball [题意]给定n种颜色的球各k个,每次以任意顺序排列所有球并将每种颜色最左端的球染成颜色0,求有多少种不同的颜色排列.n,k<=2000. [算法]计数 ...
- Gym - 101615J Grid Coloring DP 2017-2018 ACM-ICPC Pacific Northwest Regional Contest (Div. 1)
题目传送门 题目大意: 给出n*m的网格,有红蓝两种颜色,每个格子都必须被染色,当一个格子被染成蓝色后,这个格子左上方的一块都必须被染成蓝色,问最后的方案数量. 思路: 按照题目条件,如果有一个格子被 ...
随机推荐
- excel保存为制表符分隔的文本文件 js无法完整读取
excel保存为制表符分隔的文本文件 js无法完整读取 excel另存为文本有两个选项,一个是制表符分隔的文本文件,一个是unicode文本.生成的文件Unicode更大一些.但是这里需要注意的是[制 ...
- VSCode Install Go
首先是VScode官网下载:https://code.visualstudio.com/ 然后在电脑上安装go的环境 如下图在VSCode上搜go的插件进行安装: 推荐:vscode-icons这个插 ...
- NOI.ac #8 小w、小j和小z LIS
传送门 题意:在一个数轴上,给出$N$个人的初始位置与速度(速度有方向),求最大的时间使得存在$N-K$个人在这一段时间内两两没有相遇.$1 \leq K \leq N \leq 10^5$ 显然有二 ...
- cssie7.0兼容
http://www.w3dev.cn/article/20140328/IE7-float-left-touch-border-inner-break.aspx
- Luogu P1439 【模板】最长公共子序列
又是模板题呵,但这次的难度有点增加. 先看题目第一个想到DP的经典算法,要O(n^2),然后想其它的算法. 其实我们衢州市一次联考有一题很像这题,不过还要难一点. 思想是离散化+最长不下降子序列(在这 ...
- 【JVM.8】类加载及执行子系统的案例与实战
一. 案例分析 1. Tomcat:正统的类加载器架构 主流的Java Web服务器,如Tomcat.Jetty.WebLogic.WebSphere或其他服务器,都实现了自己定义的类加载器(一般都不 ...
- easyui datagrid remoteSort的实现 Controllers编写动态的Lambda表达式 IQueryable OrderBy扩展
EF 结合easy-ui datagrid 实现页面端排序 EF动态编写排序Lambda表达式 1.前端页面 var mainListHeight = $(window).height() - 20; ...
- Webpack 2 视频教程 004 - Webpack 初体验
原文发表于我的技术博客 这是我免费发布的高质量超清「Webpack 2 视频教程」. Webpack 作为目前前端开发必备的框架,Webpack 发布了 2.0 版本,此视频就是基于 2.0 的版本讲 ...
- Salesforce随笔: 将Visualforce Page导出为 Excel/CSV/txt (Display a page in Excel)
想要实现如题所述功能,可以参照 : Visualforce Developer Guide 第57页中所举的例子,在<apex:page>标签中添加contentType属性. <a ...
- Linux内核分析作业 NO.8 完结撒花~~~
进程的切换和系统的一般执行过程 于佳心 原创作品转载请注明出处 <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-10000 ...