http://acm.hdu.edu.cn/showproblem.php?pid=3652
就是个朴素的数位dp啊,但是我一直tle,毫无办法,来日再战吧。
#include<cstdio> #include<cstring> int dp[11][2][2][14]; int num[11]; int remain[11]; int n; int dfs(int i,bool state,bool have,int k,bool limit) { if(i==0){ if(k==0&&have) return 1; else return 0; } if(~dp[i][state][have][k]&&!limit) return dp[i][state][have][k]; int up=limit?num[i]:9; int res=0; for(int j=0;j<=up;j++) { int h=(k+j*remain[i])%13; res+=dfs(i-1,j==1,have||(state&&j==3),h,limit&&j==up); } if(!limit) dp[i][state][have][k]=res; return res; } int solve(int x) { memset(dp,-1,sizeof(dp)); int len=0; while(x) { len++; num[len]=x%10; x/=10; } remain[1]=1; for(int i=2;i<=len;i++) { remain[i]=remain[i-1]*10; } return dfs(len,0,0,0,1); } int main() { while(scanf("%d",&n)) { printf("%d ",solve(n)); } return 0; }