要求 $[1, N]$ 中至少有一位重复的数字的数的个数,只要有一位数字重复不好处理,可以反过来求,先求出范围内没有重复数字的数的个数,然后用总数减去没有重复数字的数的个数即可。
没有重复数字的数的个数可以用数位 DP 求解,设 $dp(i, mask)$ 表示前 $i$ 位数字中,已经选取的数字的集合为 $mask$ 的数的个数,$mask$ 的第 $j$ 位为 $1$ 表示已经选取了数字 $j$,为 $0$ 表示没有选取。那么对于第 $i+1$ 位,只能选取不在 $mask$ 中的数位,状态转移是:$dp[i, mask] += dp[i + 1, mask | 2^j]$。另外一种情况是如果当前还没有选取过任何数字,那么第 $i$ 位可以跳过,状态转移是:$dp[i, mask] += dp[i + 1, mask]$
记忆化 $dp$ 数组的时候,可以只记忆当前选取的数字没有达到 $n$ 的前缀的上界并且已经选取了数字的情况。因为如果前缀已经到了数字 $n$ 的上界,那么当前搜索分支只会进行一次,不会第二次进入这个分支,因此不用记忆。
当前选取的数字前缀是否达到 $n$ 的上界用一个参数 $bound$ 表示,当前是否曾经选取了数字用一个参数 $number$ 表示。
// Date: Thu Mar 7 19:47:21 2024
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> VI;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
template <class T> using pq = priority_queue<T>;
template <class T> using pqg = priority_queue<T, vector<T>, greater<T>>;
const int INF = 0x3f3f3f3f, MOD = 1e9 + 7, MOD1 = 998'244'353;
const ll INFL = 0x3f3f3f3f'3f3f3f3f;
const double eps = 1e-8;
const int dir[8][2] = {
{0, 1}, {0, -1}, {1, 0}, {-1, 0}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1},
};
const ull Pr = 131;
#define For(i, a, b) for (int i = int(a); i < int(b); ++i)
#define Rof(i, a, b) for (int i = int(b) - 1; i >= int(a); --i)
#define For1(i, a, b) for (int i = int(a); i <= int(b); ++i)
#define Rof1(i, a, b) for (int i = int(b); i >= int(a); --i)
#define ForE(i, j) for (int i = h[j]; i != -1; i = ne[i])
#define f1 first
#define f2 second
#define pb push_back
#define has(a, x) (a.find(x) != a.end())
#define nemp(a) (!a.empty())
#define all(a) (a).begin(), (a).end()
#define SZ(a) int((a).size())
#define NL cout << '\n';
template <class T> bool ckmin(T &a, const T &b) { return b < a ? a = b, 1 : 0; }
template <class T> bool ckmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; }
template <typename t> istream &operator>>(istream &in, vector<t> &vec) {
for (t &x : vec)
in >> x;
return in;
}
template <typename t> ostream &operator<<(ostream &out, vector<t> &vec) {
int n = SZ(vec);
For(i, 0, n) {
out << vec[i];
if (i < n - 1)
out << ' ';
}
return out;
}
void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char *x) { cerr << '\"' << x << '\"'; }
void __print(const string &x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }
template <typename T, typename V> void __print(const pair<T, V> &x) {
cerr << '{';
__print(x.first);
cerr << ", ";
__print(x.second);
cerr << '}';
}
template <typename T> void __print(const T &x) {
int f = 0;
cerr << '{';
for (auto &i : x)
cerr << (f++ ? ", " : ""), __print(i);
cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, typename... V> void _print(T t, V... v) {
__print(t);
if (sizeof...(v))
cerr << ", ";
_print(v...);
}
#ifdef _DEBUG
#define debug1(x) cout << #x " = " << x << endl;
#define debug2(x, y) cout << #x " = " << x << " " #y " = " << y << endl;
#define debug3(x, y, z) \
cout << #x " = " << x << " " #y " = " << y << " " #z " = " << z << endl;
#define dbg(x...) \
cerr << "\e[91m" << __func__ << ":" << __LINE__ << " [" << #x << "] = ["; \
_print(x); \
cerr << "\e[39m" << endl;
#else
#define debug1
#define debug2
#define debug3
#define dbg(x...)
#endif
// For LeetCode
#define LN ListNode
#define LNP ListNode *
#define TN TreeNode
#define TNP TreeNode *
#ifdef _DEBUG
struct ListNode {
int val;
ListNode *next;
ListNode() : val(0), next(nullptr) {}
ListNode(int val) : val(val), next(nullptr) {}
ListNode(int val, ListNode *next) : val(val), next(next) {}
};
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode() : val(0), left(nullptr), right(nullptr) {}
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
TreeNode(int x, TreeNode *left, TreeNode *right)
: val(x), left(left), right(right) {}
};
#endif
// End of LeetCode
int dp[15][1 << 10];
class Solution {
public:
int len;
string s;
int dfs(int pos, int mask, bool bound, bool number) {
if (pos == len)
return number;
if (!bound && number && dp[pos][mask] != -1)
return dp[pos][mask];
int low{}, up = 9, ans{};
if (!number) {
ans += dfs(pos + 1, mask, false, number);
low = 1;
}
if (bound)
up = s[pos] - '0';
For1(i, low, up) {
if ((mask >> i) & 1)
continue;
ans += dfs(pos + 1, mask | (1 << i), bound && i == up, true);
}
if (!bound && number)
dp[pos][mask] = ans;
return ans;
}
int numDupDigitsAtMostN(int n) {
s = to_string(n);
len = SZ(s);
memset(dp, -1, sizeof dp);
return n - dfs(0, 0, true, false);
}
};
#ifdef _DEBUG
int main(void) {
std::ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
Solution a;
dbg(a.numDupDigitsAtMostN(20));
dbg(a.numDupDigitsAtMostN(100));
dbg(a.numDupDigitsAtMostN(1000));
return 0;
}
#endif