您的位置:首页 > 其它

bjfu1332 简单动规

2015-06-05 21:47 288 查看
挺简单的动态规划题。我用记忆化搜索打的。直接上代码:

/*
* Author    : ben
*/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
#include <stack>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <functional>
#include <numeric>
#include <cctype>
using namespace std;
typedef long long LL;
const int card[53] = {0, 1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,6,6,6,6,7,7,7,7,8,8,8,8,9,9,9,9,10,10,10,10,11,11,11,11,12,12,12,12,13,13,13,13};
int ans[53][60];
int dfs(int m, int n) {
if (n == 0) {
return 1;
}
if (m == 0) {
return 0;
}
if (ans[m]
> -1) {
return ans[m]
;
}
if (card[m] > n) {
ans[m]
= dfs(m - 1, n);
} else {
ans[m]
= dfs(m - 1, n - card[m]) + dfs(m - 1, n);
}
return ans[m]
;
}

int main() {
int T, n;
scanf("%d", &T);
memset(ans, -1, sizeof(ans));
while (T--) {
scanf("%d", &n);
printf("%d\n", dfs(52, n));
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: