标签:pre sum res signed 完整 out name algo sign
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
#define int long long
typedef long long ll;
const int N=200;
int read()
{
int res=0,ch,flag=0;
if((ch=getchar())==‘-‘) //判断正负
flag=1;
else if(ch>=‘0‘&&ch<=‘9‘) //得到完整的数
res=ch-‘0‘;
while((ch=getchar())>=‘0‘&&ch<=‘9‘)
res=res*10+ch-‘0‘;
return flag?-res:res;
}
int mod;
int l,r,a[N],dp[N][N][N];
int dfs(int pos,int sum,int now,int limit)
{
if(!pos)
return (sum==mod)&&(now==0);
if(!limit&&dp[pos][sum][now]!=-1)
return dp[pos][sum][now];
int up=limit?a[pos]:9;
int res=0;
for(int i=0;i<=up;i++)
res+=dfs(pos-1,sum+i,(now*10+i)%mod,limit&&i==up);
if(!limit)
dp[pos][sum][now]=res;
return res;
}
int solve(int x)
{
memset(a,0,sizeof a);
memset(dp,-1,sizeof dp);
int cnt=0;
while(x)
{
a[++cnt]=x%10;
x/=10;
}
int res=0;
//枚举各位数字的和
for(mod=1;mod<=cnt*9;mod++)
{
memset(dp,-1,sizeof dp);
res+=dfs(cnt,0,0,1);
}
return res;
}
signed main()
{
l=read(),r=read();
cout<<solve(r)-solve(l-1)<<endl;
return 0;
}
标签:pre sum res signed 完整 out name algo sign
原文地址:https://www.cnblogs.com/QingyuYYYYY/p/12601059.html