首页 > 代码库 > [HDOJ3652]B-Number(数位dp)

[HDOJ3652]B-Number(数位dp)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3652

题意:找区间内的数,数内含有连续的13并且这个数能被13整除。

dfs(int l, bool one, bool three, int sum, bool flag, bool ok),分别记录位数,上一位1是否出现,当前位3是否出现,这个数%13的值,是否到达边界以及这条dfs链上的结果是否已经有13了。状态设计得有点傻B了。

 1 #include <bits/stdc++.h> 2 using namespace std; 3 #define fr first 4 #define sc second 5 #define cl clear 6 #define BUG puts("here!!!") 7 #define W(a) while(a--) 8 #define pb(a) push_back(a) 9 #define Rint(a) scanf("%d", &a)10 #define Rll(a) scanf("%I64d", &a)11 #define Rs(a) scanf("%s", a)12 #define Cin(a) cin >> a13 #define FRead() freopen("in", "r", stdin)14 #define FWrite() freopen("out", "w", stdout)15 #define Rep(i, len) for(int i = 0; i < (len); i++)16 #define For(i, a, len) for(int i = (a); i < (len); i++)17 #define Cls(a) memset((a), 0, sizeof(a))18 #define Clr(a, x) memset((a), (x), sizeof(a))19 #define Full(a) memset((a), 0x7f7f7f, sizeof(a))20 #define lrt rt << 121 #define rrt rt << 1 | 122 #define pi 3.1415926535923 #define RT return24 #define lowbit(x) x & (-x)25 #define onecnt(x) __builtin_popcount(x)26 typedef long long LL;27 typedef long double LD;28 typedef unsigned long long ULL;29 typedef pair<int, int> pii;30 typedef pair<string, int> psi;31 typedef pair<LL, LL> pll;32 typedef map<string, int> msi;33 typedef vector<int> vi;34 typedef vector<LL> vl;35 typedef vector<vl> vvl;36 typedef vector<bool> vb;37 38 const int maxn = 15;39 int digit[maxn];40 LL dp[maxn][2][2][15][2];41 LL n;42 43 LL dfs(int l, bool one, bool three, int sum, bool flag, bool ok) {44   if(l == 0) {45     if(sum != 0) return 0;46     if(ok) return 1;47     return 0;48   }49   if(!flag && ~dp[l][one][three][sum][ok]) return dp[l][one][three][sum][ok];50   LL ret = 0;51   int pos = flag ? digit[l] : 9;52   Rep(i, pos+1) {53     if(one && i == 3) ret += dfs(l-1, one, true, (sum*10+i)%13, flag&&(i==pos), true);54     else if(i == 1) ret += dfs(l-1, true, false, (sum*10+i)%13, flag&&(i==pos), ok);55     else ret += dfs(l-1, false, false, (sum*10+i)%13, flag&&(i==pos), ok);56   }57   if(!flag) dp[l][one][three][sum][ok] = ret;58   return ret;59 }60 61 LL f(LL x) {62   int pos = 0;63   while(x) {64     digit[++pos] = x % 10;65     x /= 10;66   }67   return dfs(pos, false, false, 0, true, false);68 }69 70 signed main() {71   //FRead();72   Clr(dp, -1);73   while(cin >> n) {74     cout << f(n) << endl;75   }76   RT 0;77 }

 

[HDOJ3652]B-Number(数位dp)