zoukankan      html  css  js  c++  java
  • hdu 3555 Bomb 【数位DP】

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

    题意:上一题是不要62 这个是“不要49”

    代码:

    #include <stdio.h>
    #include <ctime>
    #include <math.h>
    #include <limits.h>
    #include <complex>
    #include <string>
    #include <functional>
    #include <iterator>
    #include <algorithm>
    #include <vector>
    #include <stack>
    #include <queue>
    #include <set>
    #include <map>
    #include <list>
    #include <bitset>
    #include <sstream>
    #include <iomanip>
    #include <fstream>
    #include <iostream>
    #include <ctime>
    #include <cmath>
    #include <cstring>
    #include <cstdio>
    #include <time.h>
    #include <ctype.h>
    #include <string.h>
    #include <assert.h>
    
    using namespace std;
    
    #define N 50
    
    using namespace std;
    int bit[N];
    long long dp[N][3];
    
    /*
          dp[i][0]:前i位不含 49 的个数。
    
        dp[i][1]:前i位不含 49 数且i+1位是4的个数。
    
        dp[i][2]:前i位含 49 的个数。

    */ long long dfs(int pos, int st, bool flag) { if (pos == 0) return st == 2; if (flag && dp[pos][st] != -1) return dp[pos][st]; long long ans = 0; int u = flag ?

    9 : bit[pos]; for (int d = 0;d <= u;d++) { if (st == 2 || (st == 1 && d == 9)) ans += dfs(pos - 1, 2, flag || d<u); else if (d == 4) ans += dfs(pos - 1, 1, flag || d<u); else ans += dfs(pos - 1, 0, flag || d<u); } if (flag) dp[pos][st] = ans; return ans; } long long solve(long long n) { int len = 0; while (n) { bit[++len] = n % 10; n /= 10; } return dfs(len, 0, 0); } int main() { long long n; int t; scanf("%d",&t); while (t--) { scanf("%lld", &n); memset(dp, -1, sizeof(dp)); printf("%lld ", solve(n)); } return 0; }

  • 相关阅读:
    SGU438_The Glorious Karlutka River =)
    SGU326_Perspective
    Problem B. Harvest of Apples(莫队+数学)
    【HDU2019多校】1005
    【HDU2019多校】K
    L
    「2017 山东一轮集训 Day2」Pair (霍尔定理+线段树)
    【2017西安】Sum of xor sum(线段树)
    【2017西安】 XOR (线性基+思维)
    【SPOJ】Lightning Conductor (dp+决策单调性)
  • 原文地址:https://www.cnblogs.com/cxchanpin/p/7123555.html
Copyright © 2011-2022 走看看