Codeforces Round #387 (Div. 2) 747F(数位DP)
题目大意
给出整数k和t,需要产生一个满足以下要求的第k个十六进制数
即十六进制数每一位上的数出现的次数不超过t
首先我们先这样考虑,如果给你了0~f每个数字可以使用的次数num[i],如何求长度为L且满足要求的十六进制数有多少个
dp[i][l]表示使用了前i个数字,已经将L的空位填上了l个的数有多少个
转移方程 dp[i][l] = sigma(dp[i-1][l-j]*C[len-l+j[j]) 其中j是枚举填新的数的个数,C是组合数(选出j个空位填上新数)
有了这个dp后,现在的问题就变成了找出第k个数
首先先确定位数,枚举位数,然后就可以找到第k个数的位数是多少
然后对从最高位开始枚举,确定每一位应该是多少
最后就可以得出答案
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
typedef long long LL;
const int maxl = ;
LL C[maxl][maxl], dp[][maxl];
int num[], t;
LL k;
void prepare()
{
for(int i = ; i < maxl; i++) C[i][] = ;
for(int i = ; i < maxl; i++)
for(int j = ; j <= i; j++)
C[i][j] = C[i-][j] + C[i-][j-];
} LL solve(int len)
{
memset(dp, , sizeof(dp));
for(int i = ; i <= num[]; i++) dp[][i] = C[len][i];
for(int i = ; i < ; i++)
for(int l = ; l <= len; l++)
for(int j = ; j <= min(num[i], l); j++)
dp[i][l] += dp[i-][l-j]*C[len-l+j][j];
return dp[][len];
}
void print(int j)
{
if(j < ) cout<<j;
else cout<<(char)(j+'a'-);
}
int main()
{
prepare();
cin>>k>>t;
for(int i = ; i < ; i++) num[i] = t;
int len = ;
for(;; len++)
{
LL tmp = ;
if(len == ) tmp = ;
else
for(int j = ; j < ; j++)
{
num[j]--;
tmp += solve(len-);
num[j]++;
}
if(k > tmp) k -= tmp;
else break;
}
for(int i = len; i > ; i--)
{
if(i == )
{
for(int j = ; j < ; j++)
{
if(j == && len == ) continue;
if(num[j] != ) k--;
if(k == ) { print(j); break; }
}
break;
}
for(int j = ; j < ; j++)
{
if(i == len && j == ) continue;
num[j]--;
LL tmp = solve(i-);
if(k > tmp) k -= tmp;
else
{
print(j);
break;
}
num[j]++;
}
}
}
Codeforces Round #387 (Div. 2) 747F(数位DP)的更多相关文章
- Educational Codeforces Round 53 E. Segment Sum(数位DP)
Educational Codeforces Round 53 E. Segment Sum 题意: 问[L,R]区间内有多少个数满足:其由不超过k种数字构成. 思路: 数位DP裸题,也比较好想.由于 ...
- Codeforces Round #131 (Div. 2) B. Hometask dp
题目链接: http://codeforces.com/problemset/problem/214/B Hometask time limit per test:2 secondsmemory li ...
- Educational Codeforces Round 8 D. Magic Numbers 数位DP
D. Magic Numbers 题目连接: http://www.codeforces.com/contest/628/problem/D Description Consider the deci ...
- Codeforces Round #131 (Div. 1) B. Numbers dp
题目链接: http://codeforces.com/problemset/problem/213/B B. Numbers time limit per test 2 secondsmemory ...
- Codeforces Round #276 (Div. 1) D. Kindergarten dp
D. Kindergarten Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/proble ...
- Codeforces Round #260 (Div. 1) A - Boredom DP
A. Boredom Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/455/problem/A ...
- Codeforces Round #533 (Div. 2) C.思维dp D. 多源BFS
题目链接:https://codeforces.com/contest/1105 C. Ayoub and Lost Array 题目大意:一个长度为n的数组,数组的元素都在[L,R]之间,并且数组全 ...
- Codeforces Round #539 (Div. 2) 异或 + dp
https://codeforces.com/contest/1113/problem/C 题意 一个n个数字的数组a[],求有多少对l,r满足\(sum[l,mid]=sum[mid+1,r]\), ...
- Codeforces Round #374 (Div. 2) C. Journey DP
C. Journey 题目连接: http://codeforces.com/contest/721/problem/C Description Recently Irina arrived to o ...
随机推荐
- iOS开发网络缓存原理
一.关于同一个URL的多次请求 有时候,对同一个URL请求多次,返回的数据可能都是一样的,比如服务器上的某张图片,无论下载多少次,返回的数据都是一样的. 上面的情况会造成以下问题 (1)用户流量的浪费 ...
- PCA 实例演示二维数据降成1维
import numpy as np # 将二维数据降成1维 num = [(2.5, 2.4), (0.5, 0.7), (2.2, 2.9), (1.9, 2.2), (3.1, 3.0), (2 ...
- tar工具(打包,压缩)
tar工具(打包,压缩)========================= tar打包工具 -c:表示建立一个tar包或者压缩文件包-x:表示解包或者解压缩-v:表示可视化-f: 后面跟文件名(即-f ...
- Oracle创建表空间,添加用户及授权
工具/原料 PLSQL 方法/步骤 1.在PLSQL里,用system/css(orcl系统用户)登陆,登陆的时候普通用户登陆选择normal就可以了 2.Oracle用户新增 执行语句 crea ...
- 关于PHPExcel 基础使用方法
$dir=dirname(__FILE__);//找到当前脚本所在路径require_once $dir.'/PHPExcel/PHPExcel.php';$objPHPExcel=new PHPEx ...
- 学习Pytbon第十天 函数2 内置方法和匿名函数
print( all([1,-5,3]) )#如果可迭代对象里所有元素都为真则返回真.0不为真print( any([1,2]) )#如果数据里面任意一个数据为真返回则为真a= ascii([1,2, ...
- C# 窗口关闭事件
首先添加一个退出事件函数 //退出按键 private void Form1_FormClosing(object sender, FormClosingEventArgs e) { DialogRe ...
- 笔记-python-多线程-深入-1
笔记-python-多线程-深入-1 1. 线程池 1.1. 线程池:控制同时存在的线程数量 threading没有线程池,只能自己控制线程数量. 基本有两种方式: 每间隔一段时间创建 ...
- 《Cracking the Coding Interview》——第9章:递归和动态规划——题目3
2014-03-20 03:01 题目:给定一个已按升序排序的数组,找出是否有A[i] = i的情况出现. 解法1:如果元素不重复,是可以严格二分查找的. 代码: // 9.3 Given a uni ...
- Python 3基础教程13-写入文件
前面介绍了函数,这篇我们就利用Python 内构函数open来写入字符串到txt文件里. 直接看demo.py 这里有一个小问题,如果我要输入时中文到txt文件会报unicode错误,暂时没法解决.