zoukankan      html  css  js  c++  java
  • HDU 6148 Valley Numer (数位DP)

    题意:。。。

    析:好久没写数位DP了,几乎就是不会了。。。。

    dp[i][last][s] 表示前 i 位上一位是 last,当前的状态是 s,0表示非上升,1 表示非下降,然后就很简单了,只有 0 能转成 1,1就是最后的状态。

    代码如下:

    #pragma comment(linker, "/STACK:1024000000,1024000000")
    #include <cstdio>
    #include <string>
    #include <cstdlib>
    #include <cmath>
    #include <iostream>
    #include <cstring>
    #include <set>
    #include <queue>
    #include <algorithm>
    #include <vector>
    #include <map>
    #include <cctype>
    #include <cmath>
    #include <stack>
    #include <sstream>
    #include <bitset>
    #define debug() puts("++++");
    #define gcd(a, b) __gcd(a, b)
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    #define fi first
    #define se second
    #define pb push_back
    #define sqr(x) ((x)*(x))
    #define ms(a,b) memset(a, b, sizeof a)
    #define freopenr freopen("in.txt", "r", stdin)
    #define freopenw freopen("out.txt", "w", stdout)
    using namespace std;
    
    typedef long long LL;
    typedef unsigned long long ULL;
    typedef pair<int, int> P;
    const int INF = 0x3f3f3f3f;
    const LL LNF = 1e16;
    const double inf = 0x3f3f3f3f3f3f;
    const double PI = acos(-1.0);
    const double eps = 1e-8;
    const int maxn = 1000 + 10;
    const LL mod = 1000000007;
    const int dr[] = {-1, 0, 1, 0};
    const int dc[] = {0, 1, 0, -1};
    const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
    int n, m;
    const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    inline bool is_in(int r, int c){
      return r > 0 && r <= n && c > 0 && c <= m;
    }
    
    LL dp[maxn][11][2];
    char s[maxn];
    int a[maxn];
    
    LL dfs(int pos, int last, int s, bool is, bool ok){
      if(!pos)  return !is;
      LL &ans = dp[pos][last][s];
      if(!is && !ok && ans >= 0)  return ans;
    
      int n = ok ? a[pos] : 9;
      LL res = 0;
      if(last == 10){
        res += dfs(pos-1, 10, s, 1, ok && 0 == n);
        for(int i = 1; i <= n; ++i)
          res += dfs(pos-1, i, 0, 0, i == n && ok);
      }
      else if(s){
        for(int i = last; i <= n; ++i)
          res += dfs(pos-1, i, s, i == 0 && is, ok && i == n);
      }
      else{
        for(int i = 0; i <= n; ++i)
          if(i > last)  res += dfs(pos-1, i, 1, is && i == 0, ok && i == n);
          else  res += dfs(pos-1, i, 0, is && i == 0, ok && i == n);
      }
      res %= mod;
      if(!is && !ok)  ans = res;
      return res;
    }
    
    LL solve(char *s){
      int len = 0;
      n = strlen(s);
      for(int i = n-1; i >= 0; --i)
        a[++len] = s[i] - '0';
      return dfs(len, 10, 0, 1, 1);
    }
    
    int main(){
      ms(dp, -1);
      int T;  cin >> T;
      while(T--){
        scanf("%s", s);
        printf("%I64d
    ", solve(s));
      }
      return 0;
    }
    

      

  • 相关阅读:
    14.Java基础_函数/函数重载/参数传递
    98. 验证二叉搜索树(深搜)
    13.Java基础_数组内存图
    12Java基础_数组定义格式/动态初始化/静态初始化
    计算几何基础
    11.Java基础_IDEA常用快捷键
    Add Two Numbers
    Two Sum
    登录界面id属性的使用
    系统查看
  • 原文地址:https://www.cnblogs.com/dwtfukgv/p/7391528.html
Copyright © 2011-2022 走看看