Loj10164 数字游戏1

本文介绍一种使用数位动态规划(数位DP)的方法来解决特定区间内不降数的数量问题。通过记忆化搜索的方式,从高位到低位枚举每一位数字,实现对不降数的有效计数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目描述

科协里最近很流行数字游戏。某人命名了一种不降数,这种数字必须满足从左到右各位数字成小于等于的关系,如 123446。现在大家决定玩一个游戏,指定一个整数闭区间 [a,b][a,b][a,b],问这个区间内有多少个不降数。


数位DP的模板,记忆化搜索时枚举从当前状态开始就行。具体看注释

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <queue>
#include <stack>
#include <vector>
using namespace std;
#define MAXN 100010
#define INF 10000009
#define MOD 10000007
#define LL long long    
#define in(a) a=read()
#define REP(i,k,n) for(int i=k;i<=n;i++)
#define DREP(i,k,n) for(int i=k;i>=n;i--)
#define cl(a) memset(a,0,sizeof(a))
inline int read(){
    int x=0,f=1;char ch=getchar();
    for(;!isdigit(ch);ch=getchar()) if(ch=='-') f=-1;
    for(;isdigit(ch);ch=getchar()) x=x*10+ch-'0';
    return x*f;
}
inline void out(int x){
    if(x<0) putchar('-'),x=-x;
    if(x>9) out(x/10);
    putchar(x%10+'0');
}
int n,f[12][12],digit[12];
LL DFS(int pos,int u,int flag){//pos代表当前位,u代表当前状态,flag判断是否越界
     if(pos==0)  return 1;//如果枚举完了,这是一种情况
     LL ans=0;
     int end;
     if(flag)  end=digit[pos];//如果当前枚举的数越界,也就是擦边,那么枚举到最大
     else  end=9;//如果当前不擦边,那么久随便枚举
     REP(i,u,end)  ans+=DFS(pos-1,i,flag && i==end);
     return ans;
 }
LL get(int k){
    int l=0;
    while(k){
        digit[++l]=k%10;
        k/=10;
    }
    DFS(l,0,1);
}
int main(){
    int a,b; 
    while(cin>>a>>b)  cout<<get(b)-get(a-1)<<endl;
    return 0;    
} 

 

转载于:https://www.cnblogs.com/jason2003/p/9852585.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值