LuoguP1240 诸侯安置

本来是来练组合的,不知怎么又开始水普及DP了

#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#define R(a,b,c) for(register int a = (b); (a) <= (c); ++(a))
#define nR(a,b,c) for(register int a = (b); (a) >= (c); --(a))
#define Fill(a,b) memset(a, b, sizeof(a))
#define Max(a,b) ((a) > (b) ? (a) : (b))
#define Min(a,b) ((a) < (b) ? (a) : (b))
#define Swap(a,b) ((a) ^= (b) ^= (a) ^= (b))
#define ll long long
#define u32 unsigned int
#define u64 unsigned long long

#define ON_DEBUGG

#ifdef ON_DEBUGG

#define D_e_Line printf("
----------
") 
#define D_e(x) cout << (#x) << " : " << x << endl
#define Pause() system("pause")
#define FileOpen() freopen("in.txt", "r", stdin)
#define FileSave() freopen("out.txt", "w", stdout)
#include <ctime>
#define TIME() fprintf(stderr, "
time: %.3fms
", clock() * 1000.0 / CLOCKS_PER_SEC);
 
#else

#define D_e_Line ;
#define D_e(x) ;
#define Pause() ;
#define FileOpen() ;
#define FileSave() ;
#define TIME() ;
//char buf[1 << 21], *p1 = buf, *p2 = buf;
//#define getchar() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++)

#endif

using namespace std;
struct ios{
    template<typename ATP>inline ios& operator >> (ATP &x){
        x = 0; int f = 1; char ch;
        for(ch = getchar(); ch < '0' || ch > '9'; ch = getchar()) if(ch == '-') f = -1;
        while(ch >= '0' && ch <= '9') x = x * 10 + (ch ^ '0'), ch = getchar();
        x *= f;
        return *this;
    }
}io;

template<typename ATP>inline ATP max(ATP &a, ATP &b){
    return a > b ? a : b;
}
template<typename ATP>inline ATP min(ATP &a, ATP &b){
    return a < b ? a : b;
}
template<typename ATP>inline ATP abs(ATP &a){
    return a < 0 ? -a : a;
}

const int N = 1007;
const int mod = 504;

int num[N], f[N][N];
int main(){
	int n, K;
	io >> n >> K;
	if(K > 2 * n - 1){
		printf("0");
		return 0;
	}
	if(K == 0){
		printf("1");
		return 0;
	}
	int m = 2 * n - 1;
	num[1] = num[2] = 1;
	R(i,3,m){
		num[i] = num[i - 2] + 2;
	}
	R(i,0,m) f[i][0] = 1;
	R(i,1,m){
		R(j,1,K){
			f[i][j] = (f[i - 1][j] + f[i - 1][j - 1] * (num[i] - j + 1)) % mod;
		}
	}
	
	printf("%d", (f[m][K] + mod) % mod);
	
	return 0;
}
/*
9 6 

4 6 4
*/

原文地址:https://www.cnblogs.com/bingoyes/p/11536688.html