type
Post
status
Published
date
Jan 16, 2024
slug
数位DP模板
summary
tags
动态规划
数位DP
password
Property
Jan 16, 2024 04:05 AM
category
算法竞赛
icon
数位DP 模板
题目来源:
#include <bits/stdc++.h> using namespace std; typedef pair<int, int> PII; const int inf = 2e9; typedef long long ll; class Solution { public: int countSpecialNumbers(int n) { string s = to_string(n); int m = s.size(); vector<vector<int>>dp(m,vector<int>(1<<10,-1)); function<int(int, int, bool, bool)> f = [&](int i, int mask, bool is_num, bool is_limit) -> int { if (i == m) { return is_num; } if (!is_limit && is_num && dp[i][mask] != -1) { return dp[i][mask]; } int res = 0; if (!is_num) { res = f(i + 1, mask, false, false); } int up = is_limit ? s[i] - '0' : 9; for (int d = 1 - is_num; d <= up; d++) { if ((mask & (1 << d)) == 0) res += f(i + 1, mask | (1 << d), true, is_limit && d == up); } if (!is_limit && is_num) { dp[i][mask] = res; } return res; }; return f(0, 0, false, true); } };
模板来自于灵茶山艾府 - 力扣(LeetCode)
- Author:crystal
- URL:https://blog-crystal520.vercel.app/article/%E6%95%B0%E4%BD%8DDP%E6%A8%A1%E6%9D%BF
- Copyright:All articles in this blog, except for special statements, adopt BY-NC-SA agreement. Please indicate the source!