Luogu_P4999 annoying job search digital mathematical mind DP

Luogu_P4999 annoying math homework

Digital DP ###

Title link
obvious digital DP entitled
\ (f [i] [j ] \) referred to as the first \ (I \) bit, you sum \ (J \) number and.
Memory search.


code show as below:

#include<bits/stdc++.h>
#define ll long long
#define int ll
using namespace std;
const ll mod=1e9+7;
int t;
ll a[20],f[20][200];
ll dfs(ll pos,ll sum,bool b){
    if(pos==0)  return sum%mod;
    if(!b && f[pos][sum]!=-1)  return f[pos][sum]%mod;
    ll maxx= b ? a[pos] : 9;ll ans=0;
    for(int i=0;i<=maxx;i++)
        ans=(ans+dfs(pos-1,sum+i,b && a[pos]==i))%mod;
    if(!b) f[pos][sum]=ans%mod;
    return ans%mod;
}
inline ll sol(ll x){
    ll tot=0;
    while(x>0){
        a[++tot]=x%10;x/=10;
    }
    return dfs(tot,0,1)%mod;
}
signed main()
{
    scanf("%d",&t);
    memset(f,-1,sizeof(f));
    while(t--){
        ll l,r;scanf("%lld%lld",&l,&r);
        printf("%lld\n",(sol(r)-sol(l-1)+mod)%mod);
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/ChrisKKK/p/11620109.html