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 ...
随机推荐
- redis设置防火墙的问题
Linux 下载安装配置Redis完整步骤 安装: 1.获取redis资源 wget http://download.redis.io/releases/redis-4.0.8.tar.gz 2. ...
- github & markdown & collapse & table
github & markdown collapse & table https://github.com/Microsoft/TypeScript/issues/30034 GitH ...
- How to enable mp3 on Ubuntu
apt install gstreamer1.0 libavcodec57
- SharePoint Server 2016 - Configure Office Online Server
Step 1: Create the binding between SharePoint 2016 and Office Web Apps Server To get started, open ...
- Jquery根据滚动条显示返回按钮
<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8& ...
- mysql 自带的性能压力测试工具
mysqlslap -h127.0.0.1 -uroot -p --concurrency=100 --iterations=1 --auto-generate-sql --auto-generate ...
- BZOJ2829信用卡凸包——凸包
题目描述 输入 输出 样例输入 2 6.0 2.0 0.0 0.0 0.0 0.0 2.0 -2.0 1.5707963268 样例输出 21.66 提示 本样例中的2张信用卡的轮廓在上图中用实线标出 ...
- BZOJ2040[2009国家集训队]拯救Protoss的故乡——模拟费用流+线段树+树链剖分
题目描述 在星历2012年,星灵英雄Zeratul预测到他所在的Aiur行星在M天后会发生持续性暴雨灾害,尤其是他们的首都.而Zeratul作为星灵族的英雄,当然是要尽自己最大的努力帮助星灵族渡过这场 ...
- SpringBoot整合ssm
1.创建工程 使用idea可以快速创建SpringBoot的工程 这里选择常用的类库,SpringBoot将各种框架类库都进行了封装,可以减少pom文件中的引用配置: 比如Spring和Mybatis ...
- ☆ [HDU2089] 不要62「数位DP」
类型:数位DP 传送门:>Here< 题意:问区间$[n,m]$的数字中,不含4以及62的数字总数 解题思路 数位DP入门题 先考虑一般的暴力做法,整个区间扫一遍,判断每个数是否合法并累计 ...