AtCoder abc194_f Digits Paradise in Hexadecimal(数位dp + 状压)

题目地址

题意:

给你一个16进制数n,要求你 [ 1 , n ] [1,n] [1,n] 中出现k个不同digits的数。

思路:

  1. 数位dp
  2. 题目的n很大,所以要先压缩状态。

学习

这里介绍(统计一个数在二进制下有多少个1)

__builtin_popcount()

学习地址

AC

#include <iostream>
#include <bits/stdc++.h>
#define For(i,x,y) for(int i = (x);  i <= (y); i ++ )
#define fori(i,x,y) for(int i = (x); i < (y); i ++ )
#define mp make_pair
#define fi first
#define se second
#define pb push_back
#define sz(a) (int)a.size()
#define endl '\n'
#define all(x) a.begin(), a.end()
#define mst(x,a) memset(x, a, sizeof x)
#define debug(a) cout<< #a <<": " << a << endl
using namespace std;

typedef long long LL;
typedef pair<int,int>pa;

const int inf = 0x3f3f3f3f;
const int INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9+7;
const int maxn = 2e5+10;
const int maxm = 1e6+10;
const int N = 1e6+10;

int dp[maxn][20][2][2];
int a[maxn], n, k;
int dfs(int x, int sta, bool lead = true, bool limit = true){
    int cnt = __builtin_popcount(sta);
    int& ans  = dp[x][cnt][lead][limit];
    if(~ans) return ans;
    if(cnt > k) return ans = 0;
    if(x==n) return cnt==k;
    int up = limit?a[x]:15;
    ans = 0;
    For(i,0,up){
        int add = dfs(x+1, (lead&&!i) ? sta : sta|1<<i, !i&&lead, limit && i == up);
        ans = (LL)ans + add;
        if(ans >= mod) ans -= mod;
    }
    return ans;
}
int main()
{
    ios::sync_with_stdio(0);
#ifdef LOCAL
    freopen("in.txt", "r", stdin);
#endif
    string s;
    cin>>s>>k;
    mst(dp,-1);
    n = sz(s);
    fori(i,0,n)a[i] = isdigit(s[i])?s[i]-'0':s[i]-'A'+10;
    printf("%d\n", dfs(0,0));
    return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值