数位dp

http://codeforces.com/gym/101193/problem/A

#include<bits/stdc++.h>
using namespace std;
#define ll long long

const ll mod=1e9+7;

int n[55];
int m[55];
ll dp[55];

ll dfs(int pos,bool lead,bool limit) {
    if(pos==-1) {
        return 1;
    }
    if(!limit && !lead && dp[pos]!=-1)
        return dp[pos];

    int up=limit?n[pos]:9;
    int down=m[pos];
    ll ans=0;
    for(int i=down; i<=up; i++) {
        //cout<<"pos="<<pos<<" i="<<i<<endl;
        ans+=dfs(pos-1,lead && i==0,limit && i==n[pos]);
        ans%=mod;
    }
    if(!limit && !lead){
        dp[pos]=ans;
    }
    return ans;
}

ll solve(string nn,string mm) {
    int ln=nn.length();
    int lm=mm.length();

    if(lm>ln)
        return 0;

    int pos=0;

    for(int i=0;i<ln;i++){
        n[i]=nn[ln-1-i]-'0';

        if(lm-1-i>=0)
            m[i]=mm[lm-1-i]-'0';
        else
            m[i]=0;

        //cout<<"n["<<i<<"]="<<n[i]<<endl;
        //cout<<"m["<<i<<"]="<<m[i]<<endl;
    }

    return dfs(ln-1,true,true);
}

int main() {
    //memset(dp,-1,sizeof(dp));

    string n,m;
    while(cin>>n>>m) {
        memset(dp,-1,sizeof(dp));
        printf("%lld\n",solve(n,m));
    }
}

 

 

.

posted @ 2019-04-07 14:55  韵意  阅读(111)  评论(0编辑  收藏  举报