一、题目
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;
}