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

      1 #include <iostream>
      2 #include <queue>
      3 #include <stack>
      4 #include <cstdio>
      5 #include <vector>
      6 #include <map>
      7 #include <set>
      8 #include <bitset>
      9 #include <algorithm>
     10 #include <cmath>
     11 #include <cstring>
     12 #include <cstdlib>
     13 #include <string>
     14 #include <sstream>
     15 #include <time.h>
     16 #define x first
     17 #define y second
     18 #define pb push_back
     19 #define mp make_pair
     20 #define lson l,m,rt*2
     21 #define rson m+1,r,rt*2+1
     22 #define mt(A,B) memset(A,B,sizeof(A))
     23 using namespace std;
     24 typedef long long LL;
     25 typedef unsigned long long ull;
     26 //const double PI = acos(-1);
     27 const int N=3e3+10;
     28 const LL mod=1e9+7;
     29 const int inf = 0x3f3f3f3f3f;
     30 const LL INF=0x3f3f3f3f3f3f3f3fLL;
     31 const double eps=1e-8;
     32 LL dp[300][20][2][2];
     33 int num[300];
     34 char s[300];
     35 LL dfs(int pos,int pre,int limit,int flag,int zero)
     36 {
     37      LL res=0;
     38      if(pos<0)
     39      {
     40          if(zero)return 0;
     41          else return 1;
     42      }
     43      if(!limit&&dp[pos][pre][flag][zero]!=-1)return dp[pos][pre][flag][zero];
     44 
     45      int top=limit?num[pos]:9;
     46      if(zero)
     47      {
     48          for(int i=0;i<=top;i++)
     49          {
     50              res=(res+dfs(pos-1,i,limit&&(num[pos]==i),0,i==0))%mod;
     51          }
     52      }
     53      else
     54      {
     55          if(flag)
     56          {
     57              for(int i=pre;i<=top;i++)
     58              {
     59                  res=(res+dfs(pos-1,i,limit&&(num[pos]==i),1,0))%mod;
     60              }
     61          }
     62          else
     63          {
     64              for(int i=min(pre,top);i>=0;i--)
     65              {
     66                  res=(res+dfs(pos-1,i,limit&&(num[pos]==i),0,0))%mod;
     67              }
     68              for(int i=pre+1;i<=top;i++)
     69              {
     70                  res=(res+dfs(pos-1,i,limit&&(num[pos]==i),1,0))%mod;
     71              }
     72          }
     73      }
     74 
     75      if(!limit)dp[pos][pre][flag][zero]=res;
     76      return res;
     77 }
     78 void solve()
     79 {
     80      int n;
     81      mt(dp,-1);
     82      scanf("%s",s);
     83      n=strlen(s);
     84      for(int i=n-1,j=0;i>=0;i--,j++)num[j]=s[i]-'0';
     85      printf("%lld
    ",dfs(n-1,0,1,0,1));
     86 }
     87 int main()
     88 {
     89 #ifdef Local
     90     freopen("data.h","r",stdin);
     91     //freopen("out.txt","w",stdout);
     92 #endif
     93     //ios::sync_with_stdio(false);
     94     //cin.tie(0);
     95     //solve();
     96     int T,cas=1;
     97     cin>>T;
     98     while(T--)
     99     {
    100         //printf("Case #%d: ",cas++);
    101         solve();
    102     }
    103 #ifdef Local
    104     cerr << "time: " << (LL) clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
    105 #endif
    106     return 0;
    107 }
    View Code
  • 相关阅读:
    POJ 2689
    NEFU 109
    HDU 2098
    NEFU 2
    NEFU 117
    NEFU 84
    POJ 1061
    NEFU116 GCD
    NEFU 115
    HDU 2099
  • 原文地址:https://www.cnblogs.com/27sx/p/7414994.html
Copyright © 2011-2022 走看看