hdu 2431 遞推

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.
 

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.
 

Output
      T lines containing your counting results respectively.
      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的兩個角

 所以,比如 10, 可以是 分成2*2 跟 8*8
這時候就在第一個循環,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;
}


發佈了72 篇原創文章 · 獲贊 14 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章