1731: 【有渔8月月赛】辅助性T细胞【数位DP】

1731: 【有渔8月月赛】辅助性T细胞
分析:
dp[pos][x]:pos1x d p [ p o s ] [ x ] : 前 p o s 位 1 的 数 量 为 x 的 个 数 ,然后就记忆化瞎搞就行了(*套着数位dp板子扣扣)。
离线打表也是可行的,这是一种神奇又实用的技巧。注意一点,范围是1~n不是0~n 。

#include <bits/stdc++.h>
#include <ext/rope>
using namespace std;
using namespace __gnu_cxx;
#define met(s) memset(s, 0, sizeof(s))
#define rep(i, a, b) for(int i = a; i <= b; ++i)
template <class T> inline void scan_d(T &ret) {
char c; ret = 0;
while ((c = getchar()) < '0' || c > '9');
while (c >= '0' && c <= '9') {
ret = ret * 10 + (c - '0'), c = getchar();}}
typedef long long LL;
typedef unsigned long long ull;
typedef pair<int, int> pii;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const int MAXN = 1e5 + 10;
int a[15], dp[15][15];
int n, k;

inline int dfs(int pos, int x, int limit) {
    if(x == 0 && pos < 0) return 1;
    if(x < 0 || pos < 0) return 0;
    if(dp[pos][x] >= 0 && !limit) return dp[pos][x];
    int up = limit ? a[pos] : 9;
    int ans = 0;
    for(int i = 0; i <= up; ++i) {
        if(i == 1) ans += dfs(pos - 1, x - 1, limit && i == up);
        else ans += dfs(pos - 1, x, limit && i == up);
    }
//  printf("$$ %d %d %d\n", pos, x, ans);
    if(!limit) dp[pos][x] = ans;
    return ans;
}

inline void so(int x) {
    int p = 0;
    while(x) {
        a[p++] = x % 10;
        x /= 10;
    }
    int cnt = dfs(p - 1, k, 1);
    printf("%d\n", cnt - (k ? 0 : 1));
}

int main() {
    memset(dp, -1, sizeof(dp));
    scanf("%d %d", &n, &k);
    so(n);
    return 0;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值