uva 10581 - Partitioning for fun and profit(记忆化搜索+数论)
来源:互联网 发布:淘宝床上四件套特价 编辑:程序博客网 时间:2024/06/07 01:51
题目链接:uva 10581 - Partitioning for fun and profit
题目大意:给定m,n,k,将m分解成n份,然后按照每份的个数排定字典序,并且划分时要求ai−1≤ai,然后输出字典序排在k位的划分方法。
解题思路:因为有ai−1≤ai的条件,所以先记忆化搜索处理出组合情况dp[i][j][s]表示第i位为j,并且剩余的未划分数为s的总数为dp[i][j][s],然后就是枚举每一位上的值,判断序列的位置即可。
#include <cstdio>#include <cstring>#include <algorithm>using namespace std;typedef long long ll;const int maxn = 220;const int maxp = 10;int M, N;ll K, dp[maxp+5][maxn+5][maxn+5];ll dfs (int d, int x, int s) { if (d == N) { if (0 == s) return 1; else return 0; } ll& ans = dp[d][x][s]; if (ans != -1) return ans; ans = 0; for (int i = x; ; i++) { if ((N-d) * i > s) break; ans += dfs(d+1, i, s-i); } return ans;}void solve () { int s = M, t = 1; for (int i = 1; i < N; i++) { for (int j = t; ; j++) { ll u = dp[i][j][s-j]; if (K > u) { K -= u; } else { printf("%d\n", j); s -= j; t = j; break; } } } printf("%d\n", s);}int main () { int cas; scanf("%d", &cas); while (cas--) { scanf("%d%d%lld", &M, &N, &K); memset(dp, -1, sizeof(dp)); for (int i = 1; i * N <= M; i++) ll u = dfs(1, i, M-i); solve(); } return 0;}
1 0
- uva 10581 - Partitioning for fun and profit(记忆化搜索+数论)
- UVA 10581 - Partitioning for fun and profit(数论递推)
- UVA 10581 Partitioning for fun and profit
- UVA 10581-Partitioning for fun and profit(DP)
- poj 2522 Partitioning for fun and profit
- Smashing The Stack For Fun And Profit
- Smashing The Stack For Fun And Profit
- Method Replacement for Fun and Profit
- Git Hooks for Fun and Profit
- Using Uninitialized Memory for Fun and Profit
- 总结:Distributed systems for fun and profit
- [转载]Smashing The Stack For Fun And Profit
- [译文]Smashing The Stack For Fun And Profit
- 堆栈溢出:Smashing The Stack For Fun And Profit
- 堆栈溢出:smashing the stack for fun and profit[译文]
- Abuse of the Linux Kernel for Fun and Profit
- Smashing the Stack for Fun and Profit by Aleph One
- Faking Touch Events on iOS for Fun and Profit
- C语言中的extern关键字的用法
- 黑马程序员---笔记
- ACM 集训总结
- TX Lock的几种情况
- 多线程执行顺序
- uva 10581 - Partitioning for fun and profit(记忆化搜索+数论)
- HDU-3255-Farming
- 机器学习:朴素贝叶斯算法
- Tomcat的安装与配置
- 3399: [Usaco2009 Mar]Sand Castle城堡
- 黑马程序员_Java RMI
- 表单 对数据库字段自动加密解密表单提交(THINKPHP3.2)
- HDU:A + B Problem II
- Android 线程学习之 Runnable和Thread的区别