hdu3555 Bomb

链接

与上一个类似,更简单一些,标记前面是否出现过49和4.

 1 #include <iostream>

 2 #include<cstdio>

 3 #include<cstring>

 4 #include<algorithm>

 5 #include<stdlib.h>

 6 #include<vector>

 7 #include<cmath>

 8 #include<queue>

 9 #include<set>

10 using namespace std;

11 #define N 100000

12 #define LL long long

13 #define INF 0xfffffff

14 const double eps = 1e-8;

15 const double pi = acos(-1.0);

16 const double inf = ~0u>>2;

17 LL n,dp[22][2][2][2];

18 int d[22];

19 LL dfs(int i,bool e,bool o,bool p)

20 {

21     if(i==-1)

22     {

23         if(p) return 1;

24         return 0;

25     }

26     if(!e&&dp[i][e][o][p]!=-1) return dp[i][e][o][p];

27     int j;

28     int mk = e?d[i]:9;

29     LL ans = 0;

30     for(j = 0; j <= mk ; j++)

31     {

32         if(p)

33         ans+=dfs(i-1,e&&j==mk,0,1);

34         else

35         {

36             if(o&&j==9)

37             ans+=dfs(i-1,e&&j==mk,0,1);

38             else

39             {

40                 if(j==4)

41                 ans+=dfs(i-1,e&&j==mk,1,0);

42                 else

43                 ans+=dfs(i-1,e&&j==mk,0,0);

44             }

45         }

46     }

47     //cout<<ans<<" "<<i<<endl;

48     return e?ans:dp[i][e][o][p]=ans;

49 }

50 LL cal(LL x)

51 {

52     int g=0;

53     while(x)

54     {

55         d[g++] = x%10;

56         x/=10;

57     }

58     return dfs(g-1,1,0,0);

59 }

60 int main()

61 {

62     int t;

63     cin>>t;

64     memset(dp,-1,sizeof(dp));

65     while(t--)

66     {

67         cin>>n;

68         cout<<cal(n)<<endl;

69     }

70     return 0;

71 }
View Code

 

你可能感兴趣的:(HDU)