传送门:2020牛客多校第六场 - H Harmony Pairs
思路分析
$dp[pos][sum][lima][limb]$ 表示搜索到第$pos$位,差为$sum$,$a$,$b$有无限制的方案数
$sum$初始值1000防止负数溢出
AC代码
#include <bits/stdc++.h>
#define fi first
#define se second
#define ll long long
#define pb push_back
#define mp make_pair
#define fun function
#define sz(x) (x).size()
#define lowbit(x) (x)&(-x)
#define all(x) (x).begin(),(x).end()
#define mem(a,b) memset(a,b,sizeof(a))
namespace FastIO {
#define BUF_SIZE 100000
#define OUT_SIZE 100000
bool IOerror=0;
inline char nc() {
static char buf[BUF_SIZE],*p1=buf+BUF_SIZE,*pend=buf+BUF_SIZE;
if(p1==pend) {
p1=buf;
pend=buf+fread(buf,1,BUF_SIZE,stdin);
if(pend==p1) {
IOerror=1;
return -1;
}
}
return *p1++;
}
inline bool blank(char ch) {
return ch==' '||ch=='\n'||ch=='\r'||ch=='\t';
}
template<class T> inline bool read(T &x) {
bool sign=0;
char ch=nc();
x=0;
for(; blank(ch); ch=nc());
if(IOerror)return false;
if(ch=='-')sign=1,ch=nc();
for(; ch>='0'&&ch<='9'; ch=nc())x=x*10+ch-'0';
if(sign)x=-x;
return true;
}
template<class T,class... U>bool read(T& h,U&... t) {
return read(h)&&read(t...);
}
#undef OUT_SIZE
#undef BUF_SIZE
};
using namespace std;
using namespace FastIO;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int INF = 0x3f3f3f3f;
const int mod = 1e9+7;
ll dp[1111][2222][2][2];
char s[1111];
ll dfs(int pos,int sum,int lima,int limb) {
if(s[pos]=='\0') {
return (sum>1000);
}
if(dp[pos][sum][lima][limb] !=-1) return dp[pos][sum][lima][limb];
ll ans=0;
int upb=limb?s[pos]-'0':9;
for(int i=0; i<=upb; i++) {
int upa=lima?i:9;
for(int j=0; j<=upa; j++) {
ans=(ans + dfs(pos+1,sum+j-i,lima&&j==i,limb&&i==upb))%mod;
}
}
return dp[pos][sum][lima][limb]=ans;
}
int main() {
#ifdef xiaofan
freopen("1.in","r",stdin);
freopen("1.out","w",stdout);
#endif
mem(dp,-1);
scanf("%s",s+1);
printf("%lld\n",dfs(1,1000,1,1));
return 0;
}