数位dp,HDU 4151 The Special Number

一、题目

1、题目描述

In this problem, we assume the positive integer with the following properties are called ‘the special number’:
1) The special number is a non-negative integer without any leading zero.
2) The numbers in every digit of the special number is unique ,in decimal system.
Of course,it is easy to check whether one integer is a ‘special number’ or not, for instances, 1532 is the ‘special number’ and 101 is not. However, we just want to know the quantity of the special numbers that is less than N in this problem.

2、接口描述

​2.1输入

The input will consist of a series of signed integers which are not bigger than 10,000,000. one integer per line. (You may assume that there are no more than 20000 test cases)

Sample Input

 
 

10 12

2.2输出

For each case, output the quantity of the special numbers that is less than N in a single line.

Sample Output

 
 

9 10

3、原题链接

Problem - 4151 (hdu.edu.cn)


二、解题报告

1、思路分析

数位dp板子题,只要能正确设计状态即可。

special number要求没前导零,各位不同,所以我们要注意处理前导零

那么怎样设计状态可以处理各位不同呢?

我们用10个比特位即能表示0~9是否出现,所以用一个整数即可代表状态

其它就是跑数位dp板子

2、复杂度

时间复杂度:O(nlogn) 空间复杂度:O(nlogn)

3、代码详解

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <queue>
#include <functional>
using namespace std;
#define IOTIE ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
const int N = 9;
int f[N][1 << 10], d[N], n;
int dfs(int n, int pre, bool lim, bool zero)
{
    if (!n)
        return !zero;
    if (lim && ~f[n][pre] && !zero)
        return f[n][pre];
    int res = 0, ceil = lim ? 9 : d[n];
    for (int i = 0; i <= ceil; i++)
        if (zero && !i)
            res += dfs(n - 1, 0, 1, 1);
        else if ((1 << i) & pre)
            continue;
        else
            res += dfs(n - 1, pre | (1 << i), lim || i < ceil, zero && !i);

    if (lim && !zero)
        return f[n][pre] = res;
    return res;
}
void solve()
{
    memset(f, -1, sizeof(f));
    while (cin >> n)
    {
        int i = 0;
        n--;
        while (n)
            d[++i] = n % 10, n /= 10;
        cout << dfs(i, 0, false, true) << '\n';
    }
}
int main()
{
    IOTIE
    // freopen("in.txt", "r", stdin);
    int _ = 1;
    // cin >> _;
    while (_--)
    {
        solve();
    }
    return 0;
}

  • 16
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

EQUINOX1

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值