标签:
数位dp
http://acm.hdu.edu.cn/showproblem.php?pid=3652
据说已经是模板题了 求包含13并且能被13整除的数的个数
1 #pragma comment(linker, "/STACK:102400000,102400000") 2 3 #include <cstdio> 4 #include <cstdlib> 5 #include <cstring> 6 #include <cmath> 7 #include <ctime> 8 #include <iostream> 9 #include <algorithm> 10 #include <queue> 11 #include <map> 12 #include <set> 13 #include <vector> 14 #include <list> 15 #include <stack> 16 #include <string> 17 18 using namespace std; 19 20 #define PI acos(-1.0) 21 #define INF 0x3f3f3f3f 22 #define inf 0x3f 23 #define rst(a, b) memset(a, b, sizeof(a)) 24 #define mod 1000000007 25 #define eps 1e-6 26 #define ok puts("ok"); 27 28 typedef long long ll; 29 typedef unsigned long long ull; 30 typedef pair<int, int> pii; 31 32 const int maxn = 200005; 33 const int maxm = 10005; 34 35 int dp[12][13][3]; 36 int arr[11]; 37 38 int dfs(int pos, int mode, int have, int lim) { 39 if(pos <= 0) return mode == 0 && have == 2; 40 if(!lim && dp[pos][mode][have] != -1) return dp[pos][mode][have]; 41 int num = lim == 1 ? arr[pos] : 9; 42 int ans = 0; 43 for(int i = 0; i <= num; i++) { 44 int mod_x = (mode * 10 + i) % 13; 45 int have_x = have; 46 if(have == 0 && i == 1) have_x = 1; 47 if(have == 1 && i != 1) have_x = 0; 48 if(have == 1 && i == 3) have_x = 2; 49 ans += dfs(pos-1, mod_x, have_x, lim && i == arr[pos]); 50 } 51 if(!lim) dp[pos][mode][have] = ans; 52 return ans; 53 } 54 55 int cal(int num) { 56 rst(dp, -1); 57 int len = 0; 58 while(num) { 59 arr[++len] = num % 10; 60 num /= 10; 61 } 62 return dfs(len, 0, 0, 1); 63 } 64 65 void solve() { 66 int n; 67 while(scanf("%d", &n) != EOF) { 68 printf("%d\n", cal(n)); 69 } 70 } 71 72 int main() { 73 solve(); 74 return 0; 75 }
标签:
原文地址:http://www.cnblogs.com/mitrenick/p/4822564.html