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 ...
随机推荐
- java基础 File与递归练习 使用文件过滤器筛选将指定文件夹下的小于200K的小文件获取并打印按层次打印(包括所有子文件夹的文件) 多层文件夹情况统计文件和文件夹的数量 统计已知类型的数量 未知类型的数量
package com.swift.kuozhan; import java.io.File; import java.io.FileFilter; /*使用文件过滤器筛选将指定文件夹下的小于200K ...
- java斗地主扑克 扑克牌 洗牌 发牌 Collection 集合练习
package com.swift.poker; import java.util.ArrayList; import java.util.Collections; /*训练考核知识点:Collect ...
- 交换机基础配置之三层交换机实现vlan间通信
我们以上面的拓扑图做实验,要求为pc1,pc2,pc3配置为vlan10,pc4,pc5,pc6配置为vlan20,pc7,pc8,pc9配置为vlan30 server0和server1配置为vla ...
- JZOJ 3534. 【NOIP2013提高组day1】货车运输
Description A 国有 n 座城市,编号从 1 到 n,城市之间有 m 条双向道路.每一条道路对车辆都有重量限制,简称限重.现在有 q 辆货车在运输货物,司机们想知道每辆车在不超过车辆限重的 ...
- css 自动换行,超出省略号代替
overflow : hidden; text-overflow: ellipsis; display: -webkit-box; -webkit-line-clamp: 2; ...
- list函数及list对象的reverse方法
list的reverse方法,是就地reverse,不返回值 如a是一个list,a.reverse()直接将a这个list给reverse了,所以如果print(a.reverse())就是None ...
- POJ:2229-Sumsets(完全背包的优化)
题目链接:http://poj.org/problem?id=2229 Sumsets Time Limit: 2000MS Memory Limit: 200000K Total Submissio ...
- Alter the structure of web pages with JavaScript
Most of the DOM methods you've seen so far are useful for identifying elements. Both getElementById ...
- Servlet过滤器---登录权限控制
实现了登录时权限控制:进入首页.登录页以及登录servlet时,不用验证权限:进入其它页面时,须验证是否登录,未登录则跳转到登录页. 一个简单的首页:index.jsp <%@ page lan ...
- BF算法(蛮力匹配算法)
将主串M指定位置和目标串S开始位置进行对比,如果相同将M的下一个字符和S的下一个字符对比,如果不同则M的下一个字符和S的开始位置对比,直到S中每一个字符和M中的连续字符串相等,否则不匹配. C#代码- ...