http://acm.hdu.edu.cn/showproblem.php?pid=3652
如果没有限制,参数列表又一样的话,两个dfs跑出来是一样的,所以直接把参数列表对应的值记下来就行了
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
LL N,dp[15][2][2][13];
int a[15],tol;
LL dfs(int cur,bool is1,bool have13,int mod,int limit)
{
if(!cur)return (!mod&&have13)?1:0;
if(!limit&&dp[cur][is1][have13][mod])return dp[cur][is1][have13][mod];
int up=limit?a[cur]:9;
LL ans=0;
for(int i=0;i<=up;i++)
ans+=dfs(cur-1,i==1,have13||(is1&&i==3),(mod*10+i)%13,limit&&i==a[cur]);
if(!limit)dp[cur][is1][have13][mod]=ans;
return ans;
}
LL solve(LL x)
{
tol=0;
while(x)
{
a[++tol]=x%10;
x/=10;
}
return dfs(tol,0,0,0,1);
}
int main()
{
while(~scanf("%lld",&N))
printf("%lld\n",solve(N));
return 0;
}