我的程序样例3TLE,但是这题能AC,下面是我的代码:
#include<bits/stdc++.h>
using namespace std;
#define int long long
int x,sa,b;
const int lim=5e5;
string s;
int dp[15][500005][2][2],a[500005],pos=0;
int _dp(int ps,int mod,bool ld,bool lmt){
// cout<<ps<<' '<<mod<<' '<<ld<<' '<<lmt<<' '<<s<<endl;
if(!ps)return mod==0;
if(dp[ps][mod][ld][lmt])return dp[ps][mod][ld][lmt];
int limit=lmt?a[ps]:9;
string t=s;
bool a=false;
for(auto k:s)if(k=='0')a=true;
if(ld&&!a)t='0'+t;
int ans=0;
// cout<<ld<<' '<<a<<' '<<t<<endl;
for(auto u:t){
int v=u-'0';
if(v>limit)break;
if(v)ans+=_dp(ps-1,(mod*10+v)%x,false,lmt&&v==limit);
else ans+=_dp(ps-1,(mod*10+v)%x,ld,lmt&&v==limit);
}
return dp[ps][mod][ld][lmt]=ans;
}
bool ok(int x){
while(x){
bool flag=false;
for(auto y:s){
int z=y-'0';
if(x%10==z)flag=true;
}
if(!flag)return false;
x/=10;
}return true;
}
int solve1(int x){//[0,x]
if(!x)return 1;
pos=0;
// memset(dp,0,sizeof dp);
int _x=x;
while(_x){
a[++pos]=_x%10;
_x/=10;
}
return _dp(pos,0,true,true);
}
signed main(){
cin>>x>>sa>>b>>s;
if(x<lim)cout<<solve1(b)-solve1(sa-1)<<endl;
else{
int ans=0;
for(int i=0;i<=b+x;i+=x)if(sa<=i&&i<=b&&ok(i))ans++;
cout<<ans<<endl;
}
}
增加的点为这些:
1 1 100000000000
0123456789
5 1 100000000000
12346789
2 1 100000000000
13579