题目链接:点击这里
题目大意:
给出一个
16
16
16 进制数
n
n
n ,求
[
1
,
n
]
[1,n]
[1,n] 内有多少个
16
16
16 进制数由
k
k
k 个不同的数字组成
题目分析:
数位
d
p
dp
dp
对于一个数由多少种不同的数字组成这个条件,我们可以采取状态压缩的方式记录,在高位向低位转移时把该位的贡献算入即可,当递归到最底层是,判断该状态是否有精确的
k
k
k 个一即可
注意
0
0
0 在作为前导零时是不能算作组成数字的,因此我们在转移时应根据其是否是前导零来进行转移
具体细节见代码:
//#pragma GCC optimize(2)
//#pragma GCC optimize("Ofast","inline","-ffast-math")
//#pragma GCC target("avx,sse2,sse3,sse4,mmx")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<queue>
#define ll long long
#define inf 0x3f3f3f3f
#define int ll
#define endl '\n'
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
using namespace std;
int read()
{
int res = 0,flag = 1;
char ch = getchar();
while(ch<'0' || ch>'9')
{
if(ch == '-') flag = -1;
ch = getchar();
}
while(ch>='0' && ch<='9')
{
res = (res<<3)+(res<<1)+(ch^48);//res*10+ch-'0';
ch = getchar();
}
return res*flag;
}
const int maxn = 1e6+5;
const int mod = 1e9+7;
const double pi = acos(-1);
const double eps = 1e-8;
int n,k,a[maxn],dp[maxn][17][2];
string s;
int popcount(int num)
{
int res = 0;
while(num)
{
res += num&1;
num >>= 1;
}
return res;
}
int dfs(int pos,int sta,bool f1,bool lead)
{
int cnt = popcount(sta);
if(pos == -1) return cnt==k&&!lead;
if(cnt > k) return 0;
if(!f1 && dp[pos][cnt][lead] != -1) return dp[pos][cnt][lead];
int up = f1 ? a[pos] : 15,res = 0;
for(int i = 0;i <= up;i++)
res = (res+dfs(pos-1,(!i&&lead) ? sta : sta|(1<<i),f1&&i==up,lead&&!i))%mod;
return !f1 ? dp[pos][cnt][lead] = res : res;
}
signed main()
{
memset(dp,-1,sizeof(dp));
cin>>s>>k; int cnt = 0;
for(auto it:s) a[cnt++] = isdigit(it) ? it-'0' : it-'A'+10;
reverse(a,a+cnt);
printf("%lld\n",dfs(cnt-1,0,1,1));
return 0;
}