# Codeforces Round #387 (Div. 2)

Igor喜欢十六进制数，不过并不是所有的十六进制数码他都感兴趣，对于Igor来说，一个十六进制数码让他感兴趣，当且仅当这个数码的每种数字('1'~'f')的出现次数不大于t，现在Igor想知道，给定k,t，第k大的让他满意的十六进制数码是什么？ k <= 2E9,t <= 10，显然这样的数码一定是存在的，注意数码不能含有前导0

solution：

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<algorithm>
#include<cmath>
using namespace std;

const int N = 16;
typedef long long LL;
const char s[N] = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};

LL k,t,D,C[N][N],f[N][N],res[N];
char Ans[N];

LL Dp(int M)
{
if (!M) return 1;
memset(f,0,sizeof(f));
for (int i = 0; i < N; i++)
for (int j = 1; j <= res[i]; j++)
f[j][i] = 1;
for (int i = 1; i < M; i++)
for (int j = 0; j < N; j++)
{
if (!f[i][j]) continue;
for (int k = j + 1; k < N; k++)
for (int len = 1; len <= res[k]; len++)
{
if (i + len > M) break;
f[i+len][k] += f[i][j]*C[i+len][len];
}
}
LL ret = 0;
for (int i = 0; i < N; i++)
ret += f[M][i];
return ret;
}

int main()
{
#ifdef DMC
freopen("DMC.txt","r",stdin);
#endif

cin >> k >> t; C[0][0] = 1;
for (int i = 1; i < N; i++)
{
C[i][0] = 1;
for (int j = 1; j <= i; j++)
C[i][j] = C[i-1][j] + C[i-1][j-1];
}
for (int i = 0; i < N; i++) res[i] = t;

res[1] = t - 1;
for (int i = 1; i < N; i++)
{
LL ret = Dp(i - 1)*15LL;
if (k > ret) k -= ret;
else {D = i; break;}
}
res[1] = t;
for (int i = D; i; i--)
{
int Start = (i == D) ? 1 : 0;
for (int j = Start; j < N; j++)
{
if (!res[j]) continue;
--res[j]; LL ret = Dp(i - 1);
if (k > ret) k -= ret,++res[j];
else {putchar(s[j]); break;}
}
}
return 0;
}

#### Codeforces Round #444 (Div. 2)-贪心&尺取-Ratings and Reality Shows

2017-11-11 20:11:11

#### Educational Codeforces Round 36 (Rated for Div. 2) 题解

2018-01-13 23:16:24

#### Codeforces Round #471 div.2 ABC题解

2018-03-24 14:16:58

#### Codeforces Round #446 (Div. 2) D. Gluttony

2017-11-18 21:07:25

#### Codeforces Round #476 (Div. 2) [Thanks, Telegram!] D - Single-use Stones（思维）

2018-04-27 00:50:37

#### Codeforces Round #426 (Div. 2) D. The Bakery（DP+线段树）

2017-07-31 09:16:44

#### Codeforces Round #448 (Div. 2) ABC

2017-11-27 14:47:02

#### CF-Codeforces Round #483 (Div. 2)-D-XOR-pyramid

2018-05-16 01:15:04

#### Codeforces Round #485 (Div. 2) ABCDE

2018-05-30 11:26:56

#### Educational Codeforces Round 38 (Rated for Div. 2) B. Run For Your Prize

2018-02-17 13:35:37