UVA 10581 - Partitioning for fun and profit(数论递推)
10581 - Partitioning for fun and profit
题意:给定m, n,表示分配给n个格子,分配m个数字进去,每一个格子最少1,而且序列要是递增的,问第k个字典序的序列是什么
思路:先利用dp打出表,dp[i][j][k]表示第i个数,尾巴为j,总和剩下k的情况,写一个记忆化求出,之后在这个数组基础上,从左往右枚举要放那个数字合适,合适的就放进去而且输出,注意最后一个数字要单独输出。
代码:
#include <cstdio>
#include <cstring> typedef long long LL;
int t, M, n, vis[15][225][225];
LL k, dp[15][225][225]; LL DP(int now, int tail, int m) {
LL &ans = dp[now][tail][m];
if (vis[now][tail][m]) return ans;
vis[now][tail][m] = 1;
ans = 0;
if (now == n) return ans = 1;
if (now == n - 1) {
if (m >= tail)
return ans = DP(now + 1, m, 0);
return ans = 0;
}
for (int i = tail; i <= m - (n - now - 1); i++)
ans += DP(now + 1, i, m - i);
return ans;
} void solve(LL k) {
int v = 1;
for (int i = 1; i < n; i++) {
for (int j = v; j <= M - (n - i); j++) {
if (k > dp[i][j][M - j]) k -= dp[i][j][M - j];
else {
M -= j;
v = j;
printf("%d\n", j);
break;
}
}
}
printf("%d\n", M);
} int main() {
scanf("%d", &t);
while (t--) {
memset(vis, 0, sizeof(vis));
scanf("%d%d%lld", &M, &n, &k);
DP(0, 1, M);
solve(k);
}
return 0;
}
UVA 10581 - Partitioning for fun and profit(数论递推)的更多相关文章
- uva 10581 - Partitioning for fun and profit(记忆化搜索+数论)
题目链接:uva 10581 - Partitioning for fun and profit 题目大意:给定m,n,k,将m分解成n份,然后依照每份的个数排定字典序,而且划分时要求ai−1≤ai, ...
- Leetcode 119 Pascal's Triangle II 数论递推
杨辉三角,这次要输出第rowIndex行 用滚动数组t进行递推 t[(i+1)%2][j] = t[i%2][j] + t[i%2][j - 1]; class Solution { public: ...
- Leetcode 118 Pascal's Triangle 数论递推
杨辉三角,即组合数 递推 class Solution { vector<vector<int>> v; public: Solution() { ; i < ; ++i ...
- HDU 4834 JZP Set(数论+递推)(2014年百度之星程序设计大赛 - 初赛(第二轮))
Problem Description 一个{1, ..., n}的子集S被称为JZP集,当且仅当对于任意S中的两个数x,y,若(x+y)/2为整数,那么(x+y)/2也属于S.例如,n=3,S={1 ...
- UVa 10934 Dropping water balloons:dp(递推)
题目链接:https://vjudge.net/problem/27377/origin 题意: 有一栋n层高的楼,并给你k个水球.在一定高度及以上将水球扔下,水球会摔破:在这个高度以下扔,水球不会摔 ...
- UVa LA 3882 - And Then There Was One 递推,动态规划 难度: 2
题目 https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_pr ...
- uva 10328 - Coin Toss 投硬币(dp递推,大数)
题意:抛出n次硬币(有顺序),求至少k个以上的连续正面的情况的种数. 思路:转换成求抛n个硬币,至多k-1个连续的情况种数,用所有可能出现的情况种数减去至多k-1个的情况,就得到答案了.此题涉及大数加 ...
- 【uva 12627】Erratic Expansion(算法效率--递推)
题意:初始1个红气球,每小时后,1个红气球会变成3个红气球和1个蓝气球,而1个蓝气球会变成4个蓝气球.问经过N小时后,第L~R行一共有多少个红气球. 解法:问行数就定义f[i][j]表示 i 小时后前 ...
- UVA - 11584 Partitioning by Palindromes[序列DP]
UVA - 11584 Partitioning by Palindromes We say a sequence of char- acters is a palindrome if it is t ...
随机推荐
- python 字典有序无序及查找效率,hash表
刚学python的时候认为字典是无序,通过多次插入,如di = {}, 多次di['testkey']='testvalue' 这样测试来证明无序的.后来接触到了字典查找效率这个东西,查了一下,原来字 ...
- USB接口的SmartCard Class协议标准:ICCD and CCID
ICCD是 Intergrated Circuit(s) card Device 的缩写.CCID是 Integrated Circuit(s) cards interface devices的缩写I ...
- 第二节 EAN 8 码 / EAN 13 码
EAN码的全名为欧洲商品条码(European Article Number),源於西元1977年,由欧洲十二个工业国家所共同发展出来的一种条码.目前已成为一种国际性的条码系统.EAN条码系统的管理是 ...
- 一个i++和++i导致的严重的错误
当我曾经在写一个strlen的实现时,用递归写出了如下的代码: int strlen(const char *s) { if(*s=='\0') ; else ; } 程序一运行就崩溃了,why!都是 ...
- java学习之内省
反射加内省解决耦合问题 package com.gh.introspector; /** * JavaBean * @author ganhang * */ public class Dog { pr ...
- poj 2363 Blocks(暴力)
题目链接:http://poj.org/problem?id=2363 思路分析:由于数据较小,采用暴力搜索法.假设对于矩形边长 1 <= a <= b <= c <= N; ...
- uva-11995 - I Can Guess the Data Structure!(栈,优先队列,队列,水题)
11995 - I Can Guess the Data Structure! There is a bag-like data structure, supporting two operation ...
- 简单浮点数除法模拟-hdu-4493-Tutor
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4493 题目意思: 给小数点后两位的12个月的工资,求出平均工资,输出离小数点后第二位最近的两位小数, ...
- Android:源码环境编译自定义的APP到ROM(System Image)中
有时候我们需要在源码环境中增加自己的应用或模块,随ROM一起发布. 下面讲述相关步骤: 1. 首先可以在SDK环境下进行编码设计(如果你的APP不涉及到emulator无法模拟的硬件的话) 也可以参考 ...
- js php xmlrequest 上传图片
本来想用插件上传图片的,后来自己写了一个简单的js实现异步的图片上传,不多说上代码很easy upload.php <?php if(isset($_FILES["myfile&quo ...