萌新求助
查看原帖
萌新求助
756179
so_find_skind楼主2023/12/24 17:26

本来我写的是

#include<bits/stdc++.h>
#define mod 1000000007
#define int long long
using namespace std;
int t;
int a[25],l,r,f[25][555];
int dp(int pos,bool limit,int sum){
    if(!pos)
        return sum;
    if(limit){
        int ans=0;
        for(int i=0;i<=a[pos];i++){
            if(i==a[pos])
                ans+=dp(pos-1,true,sum+i)%mod;
            else
                ans+=dp(pos-1,false,sum+i)%mod;
            ans%=mod;
        }
        return ans%mod;
    }
    if(f[pos][sum])
        return f[pos][sum];
    int ans=0;
    for(int i=0;i<=9;i++){
        ans+=dp(pos-1,false,sum+i)%mod;
        ans%=mod;
    }
    return f[pos][sum]=ans;
}
int solve(int x){
    int len=1;
    while(x){
        a[len++]=x%10;
        x/=10;
    }
    len--;
    return dp(len,true,0);
}
signed main(){
    cin>>t;
    while(t--){
        cin>>l>>r;
        cout<<(solve(r)-solve(l-1))%mod<<"\n";
    }
    return 0;
}

然而WA70,可是当我在46行输出中的括号里加了一个+md时,却AC了?!

求问为什么

AC代码(改的地方用-围起来了)

#include<bits/stdc++.h>
#define mod 1000000007
#define int long long
using namespace std;
int t;
int a[25],l,r,f[25][555];
int dp(int pos,bool limit,int sum){
    if(!pos)
        return sum;
    if(limit){
        int ans=0;
        for(int i=0;i<=a[pos];i++){
            if(i==a[pos])
                ans+=dp(pos-1,true,sum+i)%mod;
            else
                ans+=dp(pos-1,false,sum+i)%mod;
            ans%=mod;
        }
        return ans%mod;
    }
    if(f[pos][sum])
        return f[pos][sum];
    int ans=0;
    for(int i=0;i<=9;i++){
        ans+=dp(pos-1,false,sum+i)%mod;
        ans%=mod;
    }
    return f[pos][sum]=ans;
}
int solve(int x){
    int len=1;
    while(x){
        a[len++]=x%10;
        x/=10;
    }
    len--;
    return dp(len,true,0);
}
signed main(){
    cin>>t;
    while(t--){
        cin>>l>>r;
        cout<<(solve(r)-solve(l-1)---+mod---)%mod<<"\n";
    }
    return 0;
}
2023/12/24 17:26
加载中...