Counting Problem
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 532 Accepted Submission(s): 287
Problem Description
Yes. Counting problem comes again.
Given a chess board of N*N (N<=500), how many ways are there to put N queens on it so that no queen can attack another?
Luckily this is not the problem you should solve in this contest. Instead, we have a much easier one here.
Given a chess board of N*N (N<=500), how many ways are there to put 2*N queens on it so that there are exactly two queens in each row and each column?
To make it even easier, we will consider one way to be the same with another if it can be transformed to the other one by swapping rows and swapping columns.
Given a chess board of N*N (N<=500), how many ways are there to put N queens on it so that no queen can attack another?
Luckily this is not the problem you should solve in this contest. Instead, we have a much easier one here.
Given a chess board of N*N (N<=500), how many ways are there to put 2*N queens on it so that there are exactly two queens in each row and each column?
To make it even easier, we will consider one way to be the same with another if it can be transformed to the other one by swapping rows and swapping columns.
Input
The first line of the input is an integer T indicating the number of cases.
Line 2..T+1 will contain an positive integer N (N <=500) each.
Line 2..T+1 will contain an positive integer N (N <=500) each.
Output
T lines containing your counting results respectively.
For the number may be very large, you just have to output its module by 1000007.
For the number may be very large, you just have to output its module by 1000007.
Sample Input
3 2 5 4
Sample Output
1 2 2
Source
每一个n分成两个正方形
这两个正方形可以的任意的,不过要合起来是n的两个角
这两个正方形可以的任意的,不过要合起来是n的两个角
所以,比如 10, 可以是 分成2*2 跟 8*8
这时候就在第一个循环,i=2 那里算了一次,
分成3*3 7*7 的时候, 就在i的第二次循环(i==3)的时候算了一次
所以最终是可以算出所有情况的
j不从2开始,而是从i开始,这样就可以避免出现矩阵和他的转置被算重复的情况了
这时候就在第一个循环,i=2 那里算了一次,
分成3*3 7*7 的时候, 就在i的第二次循环(i==3)的时候算了一次
所以最终是可以算出所有情况的
j不从2开始,而是从i开始,这样就可以避免出现矩阵和他的转置被算重复的情况了
/*
* hdu 2431
* author : mazciwong
* creat on: 2016-1-15
*/
/*
递推, 比较难想到
题意:
给一个n*n的棋盘,往上面放2*n个棋子,使得每行每列都有且只有2个棋子,问有多少种放法.
思路:
每一个n*n的棋盘都可以分成a*a和b*b的棋盘,其中(a+b)=n且a>1,b>1,
所以可以直接从前面的合法情况推后面的.
*/
#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
#include <functional> //greater
#include <algorithm>
using namespace std;
typedef long long ll;
const int mod = 1000007;
const int maxn = 500 + 5;
int n;
int ans[maxn];
void init()
{
ans[0] = 1; ans[1] = 0;
for (int i = 2; i < maxn; i++)
for (int j = i; j < maxn; j++)
ans[j] = (ans[j] + ans[j - i]) % mod;
}
void solve()
{
printf("%d\n", ans[n]);
}
int main()
{
init();
int t;
scanf("%d", &t);
for (int i = 0; i < t; i++)
{
scanf("%d", &n);
solve();
}
return 0;
}