题意:http://acm.hdu.edu.cn/showproblem.php?pid=3652
问 从0到n 多少个数字是13倍数并且含有子串13
记一下当前的余数,是否已经是13倍数和上一位的数字大小
#include<cstdio> #include<cstring> #include<algorithm> #include<iostream> #include<queue> #include<map> #include<vector> #include<math.h> #include<string> using namespace std; #define INF 0x3f3f3f3f #define LL long long #define N 106 #define Lson rood<<1 #define Rson rood<<1|1 LL dp[20][20][14][3],d[20]; LL dfs(int now,int up,int mod,int falg,int fp) {///保留上一个数up 以及对13取余mod 是否含有13的falg if(now==1) return !mod&&falg;///同时成立 if(!fp&&dp[now][up][mod][falg]!=-1) return dp[now][up][mod][falg]; LL ans=0; int ma=fp?d[now-1]:9; for(int i=0;i<=ma;i++) ans+=dfs(now-1,i,(mod*10+i)%13,(up==1&&i==3)||falg,fp&&i==ma); if(!fp&&dp[now][up][mod][falg]) dp[now][up][mod][falg]=ans; return ans; } LL calc(LL x) {///模板分离 LL xxx=x; int len=0; while(xxx) { d[++len]=xxx%10; xxx/=10; } LL sum=0; for(int i=0;i<=d[len];i++) sum+=dfs(len,i,i,0,i==d[len]); return sum; } int main() { LL n; memset(dp,-1,sizeof(dp)); while(scanf("%lld",&n)!=EOF) printf("%lld ",calc(n)); return 0; }