Counting Problem
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 532 Accepted Submission(s): 287
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.
Line 2..T+1 will contain an positive integer N (N <=500) each.
For the number may be very large, you just have to output its module by 1000007.
這兩個正方形可以的任意的,不過要合起來是n的兩個角
這時候就在第一個循環,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;
}