标签:
求出1-n中包含13并且能被13整除的数的个数
开一个四维数组dp[i][j][k][l], i表示第i位, j表示这个数mod13, k表示是否包含13, l表示前一位是什么。
1 #include<bits/stdc++.h> 2 using namespace std; 3 #define pb(x) push_back(x) 4 #define ll long long 5 #define mk(x, y) make_pair(x, y) 6 #define lson l, m, rt<<1 7 #define mem(a) memset(a, 0, sizeof(a)) 8 #define rson m+1, r, rt<<1|1 9 #define mem1(a) memset(a, -1, sizeof(a)) 10 #define mem2(a) memset(a, 0x3f, sizeof(a)) 11 #define rep(i, a, n) for(int i = a; i<n; i++) 12 #define ull unsigned long long 13 typedef pair<int, int> pll; 14 const double PI = acos(-1.0); 15 const double eps = 1e-8; 16 const int mod = 1e9+7; 17 const int inf = 1061109567; 18 const int dir[][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} }; 19 int dp[20][30][2][10], digit[20]; 20 int dfs(int len, int num, int pre, bool ok, bool fp) { 21 if(!len) { 22 return ok&&num==0; 23 } 24 if(!fp&&dp[len][num][ok][pre]!=-1) 25 return dp[len][num][ok][pre]; 26 int maxx = fp?digit[len]:9, ret = 0; 27 for(int i = 0; i<=maxx; i++) { 28 ret += dfs(len-1, (num*10+i)%13, i, ok||(pre==1&&i==3), fp&&i==maxx); 29 } 30 if(!fp) 31 return dp[len][num][ok][pre] = ret; 32 return ret; 33 } 34 int cal(int n) { 35 int len = 0; 36 while(n) { 37 digit[++len] = n%10; 38 n/=10; 39 } 40 return dfs(len, 0, 0, 0, 1); 41 } 42 int main() 43 { 44 int a; 45 mem1(dp); 46 while(scanf("%d", &a)!=EOF) { 47 printf("%d\n", cal(a)); 48 } 49 }
标签:
原文地址:http://www.cnblogs.com/yohaha/p/5036392.html