(力扣周赛)6151. 统计特殊整数(数位dp)

(力扣周赛)6151. 统计特殊整数(数位dp)题目链接 这里先特别感谢 灵茶山艾府 的 这篇文章 让我重新学习了数位 dp 也可以直接看里面的视频讲解 分析 记忆化搜索返回从当前状态下 能构造出来的特殊整数的数量 其中 i 表示从第 i 位开始填数字 S 表示前面填的数字的集合 S

大家好,我是讯享网,很高兴认识大家。

题目链接

这里先特别感谢 灵茶山艾府 的 这篇文章
让我重新学习了数位dp!
(也可以直接看里面的视频讲解)

分析

记忆化搜索返回从当前状态下 能构造出来的特殊整数的数量;
其中:
i:表示从第i位开始填数字;


讯享网

S:表示前面填的数字的集合S(状态压缩);

代码

#include<iostream> #include<queue> #include<cstring> #include<vector> #include<stdio.h> #include<map> #include<algorithm> #include<deque> #include<stack> #include<set> // #include <unordered_map> #include<math.h> #include<string.h> #define IOS ios::sync_with_stdio(false),cin.tie(0); using namespace std; #define pb push_back #define coutl cout<<"------------"<<endl; #define fi first #define se second #define ire(x) scanf("%d",&x) #define iire(a,b) scanf("%d %d",&a,&b) #define lre(x) scanf("%lld",&x) #define llre(a,b) scanf("%lld %lld",&a,&b) #define rep(i,a,b) for(int i=a;i<=b;i++) #define endl "\n" #define PI acos(-1.0) // #define int long long // #define double long double typedef long long ll; typedef unsigned long long ull; typedef pair<int, int> PII; typedef pair<double, int> PDI; typedef pair<ll, ll> PLL; typedef pair<double, double> PDD; typedef pair<double, pair<int, double> > PDID; typedef pair<char, char> PCC; typedef pair<char, pair<int, int> > PCII; typedef pair<int, pair<int, int> > PIII; typedef pair<int, pair<int, pair<int, int> > > PIIII; typedef pair<ll, pair<int, int> > PLII; const int maxn = 1e6 + 7; const int N = 2e6 + 7; const int M = 4e6 + 7; const int mod = 1e9 + 7; const int inv = mod - mod/2; const int inf = 0x3f3f3f3f; const ll INF = 0x3f3f3f3f3f3f3f3f; const double pi = acos(-1); const double eps = 1e-8; ll gcd(ll a,ll b) { 
   return b==0 ? a : gcd(b,a%b);} ll lcm(ll a,ll b) { 
   return a*b / gcd(a,b);} ll qmi(ll a,ll b,ll p) { 
   ll ans = 1; while(b) { 
    if(b & 1) ans = ans * a % p; a = a * a % p; b >>= 1; } return ans;} int lowbit(int x) { 
   return x & (-x);} string s; int dp[11][1<<10][2][2]; int dfs(int i,int S,bool is_litmit,bool is_num) { 
    if(i == s.size()) return (int)is_num; //出口 if(dp[i][S][is_litmit][is_num] != -1) return dp[i][S][is_litmit][is_num]; int ans = 0; //前面没填数字,可以选择跳过 if(!is_num) ans += dfs(i+1,S,false,false); int up = is_litmit ? (s[i]-'0') : 9; //可以填的数字的上界 int dn = is_num ? 0 : 1; //可以填的数字的下界 for(int num = dn ; num <= up ; num ++) if((S >> num & 1) == 0) //没有填过 ans += dfs(i+1, S | (1 << num), is_litmit && num == up, true); dp[i][S][is_litmit][is_num] = ans; return ans; } class Solution { 
    public: int countSpecialNumbers(int n) { 
    memset(dp,-1,sizeof dp); s = to_string(n); //由于第0位是要限制的,所以一开始is_litmit为true int ans = dfs(0,0,true,false); return ans; } }; 

讯享网
小讯
上一篇 2025-01-26 13:22
下一篇 2025-01-19 12:07

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/108414.html