Bomb HDU - 3555 (数位DP)

The counter-terrorists found a time bomb in the dust. But this time the terrorists improve on the time bomb. The number sequence of the time bomb counts from 1 to N. If the current number sequence includes the sub-sequence "49", the power of the blast would add one point. 
Now the counter-terrorist knows the number N. They want to know the final points of the power. Can you help them? 

InputThe first line of input consists of an integer T (1 <= T <= 10000), indicating the number of test cases. For each test case, there will be an integer N (1 <= N <= 2^63-1) as the description.

The input terminates by end of file marker. 
OutputFor each test case, output an integer indicating the final points of the power.Sample Input

  1. 3
  2. 1
  3. 50
  4. 500

Sample Output

  1. 0
  2. 1
  3. 15

Hint

  1. From 1 to 500, the numbers that include the sub-sequence "49" are "49","149","249","349","449","490","491","492","493","494","495","496","497","498","499",
  2. so the answer is 15.
  3.  
  4. 题意:求1-n之间有几个数带有49
    题解:一题数位DP,可以用记忆化搜索解决,感觉板子还是有点懂了,但不知道改了之后会不会了,具体的dfs的每一步的具体操作已经放在代码里了
  1. #include<cstdio>
  2. #include<iostream>
  3. #include<algorithm>
  4. #include<cstring>
  5. #include<sstream>
  6. #include<cmath>
  7. #include<stack>
  8. #include<cstdlib>
  9. #include <vector>
  10. #include<queue>
  11. using namespace std;
  12.  
  13. using namespace std;
  14. #define ms(a,b) memset(a,b,sizeof(a))
  15. #define lson rt*2,l,(l+r)/2
  16. #define rson rt*2+1,(l+r)/2+1,r
  17. typedef unsigned long long ull;
  18. typedef long long ll;
  19. const int MAXN=1e4+5;
  20. const double EPS=1e-8;
  21. const int INF=0x3f3f3f3f;
  22. const int MOD = 1e9+7;
  23.  
  24. #define ll long long
  25. #define llu unsigned long long
  26. #define INF 0x3f3f3f3f
  27. #define PI acos(-1.0)
  28. const int maxn = 1e5+5;
  29. const int mod = 1e9+7;
  30.  
  31. int bit[40];
  32. ll f[40][4];
  33. ll dp(int pos,int st,bool flag)
  34. {
  35. //pos为位,st为状态,st=0:没有49,st=1:前一位为4,st=2,表示49都已经出现过了
  36. //flag表示高位与原数是否相同
  37. if(pos == 0)
  38. return st == 2;
  39. if(flag && f[pos][st] != -1)
  40. return f[pos][st];
  41. ll ans = 0;
  42. int x;
  43. if(flag)
  44. x = 9;
  45. else
  46. x = bit[pos];
  47. //cout<<x<<endl;
  48. for(int i = 0;i <= x; i++)
  49. {
  50. if((st == 2) || (st == 1 && i == 9)) //如果上一位已经有49(st==2)或者上一位为4(st==1)当前位为9
  51. ans += dp(pos-1,2,flag || i<x); //只能加上上一个状态,且上一个状态一定为已经有49了
  52. else if(i == 4)
  53. ans += dp(pos-1,1,flag || i<x); //当前为4和前一位也为4的状态是一样的
  54. else //维持st=0的状态
  55. ans += dp(pos-1,0,flag || i<x);
  56. }
  57. if(flag)
  58. f[pos][st] = ans; //记忆化
  59. return ans;
  60. }
  61.  
  62. ll calc(ll x)
  63. {
  64. int len = 0;
  65. while(x)
  66. {
  67. bit[++len] = x % 10;
  68. x /= 10;
  69. }
  70. //cout<<len<<endl;
  71. return dp(len,0,0);
  72. }
  73. int main()
  74. {
  75. int t;
  76. scanf("%d",&t);
  77. memset(f,-1,sizeof f);
  78. while(t--)
  79. {
  80. ll n;
  81. scanf("%lld",&n);
  82. printf("%lld\n",calc(n));
  83. }
  84. }

