hdu 1799 记忆化搜索

首先要能推出公式,结果其实就是C(n,m),接下来求C(n,m)就用到了公式C(n,m)=C(n-1,m)+C(n-1,m-1)。我用记忆化搜索做的,觉得记忆化搜索写起来就是简捷啊~

/*
* hdu1799/win.cpp
* Created on: 2011-10-3
* Author : ben
*/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <queue>
using namespace std;
const int MAXNM = 2010;
int p = 1007;
int dp[MAXNM][MAXNM];

int comb(int n, int m) {
if (dp[n][m] >= 0) {
return dp[n][m];
}
int ret = (comb(n - 1, m) % p + comb(n - 1, m - 1) % p) % p;
dp[n][m] = ret;
return ret;
}

void init() {
memset(dp, -1, sizeof(dp));
for (int i = 1; i < MAXNM - 1; i++) {
dp[i][0] = dp[i][i] = 1;
}
}

int main() {
#ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
#endif
int n, m, T;
scanf("%d", &T);
init();
while (T--) {
scanf("%d%d", &m, &n);
if (m > n) {
puts("0");
} else {
printf("%d\n", comb(n, m));
}
}
return 0;
}


原文地址:https://www.cnblogs.com/moonbay/p/2198517.html