Codeforces 747F Igor and Interesting Numbers DP 组合数
题意:给你一个数n和t,问字母出现次数不超过t,第n小的16进制数是多少。
思路:容易联想到数位DP, 然而并不是。。。我们需要知道有多少位,在知道有多少位之后,用试填法找出答案。我们设dp[i][j]为考虑前i种字母,已经占了j个位置的方案数。那么dp[i][j] += dp[i - 1][j - k] * C[len - j + k][k],k的范围为[0, limit[k]]。意思是我们暴力枚举第i个字母放多少个,然后排列组合。
这个题有一个细节需要注意,因为最高为一定不为0,所以我们试填的时候可以直接把最高位为0的所有数提前减掉,然后试填的时候跳过最高位为0的情况。最高位为0的所有数在找多少位的时候顺便就算出来了。
代码:
#include <bits/stdc++.h>
#define LL long long
using namespace std;
int limit[20];
LL C[20][20], dp[20][20];
void out(int x) {
if(x < 10) printf("%d", x);
else printf("%c", x - 10 + 'a');
}
LL solve(int len, int flag) {
if(len == 0) return 1;
memset(dp, 0, sizeof(dp));
for (int i = 0; i <= len && i <= limit[0]; i++)
dp[0][i] = C[len][i];
for (int i = 1; i < 16; i++)
for (int j = 0; j <= len; j++) {
for (int k = 0; k <= j && k <= limit[i]; k++) {
dp[i][j] += dp[i - 1][j - k] * C[len - j + k][k];
}
}
return dp[15][len];
}
int res[20];
int main() {
LL n, ans;
int m;
scanf("%lld%d", &n, &m);
ans = n;
for (int i = 0; i < 16; i++) limit[i] = m;
for (int i = 0; i <= 15; i++) C[i][0] = 1;
for (int i = 1; i <= 15; i++)
for (int j = 1; j <= i; j++)
C[i][j] = C[i - 1][j - 1] + C[i - 1][j];
int pos = -1;
for (int i = 0; i >= 0; i++) {
LL tmp = 0;
for (int j = 1; j < 16; j++) {
limit[j]--;
tmp += solve(i, j);
limit[j]++;
if(tmp >= ans) {
pos = i;
break;
}
}
if(tmp >= ans) break;
else ans -= tmp;
}
n = ans;
for (int i = pos; i >= 0 ; i--) {
for (int j = 0; j < 16; j++) {
if(i == pos && j == 0) continue;
if(limit[j] == 0) continue;
limit[j]--;
LL tmp = solve(i, j);
limit[j]++;
if(tmp < n) {
n -= tmp;
} else {
res[i] = j;
limit[j]--;
break;
}
}
}
for (int i = pos; i >= 0; i--) {
out(res[i]);
}
printf("\n");
}
Codeforces 747F Igor and Interesting Numbers DP 组合数的更多相关文章
- F. Igor and Interesting Numbers
http://codeforces.com/contest/747/problem/F cf #387 div2 problem f 非常好的一道题.看完题,然后就不知道怎么做,感觉是dp,但是不知道 ...
- CF747F Igor and Interesting Numbers
我佛了,这CF居然没有官方题解. 题意:给定k,t,求第k小的16进制数,满足每个数码的出现次数不超过t. 解: 每个数都有个出现次数限制,搞不倒.一开始想到了排序hash数位DP,不过写了写觉得不胜 ...
- 算法笔记_093:蓝桥杯练习 Problem S4: Interesting Numbers 加强版(Java)
目录 1 问题描述 2 解决方案 1 问题描述 Problem Description We call a number interesting, if and only if: 1. Its d ...
- java实现 蓝桥杯 算法提高 Problem S4: Interesting Numbers 加强版
1 问题描述 Problem Description We call a number interesting, if and only if: 1. Its digits consists of o ...
- ural 2070. Interesting Numbers
2070. Interesting Numbers Time limit: 2.0 secondMemory limit: 64 MB Nikolay and Asya investigate int ...
- Codeforces 385C Bear and Prime Numbers
题目链接:Codeforces 385C Bear and Prime Numbers 这题告诉我仅仅有询问没有更新通常是不用线段树的.或者说还有比线段树更简单的方法. 用一个sum数组记录前n项和, ...
- Codeforces 1109D. Sasha and Interesting Fact from Graph Theory
Codeforces 1109D. Sasha and Interesting Fact from Graph Theory 解题思路: 这题我根本不会做,是周指导带飞我. 首先对于当前已经有 \(m ...
- HDOJ(HDU).1058 Humble Numbers (DP)
HDOJ(HDU).1058 Humble Numbers (DP) 点我挑战题目 题意分析 水 代码总览 /* Title:HDOJ.1058 Author:pengwill Date:2017-2 ...
- noj 2033 一页书的书 [ dp + 组合数 ]
传送门 一页书的书 时间限制(普通/Java) : 1000 MS/ 3000 MS 运行内存限制 : 65536 KByte总提交 : 53 测试通过 : 1 ...
随机推荐
- phpredis中incr以及decr等自增命令出现的问题
在做项目中使用redis的incr以及hincrby自增时,出现自增失败,set之后的数据,无法自增,当redis中不存在该key时,直接用incr是成功的.查找了原因,是因为phpredis初始化的 ...
- ARM与X86 CPU架构区别
CISC(复杂指令集计算机)和RISC(精简指令集计算机)是当前CPU的两种架构.它们的区别在于不同的CPU设计理念和方法.早期的CPU全部是CISC架构,它的设计目的是 CISC要用最少的机器语言指 ...
- MD相关语法
原文链接:https://www.jianshu.com/p/96ecaa2cc989 标题 一个#表示一级标题,最多6个表示6级标题 h1 h2 h3 h4 h5 h6 列表 无序列表,用 * + ...
- 1.MVC的初步了解
1.MVC简单介绍 1)原理(如图)如果想更加深入了解原理,可以访问此地址(https://www.cnblogs.com/uicodeintoworld/p/8950877.html) 理解:客户端 ...
- 深入理解java虚拟机:笔记
1.运行时数据区域 1.程序计数器 当前线程执行字节码的行号指示器,字节码解释器工作通过改变这个计数器的值来选取下一条需要执行的字节码指令,每一个线程拥有独立的程序计数器,线程私有的内存 2.虚拟机栈 ...
- linux软件包rpm的使用
一rpm包管理器 (一)rpm的介绍 rpm不仅是文件的后缀,也是一个工具,外部命令,程序包管理器 功能:将编译好的应用程序的各组成文件打包一个或几个程序包文件,从而方便快捷地实现程序包的安装.卸载. ...
- I2C自编设备驱动设计
一.自编设备驱动模型 at24.c: static int __init at24_init(void) { io_limit = rounddown_pow_of_two(io_limit); re ...
- jdbc blob插入及查询操作
首先建一张表 create table picture( picId ) primary key not null, picName ) not null, picfile image null ) ...
- Database基础(六):实现MySQL读写分离、MySQL性能调优
一.实现MySQL读写分离 目标: 本案例要求配置2台MySQL服务器+1台代理服务器,实现MySQL代理的读写分离: 用户只需要访问MySQL代理服务器,而实际的SQL查询.写入操作交给后台的2台M ...
- Service系统服务(三):查看进程信息、进程调度及终止、系统日志分析、使用systemctl工具
一.查看进程信息 目标: 本例要求掌握查看进程信息的操作,使用必要的命令工具完成下列任务: 找出进程 gdm 的 PID 编号值 列出由进程 gdm 开始的子进程树结构信息 找出进程 sshd 的父进 ...