Bomb HDU - 3555 (数位DP)的更多相关文章

  1. Bomb HDU - 3555 数位dp

    Code: #include<cstdio> #include<algorithm> #include<cstring> #include<string> ...

  2. HDU 3555 数位dp

    Bomb Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)Total Submi ...

  3. HDU 3555 数位dp入门

    开始想用dp[i][j]来记录第i位j开头含有49的数的个数 但是init后并不知道如何进行cal 想了想可以用不要62的思想 当作不要49来做 然后减一下 就好 看网上的代码 不要62和这道题用的d ...

  4. hdu 3555数位dp基础入门题

    #include<stdio.h> #define N 20 long long  dp[N][3]; void init(){ long long  i; dp[0][0]=1; for ...

  5. Bomb HDU - 3555

    Bomb HDU - 3555 求1~n中含有49数的个数 #include<bits/stdc++.h> #define LL long long using namespace std ...

  6. hdu 4507 数位dp(求和,求平方和)

    http://acm.hdu.edu.cn/showproblem.php?pid=4507 Problem Description 单身! 依旧单身! 吉哥依旧单身! DS级码农吉哥依旧单身! 所以 ...

  7. hdu 4352 数位dp + 状态压缩

    XHXJ's LIS Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  8. Bomb HDU 3555 dp状态转移

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=3555 题意: 给出一个正整数N,求出1~N中含有数字“49”的数的个数 思路: 采用数位dp的状态转移方程 ...

  9. 2017中国大学生程序设计竞赛 - 网络选拔赛 HDU 6156 数位DP

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6156 题意:如题. 解法:数位DP,暴力枚举进制之后,就转化成了求L,R区间的回文数的个数,这个直接做 ...

随机推荐

  1. Quality of Service (QoS) in LTE

    Background: Why we need QoS ? There are premium subscribers who always want to have better user expe ...

  2. struts2的执行流程

    在浏览器端输入相应的访问地址>>>>把请求发送给tomact>>>>tomact判断应该交给那个webApplication>>>&g ...

  3. xaml实现无边框窗口

    <Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft.com/w ...

  4. Angular CLI的简单使用(1)

    参考地址:  https://v2.angular.cn/docs/ts/latest/cli-quickstart.html Angular CLI是一个命令行界面工具,它可以创建项目.添加文件以及 ...

  5. 【迷你微信】基于MINA、Hibernate、Spring、Protobuf的即时聊天系统:9.观察者模式

    欢迎阅读我的开源项目<迷你微信>服务器与<迷你微信>客户端 前言 在一个程序的迭代过程中,复杂度渐渐上升,可能会出现一些跨模块的调用的需求,若是直接得到引用来进行使用,会导致模 ...

  6. HDU 3351 Seinfeld 宋飞正传(水)

    题意: 给出一个串,串内只有大括号,问经过几次改变可使全部括号合法?改变指的是可以将某一方向的括号变成另一方向. 思路: 利用栈的特点,若出现成对的合法括号,直接删掉,留下那些不合法的成为一串.既然不 ...

  7. 【强力卸载】使用Uninstall Tool卸载各类不易卸载的软件

    Uninstall Tool 经测试卸载MySql5.7.18成功. 下载地址: http://files.cnblogs.com/files/xiaohi/%E3%80%90%E8%BD%AF%E4 ...

  8. 使用SAP云平台的destination消费Internet上的OData service

    通过SAP云平台上的destination我们可以消费Internet上的OData service或者其他通过HTTP方式暴露出来的服务. 创建一个新的destination: 维护如下属性: 点击 ...

  9. 百度地图API 基础入门

    一.注册账号,获取密钥 流程-注册-登录-控制台-创建应用-获取密钥: 1.你想要调取百度地图,首先,你需要注册一个百度账号,获取密匙. 2.密钥获取以后,引入到你需要调用百度地图的界面中. 二.创建 ...

  10. IOS类似9.png

    图形用户界面中的图形有两种实现方式,一种是用代码画出来,比如Quartz 2D技术,狠一点有OpenGL ES,另一种则是使用图片. 代码画的方式比较耗费程序员脑力,CPU或GPU; 图片则耗费磁盘空 ...