题意:给你一个数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 组合数的更多相关文章

  1. F. Igor and Interesting Numbers

    http://codeforces.com/contest/747/problem/F cf #387 div2 problem f 非常好的一道题.看完题,然后就不知道怎么做,感觉是dp,但是不知道 ...

  2. CF747F Igor and Interesting Numbers

    我佛了,这CF居然没有官方题解. 题意:给定k,t,求第k小的16进制数,满足每个数码的出现次数不超过t. 解: 每个数都有个出现次数限制,搞不倒.一开始想到了排序hash数位DP,不过写了写觉得不胜 ...

  3. 算法笔记_093:蓝桥杯练习 Problem S4: Interesting Numbers 加强版(Java)

    目录 1 问题描述 2 解决方案   1 问题描述 Problem Description We call a number interesting, if and only if: 1. Its d ...

  4. java实现 蓝桥杯 算法提高 Problem S4: Interesting Numbers 加强版

    1 问题描述 Problem Description We call a number interesting, if and only if: 1. Its digits consists of o ...

  5. ural 2070. Interesting Numbers

    2070. Interesting Numbers Time limit: 2.0 secondMemory limit: 64 MB Nikolay and Asya investigate int ...

  6. Codeforces 385C Bear and Prime Numbers

    题目链接:Codeforces 385C Bear and Prime Numbers 这题告诉我仅仅有询问没有更新通常是不用线段树的.或者说还有比线段树更简单的方法. 用一个sum数组记录前n项和, ...

  7. Codeforces 1109D. Sasha and Interesting Fact from Graph Theory

    Codeforces 1109D. Sasha and Interesting Fact from Graph Theory 解题思路: 这题我根本不会做,是周指导带飞我. 首先对于当前已经有 \(m ...

  8. HDOJ(HDU).1058 Humble Numbers (DP)

    HDOJ(HDU).1058 Humble Numbers (DP) 点我挑战题目 题意分析 水 代码总览 /* Title:HDOJ.1058 Author:pengwill Date:2017-2 ...

  9. noj 2033 一页书的书 [ dp + 组合数 ]

    传送门 一页书的书 时间限制(普通/Java) : 1000 MS/ 3000 MS          运行内存限制 : 65536 KByte总提交 : 53            测试通过 : 1 ...

随机推荐

  1. webpack (1)

    node_modules/.bin/webpack 用webpack 打包 因为我们没有全局安装webpack 所以要用到这条命令 创建html npm install --save-dev html ...

  2. oracle死锁查询

    select sess.sid ||','|| sess.serial#, lo.oracle_username, lo.os_user_name, ao.object_name, lo.locked ...

  3. 字符串操作——C语言实现

    代码如下: #include <stdio.h> #include <stdlib.h> #include <string.h> #include <asse ...

  4. redis相关笔记(三.redis设计与实现(笔记))

    redis笔记一 redis笔记二 redis笔记三 1.数据结构 1.1.简单动态字符串: 其属性有int len:长度,int free:空闲长度,char[] bur:字符数组(内容) 获取字符 ...

  5. JAVA 输入输出程序开发

    参考: java中 静态方法和非静态方法的区别 字符流的输入和输出 java文件创建.删除.读取.写入操作大全 Java键盘输入并且写入文件 File类的isDiretory Java统计子串在字符串 ...

  6. printf 输出格式设置\033[47\033[5m 与-8.8s

    摘要:在使用linux终端命令的时候,我们可以看到像more命令,它的显示方式与一般的字符串不同,是用了反显.同样,linux C下printf还有很多其他不常见的格式化输出形式.本文主要为你盘点这些 ...

  7. POJ 2808 校门外的树(线段树入门)

    题目描述 某校大门外长度为L的马路上有一排树,每两棵相邻的树之间的间隔都是1米.我们可以把马路看成一个数轴,马路的一端在数轴0的位置,另一端在L的位置:数轴上的每个整数点,即0,1,2,……,L,都种 ...

  8. Python每日一题 008

    题目 基于多线程的网络爬虫项目,爬取该站点http://www.tvtv.hk 的电视剧收视率排行榜 分析 robots.txt User-agent: Yisouspider Disallow: / ...

  9. AcWing 215. 破译密码 (莫比乌斯反演)打卡

    达达正在破解一段密码,他需要回答很多类似的问题: 对于给定的整数a,b和d,有多少正整数对x,y,满足x<=a,y<=b,并且gcd(x,y)=d. 作为达达的同学,达达希望得到你的帮助. ...

  10. QTP学习笔记--define new test object

    目前在测的系统里,有图片控件,图片源都是一样的,链接地址不同(链接地址是动态变化的,不适合作为属性). QTP的识别机制是根据Index来的,复制对象之后更改Index的属性方法无效,采用define ...