站外题求调
  • 板块学术版
  • 楼主Genshin_Morax
  • 当前回复3
  • 已保存回复3
  • 发布时间2025/1/24 22:46
  • 上次更新2025/1/25 11:07:32
查看原帖
站外题求调
749194
Genshin_Morax楼主2025/1/24 22:46

个人思路数位DP,50分,样例都没过,求调

//下面注释的cout是调试代码
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod=1e9+7;
const int N=1000;
string l,r;
int a[N+5];
int len;
ll dp[N+5][11][11];
ll dfs(int step,bool flag,int last,int pre,bool zero,int cnt)//step表示第几位,flag表示取值有没有限制,last是上上个数,pre是上一个数,zero是有(1)无(0)前导0
{
    if(step==0)
    {
        return zero?0ll:1ll;
    }
    if(!flag&&!zero&&dp[step][last][pre]!=-1) return dp[step][last][pre];
    ll ret=0;
    int maxx=flag?a[step]:9;
    for(int i=0;i<=maxx;i++)
    {
        ll res=ret;
        int QWQ=(step==2),QAQ;
        if(step==2&&pre==0&&i==0)
        {
            //cout<<1<<"\n";
        }
        if((i==last&&cnt>=2)||(i==pre&&!zero)) continue;//如果出现回文
        if(zero&&i==0) ret=(ret+dfs(step-1,flag&&(i==maxx),pre,0,1,0))%mod,QAQ=QWQ*1;//如果有前导0
        else ret=(ret+dfs(step-1,flag&&(i==maxx),pre,i,0,cnt+1))%mod,QAQ=QWQ*2;
        if(step==1&&pre==0&&last==0)
        {
            //cout<<i<<" "<<ret-res<<"\n";
        }
    }
    if(!flag&&!zero) dp[step][last][pre]=ret;
    return ret;
}
ll solve(string str)
{
    len=str.size();
    for(int i=1;i<=1001;i++) a[i]=10;
    ll res=0;
    for(int i=0;i<len;i++)//存数
    {
        a[len-i]=str[i]-'0';
        res=(res*10+a[len-i])%mod;
    }
    memset(dp,-1,sizeof(dp));//cout<<"\n";
    ll k=dfs(len,1,10,10,1,0);
    //cout<<k<<"\n\n";
    return (res-k+mod)%mod;
}
void QAQ()//l减1
{
    int sz=l.size()-1;
    int id=sz;
    while(l[id]=='0') id--;
    l[id++]--;
    while(id<sz) l[id++]='9';
}
int main()
{
    cin>>l>>r;
    QAQ();
    cout<<(solve(r)-solve(l)+mod)%mod<<"\n";
    return 0;
}
2025/1/24 22:46
加载中...