-
[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);
}
}