BZOJ 5064: B-number
数位DP
#include<cstdio>
#include<cstring>
using namespace std;
int A[16];
long long F[16][13][10][2];
long long dfs(int t,int Y,int Last,int lim13,int lim){
if (!lim && F[t][Y][Last][lim13]!=-1) return F[t][Y][Last][lim13];
if (t==0){
if (!Y && lim13) return 1;
return 0;
}
int Lim=9;
if (lim) Lim=A[t];
long long ANS=0;
for (int up=0; up<=Lim; up++) ANS+=dfs(t-1,(Y*10+up)%13,up,lim13|(Last==1 && up==3),lim&(up==Lim));
if (!lim) F[t][Y][Last][lim13]=ANS;
return ANS;
}
long long solve(long long x){
int Len=0;
while (x){
A[++Len]=x%10;
x/=10;
}
return dfs(Len,0,0,0,1);
}
int main(){
long long n;
scanf("%lld",&n);
memset(F,-1,sizeof(F));
printf("%lld\n",solve(n));
return 0;
}

浙公网安备 33010602011771号