HDU 2431 Counting Problem

题意: 问 n * n 的格子里面放 2*n个皇后的放法数,满足每行每列的皇后数都是2。

分析: 解法可以由前面的递推到后面,知道 2 * 2 的图有一种情况后,可知大于 2 * 2 的图都可以预留 2 * 2 的空间,对预留之后的 (N - 2) * (N - 2) 进行处理,以此类推

/***************************************
* File Name:2431.cpp
* Created Time:2013年12月14日 10:56:21
***************************************/
#include <map>
#include <cmath>
#include <queue>
#include <string>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 501;
const int mod = 1000007;
int f[maxn];

void init(){
    f[0] = 1;
    for (int i=2; i<maxn; i++){
        for (int j=i; j<maxn; j++){
            f[j] += f[j-i];
            if (f[j] >= mod){
                f[j] -= mod;
            }    
        }
    }
}
int main(){
    int T;
    int n;
    init();
    scanf("%d",&T);
    while (T--){
        scanf("%d",&n);
        printf("%d
",f[n]);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/sky0917/p/3474148.html