NBUT 1619 - 递增数字(数位DP)

  • [1619] 递增数字

  • 时间限制: 1000 ms 内存限制: 65535 K
  • 问题描述
  • 我们把数字的高位到低位逐渐变大的数字叫作递增数字,形如0,12344,35799。现在给出区间L和R,请你计算L到R内递增数字的个数。因为个数比较多,将答案输出mod (1e9+7)后输出。
  • 输入
  • 多组输入,每组输入两个数,分别代表L和R。
    数据范围:0<=L<=R<=10^100。 
  • 输出
  • 每组输出一行,表示答案个数mod 1e9+7后的值。 
  • 样例输入
  • 13 24
    93 111
    12345 54321
  • 样例输出
  • 10
    2
    952
  • 提示
  • 来源
  • 2015苏州大学ACM-ICPC集训队选拔赛(1)


注意是大数。

数位DP,模板里改一点就行了。


#include <iostream>
#include <stdio.h>
#include <math.h>
#include <algorithm>
#include <string.h>
using namespace std;
typedef long long  LL;
const LL maxn = 500+44;
const LL inf = 0x3f3f3f3f;
const LL mod = 1e9+7;
LL num[111];
LL dp[111][12];
LL dfs(LL pos,LL pre,LL sta,bool limit)
{
	if(pos==0) return 1;
	if(!limit&&dp[pos][sta]!=-1) return dp[pos][sta];
	LL up=limit?num[pos]:9;
	LL temp=0;
	for(LL i=0;i<=up;i++){
		if(pre==-1){
			(temp+=dfs(pos-1, i, i+1,limit&&(i==num[pos])))%=mod;
		}else{
			if(i<pre){
				continue;
			}
			(temp+=dfs(pos-1,i,i+1,limit&&(i==num[pos])))%=mod;
		}
	}
	if(!limit) dp[pos][sta]=temp;
	return temp%mod;
}
LL doit(char x[])
{
	LL cnt=0;
	LL l=strlen(x);
	for(LL i=l-1;i>=0;i--){
		num[++cnt]=x[i]-'0';
	}
	return dfs(cnt,-1,0,true);
}
LL haha(char s[])
{
	LL l=strlen(s);
	for(LL i=1;i<l;i++){
		if(s[i]<s[i-1]){
			return 0;
		}
	}
	return 1;
}
int main()
{
	memset(dp,-1,sizeof dp);
	char s1[111],s2[111];
	while(~scanf("%s %s",s1,s2)){
		LL k=haha(s1);
		printf("%lld\n",(mod+doit(s2)-doit(s1)+k)%mod);
	}

}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值