"Gym 100015B" Ball Painting

Source: Internet
Author: User

Problem

There is 2N white balls to a table in both 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 of the balls black, but he would like to has 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 is too easy. So, he
Introduced some rules to make the problem more interesting.
the first ball that Jon paints can is any one of the 2N balls.
after that, each subsequent ball he paints must is adjacent to some black ball (that is already
Painted). Balls is assumed to be adjacent if they is next to all other horizontally, vertically,
or diagonally.
Jon is 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 is:
2
24
480

Test instructions

give you two rows n column of 2*n ball, first you randomly choose a black, and then must be in the ball adjacent to the black ball to choose a re-painted black, can be diagonally adjacent, to finish painting 2n ball how many kinds of coating method.

Analysis

Recursive no way, only dynamic planning, f[i][j], the dyeing length of the two lines of the matrix, the number of J-ball dyeing, dyeing length refers to the continuous column I each column at least one ball dyed, according to the rules of the ball must be in a dyeing matrix, there will be no separated by a row of colored balls.

State transition equation:

f[i][j]+=f[i][j-1]* (2*i-(j-1)) said that in the I joins choose not to dye the ball 2*i-(j-1) for dyeing, they can certainly stain.

F[i][j]+=f[i-1][j-1]*4 says that it can choose a staining matrix of I-column after staining from the left or the right four balls from the outside of the staining matrix in one less column.

Code
#include <stdio.h> #define N 1005#define M 1000000007long long dp[n][n],n;int Main () {for    (int i=1;i<=1000;i + +) for    (int j=i;j<=2*i;j++)        if (i==1) dp[i][j]=2;        else dp[i][j] = dp[i][j-1] * (2*i-j+1)%M + dp[i-1][j-1]*4%M;    while (scanf ("%i64d", &n) &&n)        printf ("%i64d\n", Dp[n][2*n]);    return 0;}

"Gym 100015B" Ball Painting

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.