CF747F Igor and Interesting Numbers
我佛了,这CF居然没有官方题解。
题意:给定k,t,求第k小的16进制数,满足每个数码的出现次数不超过t。
解:
每个数都有个出现次数限制,搞不倒。一开始想到了排序hash数位DP,不过写了写觉得不胜其烦,就弃疗了。
但是思考一下,如果我们知道了每个数的出现次数和数的位数,那么一次普通DP就能够求出方案数。
所以我们暴力做多次这种普通DP即可......
具体来说,分为带前导0和不带前导0两个DP函数。
首先枚举数的长度,计算不带前导0的个数。如果不到k就减去。
然后知道了长度,再一位一位的确定。在每一位上枚举放哪个数码。如果方案数不足就减去这么多。
对于那个DP函数,状态设计f[i][j]表示用前i个数码放j位的数的方案数。转移就是
f[i][j] = f[i - 1][j - k] * C(j, k),表示在j个位置中选出k个放数码i,剩下的放前面的数码,前面的数码相对位置不变。
#include <cstdio>
#include <cstring>
#include <algorithm> typedef long long LL;
const int N = , B = ; LL f[][N], C[N][N], choose[N];
int rest[B]; inline LL DP(int n) { // with leading zero
if(n <= ) {
return ;
}
memset(f, , sizeof(f));
for(int i = ; i <= B; i++) {
f[i - ][] = ;
for(int j = ; j <= n; j++) {
for(int k = ; k <= rest[i - ] && k <= j; k++) {
f[i][j] += f[i - ][j - k] * C[j][k];
}
}
} return f[][n];
} inline LL DP1(int n) { // no leading zero
LL ans = ;
for(int i = ; i < B; i++) {
if(rest[i]) {
rest[i]--;
ans += DP(n - );
rest[i]++;
}
}
return ans;
} int main() {
for(int i = ; i < ; i++) {
C[i][] = C[i][i] = ;
for(int j = ; j < i; j++) {
C[i][j] = C[i - ][j] + C[i - ][j - ];
}
}
int t;
LL k;
scanf("%lld%d", &k, &t);
int len;
for(len = ; ; len++) {
for(int i = ; i < B; i++) {
rest[i] = t;
}
LL temp = DP1(len);
if(temp >= k) {
break;
}
k -= temp;
} for(int i = ; i < B; i++) {
rest[i] = t;
}
for(int i = len; i >= ; i--) {
for(int j = (i == len); j < B; j++) {
if(!rest[j]) {
continue;
}
rest[j]--;
LL temp = DP(i - );
if(temp < k) {
k -= temp;
rest[j]++;
}
else {
choose[i] = j;
break;
}
}
} for(int i = len; i >= ; i--) {
if(choose[i] < ) {
printf("%d", choose[i]);
}
else {
putchar('a' + choose[i] - );
}
}
return ;
}
AC代码
CF747F Igor and Interesting Numbers的更多相关文章
- F. Igor and Interesting Numbers
http://codeforces.com/contest/747/problem/F cf #387 div2 problem f 非常好的一道题.看完题,然后就不知道怎么做,感觉是dp,但是不知道 ...
- Codeforces 747F Igor and Interesting Numbers DP 组合数
题意:给你一个数n和t,问字母出现次数不超过t,第n小的16进制数是多少. 思路:容易联想到数位DP, 然而并不是...我们需要知道有多少位,在知道有多少位之后,用试填法找出答案.我们设dp[i][j ...
- ural 2070. Interesting Numbers
2070. Interesting Numbers Time limit: 2.0 secondMemory limit: 64 MB Nikolay and Asya investigate int ...
- 算法笔记_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 (找规律)
题意:在[L, R]之间求:x是个素数,因子个数是素数,同时满足两个条件,或者同时不满足两个条件的数的个数. 析:很明显所有的素数,因数都是2,是素数,所以我们只要算不是素数但因子是素数的数目就好,然 ...
- 【线性筛】【筛法求素数】【约数个数定理】URAL - 2070 - Interesting Numbers
素数必然符合题意. 对于合数,如若它是某个素数x的k次方(k为某个素数y减去1),一定不符合题意.只需找出这些数. 由约数个数定理,其他合数一定符合题意. 就从小到大枚举素数,然后把它的素数-1次方都 ...
- Ural 2070:Interesting Numbers(思维)
http://acm.timus.ru/problem.aspx?space=1&num=2070 题意:A认为如果某个数为质数的话,该数字是有趣的.B认为如果某个数它分解得到的因子数目是素数 ...
- HDU 6659 Acesrc and Good Numbers (数学 思维)
2019 杭电多校 8 1003 题目链接:HDU 6659 比赛链接:2019 Multi-University Training Contest 8 Problem Description Ace ...
随机推荐
- QueryRunner 错误
QueryRunner qr=new QueryRunner(JDBCUtils.getDataSource()); 写成了 QueryRunner qr = new QueryRunner(); 导 ...
- js中的call、apply、bind
在js中每个函数都包含两个非继承而来的方法:call()和apply() call和apply的作用都是在特定的作用域中将函数绑定到另外一个对象上去运行,即可以用来重新定义函数的执行环境,两者仅在定义 ...
- AssemblyScript的测试
详细文档介绍 export function f(x: i32): i32 { if (x === 1 || x === 2) { return 1; } return f(x - 1) + f(x ...
- shell中数组及其相关操作
转载 https://blog.csdn.net/jerry_1126/article/details/52027539
- linux操作命令 开发人员需要掌握的一些命令
1.man 查看帮助 2.命令 --help 简单帮助 3.help cd 查看一些Linux 命令行的一些内置命令 4.cp 粘贴复制命令 eg:cp yum.log /root/ 5.find ...
- Spring Boot 构建电商基础秒杀项目 (一) 项目搭建
SpringBoot构建电商基础秒杀项目 学习笔记 Spring Boot 其实不是什么新的框架,它默认配置了很多框架的使用方式,就像 maven 整合了所有的 jar 包, Spring Boot ...
- faster rcnn训练自己的数据集
采用Pascal VOC数据集的组织结构,来构建自己的数据集,这种方法是faster rcnn最便捷的训练方式
- [离散时间信号处理学习笔记] 9. z变换性质
z变换描述 $x[n] \stackrel{\mathcal{Z}}{\longleftrightarrow}X(z) ,\quad ROC=R_x$ 序列$x[n]$经过z变换后得到复变函数$X(z ...
- java基础之Number
1.Java是一个近乎纯洁的面向对象编程语言,但是为了编程的方便还是引入了基本数据类型,但是为了能够将这些基本数据类型当成对象操作,Java为每一个基本数据类型都引入了对应的包装类型(wrapper ...
- SpringBoot之get请求404
后台:SpringBoot 前台:VUE 异常:调get接口,返回404 场景:get请求传参,后台返回n条数据,不传参则返回所有 原因:原请求url为"~/one/{param}" ...