数位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;
}

相关推荐

  1. 415. 字符串相加

    2024-01-27 05:40:01       34 阅读
  2. leetcode415:字符串相加

    2024-01-27 05:40:01       26 阅读
  3. 【算法详解】力扣415.字符串相加

    2024-01-27 05:40:01       74 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-27 05:40:01       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-27 05:40:01       101 阅读
  3. 在Django里面运行非项目文件

    2024-01-27 05:40:01       82 阅读
  4. Python语言-面向对象

    2024-01-27 05:40:01       91 阅读

热门阅读

  1. 达梦表空间不足解决办法

    2024-01-27 05:40:01       60 阅读
  2. 代码随想录day24

    2024-01-27 05:40:01       37 阅读
  3. 前端学习-0125

    2024-01-27 05:40:01       35 阅读
  4. 前端小白入门试题——附答案

    2024-01-27 05:40:01       47 阅读
  5. 初识子域名

    2024-01-27 05:40:01       72 阅读
  6. 前端各种优化

    2024-01-27 05:40:01       49 阅读
  7. 聊聊PowerJob的ProcessorTracker

    2024-01-27 05:40:01       45 阅读
  8. 索引统计信息是什么

    2024-01-27 05:40:01       57 阅读
  9. Git提交规范

    2024-01-27 05:40:01       58 阅读
  10. SpringBoot和Vue接口调用传参方式

    2024-01-27 05:40:01       50 阅读
  11. vue中数据状态轮询

    2024-01-27 05:40:01       55 阅读