类比2019南昌区域赛c题,发现也是个二维数位dp,我们发现差值不可能超过+-1000,并且位数一共100位,因此可以列出状态方程
这题还需要考虑a<b,因此多枚举一位表示当前是否已经小于b
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<bits/stdc++.h> using namespace std; typedef long long ll; const int mod=1e9+7; string s; ll f[105][2005][2][2][2]; string t; int len; ll dfs(int u,int sum,int sign1,int sign2,int flag){ if(u==len){ return sum>1000; } auto &x=f[u][sum][sign1][sign2][flag]; if(x!=-1) return x; int up1=9; int up2=9; if(sign1){ up1=t[u]-'0'; } if(sign2){ up2=t[u]-'0'; } int i,j; ll res=0; for(i=0;i<=up1;i++){ for(j=0;j<=up2;j++){ if(!flag&&i>j) continue; res+=dfs(u+1,sum+(i-j),sign1&&(i==up1),sign2&&(j==up2),flag||i<j); res%=mod; } } return x=res; } ll solve(string s){ t=s; len=t.size(); memset(f,-1,sizeof f); return dfs(0,1000,1,1,0); } int main(){ ios::sync_with_stdio(false); cin>>s; cout<<solve(s)<<endl; }