P4999 烦人的数学作业

//URL:https://www.luogu.com.cn/problem/P4999
/*
给出一个区间LL~RR,求LL到RR区间内每个数的数字和,如123这个数的数字和为1+2+3=6。
(1≤L≤R≤1021≤L≤R≤102)
同学们纷纷做出来了,Mr.G一看这最后一题跟摆设没区别了呀,于是他迅速修改了题目,把范围定得非常非常大,且有TT组数据,将最终的答案mod 109+7109+7。
(1≤L≤R≤10181≤L≤R≤1018) (1≤T≤201≤T≤20)

每个数字出现次数*i

*/
/*
2
24 69
70 120

411
498
*/
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<string.h>
#include<queue>
#include<vector> 
#include<bits/stdc++.h>
typedef long long ll;
#define ddd printf("-----------------------\n");
using namespace std;
const int maxn=1e1 +10;
const int mod=1e9+7;
const int inf=0x3f3f3f3f;

ll dp[20][20],dim[20];
ll dfs(int pos,int sum,int limit,int zero,int dig)// limit 1/0 zero 1/0
{
    if(pos==0) return sum%mod;
    if(limit==0&&zero==0&&dp[pos][sum]!=-1) return dp[pos][sum];
    int end=limit? dim[pos]:9;
    ll res=0;
    for(int i=0;i<=end;i++)
    {
        res=(res+dfs(pos-1,sum+((zero==0||i)&&(i==dig)),limit&&i==end,zero&&i==0,dig)+mod)%mod;                          
    }
    if(limit==0&&zero==0) dp[pos][sum]=res%mod;
    return res%mod;
}
//数位dp的操作
ll  work(long long x,int dig)
{
    memset(dp,-1,sizeof(dp));//初始化
    dim[0]=0;
    while(x)
    {
        dim[++dim[0]]=x%10;//最高位在第一位 
        x/=10;
    }
    return    dfs(dim[0],0,1,1,dig)%mod;
}
int main()
{
    ios::sync_with_stdio(false);
    int T;cin>>T;
    while(T--)
    {
        ll a,b; cin>>a>>b;
        ll ans=0;
        for(int i=1;i<=9;i++)//九个digit
        {
             ans=(ans+(work(b,i)-work(a-1,i))*i+mod)%mod;
        }
        cout<<(ans+mod)%mod<<'\n';
    
    }
   
    return 0;
}

 

posted @ 2024-02-08 18:35  JMXZ  阅读(5)  评论(0)    收藏  举报