数位dp的模板
hdu 2089
设dp[len][flag],flag = 1表示前一位是6,否则前一位不是6.
#include <stdio.h> #include <iostream> #include <map> #include <set> #include <list> #include <stack> #include <vector> #include <math.h> #include <string.h> #include <queue> #include <string> #include <stdlib.h> #include <algorithm> #define LL __int64 #define eps 1e-12 #define PI acos(-1.0) using namespace std; const int INF = 0x3f3f3f3f; const int maxn = 50010; int dig[10]; int dp[10][2]; int dfs(int len, int flag, int up) { if(len == 0) return 1; if(!up && dp[len][flag] != -1) return dp[len][flag]; int n = up == 1?dig[len]:9; int res = 0; for(int i = 0; i <= n; i++) { if(i == 4 || (flag == 1 && i == 2)) continue; res += dfs(len-1,i==6?1:0,up&&(i==n) ); } if(!up) dp[len][flag] = res; return res; } int cal(int num) { int cnt = 0; while(num) { dig[++cnt] = num%10; num /= 10; } memset(dp,-1,sizeof(dp)); return dfs(cnt,0,1); } int main() { int a,b; while(~scanf("%d %d",&a,&b)) { if(a == 0 && b == 0) break; printf("%d\n",cal(b) - cal(a-1) ); } return 0; }
不能出现49,与上题类似
#include <stdio.h> #include <iostream> #include <map> #include <set> #include <list> #include <stack> #include <vector> #include <math.h> #include <string.h> #include <queue> #include <string> #include <stdlib.h> #include <algorithm> #define LL __int64 #define eps 1e-12 #define PI acos(-1.0) using namespace std; const int INF = 0x3f3f3f3f; const int maxn = 4010; int dig[100]; LL dp[100][4]; LL dfs(int len, int flag, int up) { if(len == 0) return flag == 2; if(!up && dp[len][flag] != -1) return dp[len][flag]; int n = up ? dig[len] : 9; LL res = 0; for(int i = 0; i <= n; i++) { int tflag = flag; if(flag == 0 && i == 4) tflag = 1; else if(flag == 1 && i == 9) tflag = 2; else if(flag == 1 && i != 4) tflag = 0; res += dfs(len-1,tflag,up&&(i==n)); } if(!up) dp[len][flag] = res; return res; } LL cal(LL num) { int len = 0; while(num) { dig[++len] = num % 10; num /= 10; } memset(dp,-1,sizeof(dp)); return dfs(len,0,1); } int main() { int test; LL n; scanf("%d",&test); while(test--) { scanf("%I64d",&n); printf("%I64d\n",cal(n)); } return 0; }
要求两个相邻的数之差大于等于2,对于第一位是特殊的。所以dfs的时候加一个变量first表示前面是否全是前导0。
#include <stdio.h> #include <iostream> #include <map> #include <set> #include <list> #include <stack> #include <vector> #include <math.h> #include <string.h> #include <queue> #include <string> #include <stdlib.h> #include <algorithm> #define LL __int64 #define eps 1e-12 #define PI acos(-1.0) using namespace std; const int INF = 0x3f3f3f3f; const int maxn = 4010; int dig[15]; int dp[15][15]; //dp[i][j]表示第i位它的上一位即i+1位为j int dfs(int len, int pre, int up, int first) { if(len == 0) return first == 0; if(!up && dp[len][pre] != -1 && !first) return dp[len][pre]; int n = up ? dig[len] : 9; int res = 0; for(int i = 0; i <= n; i++) { if(first) //当前这一位是第一位,当i==0时,第i-1为也是第一位,否则不是 res += dfs(len-1,i,up&&i==n,first&&i==0); else if(abs(pre-i) >= 2) res += dfs(len-1,i,up&&i==n,first); } if(!up && !first) dp[len][pre] = res; return res; } int cal(int num) { int len = 0; while(num) { dig[++len] = num%10; num /= 10; } memset(dp,-1,sizeof(dp)); return dfs(len,0,1,1); } int main() { int a,b; while(~scanf("%d %d",&a,&b)) { printf("%d\n",cal(b) - cal(a-1)); } return 0; }