Meaning of the questions: Given two positive integers \ (l \) and \ (r \) , seeking in \ ([l, r] \ ) of all integers, each digital each appeared many times. \ (( 1 <= l <= r << 10 ^ {12}) \)
The meaning of problems: a given interval \ ([l, r] \ ) , find \ ([l, r] \ ) of each of the numbers and the number and (p \ (1e9 + 7 \) modulo) As. \ (123 \) number and this number is \ (1 + 2 + 3 = 6.1 <= l <= r <= 10 ^ {18}. \)
These two questions things just is not the same output, the process is exactly the same.
Analysis: for the ten digits 0 to 9, which were specifically how to digital DP request, sets of templates on the line.
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<map>
#include<set>
#define ll long long
using namespace std;
inline ll read(){
ll x=0,o=1;char ch=getchar();
while(ch!='-'&&(ch<'0'||ch>'9'))ch=getchar();
if(ch=='-')o=-1,ch=getchar();
while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
return x*o;
}
int len,a[20];ll dp[20][20][20];
inline ll dfs(int pos,int pre,int sum,int lead,int limit,int goal){
//pos当前考虑到哪一位
//pre上一位填的是什么
//sum当前共出现了几次goal这个数
//lead判断是否有前导零
//limit判断是否有填数限制
//goal当前DP要统计的是哪个数出现的次数
if(pos>len)return sum;//找到了一个合法的数,返回答案
if(dp[pos][pre][sum]!=-1&&!limit&&!lead)return dp[pos][pre][sum];//记忆化搜索
ll cnt=0;int res=limit?a[len-pos+1]:9;
for(int i=0;i<=res;++i){
if((!i)&&lead)cnt+=dfs(pos+1,0,0,1,limit&&(i==res),goal);
else if(i&&lead)cnt+=dfs(pos+1,i,(i==goal),0,limit&&(i==res),goal);
else cnt+=dfs(pos+1,i,sum+(i==goal),0,limit&&(i==res),goal);
}
return (!limit&&!lead)?dp[pos][pre][sum]=cnt:cnt;
}
inline ll part(ll x,int num){
len=0;while(x)a[++len]=x%10,x/=10;
memset(dp,-1,sizeof(dp));
return dfs(1,0,0,1,1,num);
}
int main(){
ll l=read(),r=read();
for(int i=0;i<=9;++i)
printf("%lld ",part(r,i)-part(l-1,i));
printf("\n");
return 0;
}
Put the number of times each number appears and then multiply this number add up to answer.
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<map>
#include<set>
#define ll long long
using namespace std;
inline ll read(){
ll x=0,o=1;char ch=getchar();
while(ch!='-'&&(ch<'0'||ch>'9'))ch=getchar();
if(ch=='-')o=-1,ch=getchar();
while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
return x*o;
}
const int mod=1e9+7;
int len,a[20];ll dp[20][20][20];
inline ll dfs(int pos,int pre,int sum,int lead,int limit,int goal){
if(pos>len)return sum%mod;
if(dp[pos][pre][sum]!=-1&&!limit&&!lead)return dp[pos][pre][sum];
ll cnt=0;int res=limit?a[len-pos+1]:9;
for(int i=0;i<=res;++i){
if((!i)&&lead)cnt+=dfs(pos+1,0,0,1,limit&&(i==res),goal);
else if(i&&lead)cnt+=dfs(pos+1,i,(i==goal),0,limit&&(i==res),goal);
else cnt+=dfs(pos+1,i,sum+(i==goal),0,limit&&(i==res),goal);
}
return (!limit&&!lead)?dp[pos][pre][sum]=cnt:cnt;
}
inline ll part(ll x,int num){
len=0;while(x)a[++len]=x%10,x/=10;
memset(dp,-1,sizeof(dp));
return dfs(1,0,0,1,1,num)%mod;
}
int main(){
int T=read();
while(T--){
ll l=read(),r=read(),ans=0;
for(int i=1;i<=9;++i)ans=((ans+i*(part(r,i)-part(l-1,i)))%mod+mod)%mod;//0没必要考虑
printf("%lld\n",ans);
}
return 0;
}