Bomb
Description
Now the counter-terrorist knows the number N. They want to know the final points of the power. Can you help them?
Input
The input terminates by end of file marker.
Output
Sample Input
3
1
50
500
Sample Output
0
1
15
Hint
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", so the answer is 15.
题目大意:
给一个数字n,范围在1~2^63-1,求1~n之间含有49的数字有多少个。
经典的数位dp,参考了大腿的代码:http://www.cnblogs.com/luyi0619/archive/2011/04/29/2033117.html
状态转移:
dp[i][0]代表长度为 i 并且不含有49的数字的个数;
dp[i][1]代表长度为 i 并且不含有49,但是最高位是9的数字的个数;
dp[i][2]代表长度为 i 并且含有49的数字的个数。
数组 a[i] 从低位到高位存储 n 的每一位数字。
详解参考:http://www.cnblogs.com/liuxueyang/archive/2013/04/14/3020032.html
代码如下:
#include<iostream>
#include <string.h>
#include <stdio.h>
using namespace std;
typedef long long ll;
ll dp[25][3]; void init()
{
dp[0][0] = 1; //表示位数为i的无49的个数; 等于1是为首位为9,位数为1的数服务;
dp[0][1] = 0; //表示位数为i的无49的个数但最高位为9的个数;
dp[0][2] = 0; //表示位数为i的含有49的个数;
for(int i = 1; i < 25; i++)
{
dp[i][0] = dp[i - 1][0] * 10 - dp[i - 1][1]; //减去最高位为9的情况;
dp[i][1] = dp[i - 1][0]; //最高位加9的个数
dp[i][2] = dp[i - 1][2] * 10 + dp[i - 1][1]; //加上没含49但最高位是9的个数(加上4就成49了);
}
}
ll solve(ll n)
{
ll ans = 0;
bool vis = false;
int bit[25],tot = 0;
while(n)
{
bit[++tot] = n % 10;
n /= 10;
}
bit[tot + 1] = 0;
for(int i = tot; i >= 1; i--)
{
ans += dp[i - 1][2] * bit[i];
if(vis) ans += bit[i] * dp[i - 1][0]; // 这一步和下一步要细心体会其中的高深之处,看了kuangbin大神的代码好久才明白这个道理;
else if(bit[i] > 4) ans += dp[i - 1][1]; //如果n本身已经含有49了就不用考虑是否最高位为9了,直接加上没49的个数,因为n含了49
if(bit[i + 1] == 4 && bit[i] == 9) vis = true;
}
if(vis) ans++; //如果n本身也包含49,则要加1;
return ans;
} int main()
{
ll n;
int t;
init();
scanf("%d", &t);
while(t--)
{
scanf("%lld",&n);
printf("%lld\n",solve(n));
}
return 0;
}
Bomb的更多相关文章
- HDU3555 Bomb[数位DP]
Bomb Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)Total Submi ...
- Leetcode: Bomb Enemy
Given a 2D grid, each cell is either a wall 'W', an enemy 'E' or empty '0' (the number zero), return ...
- HDU 5934 Bomb(炸弹)
p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...
- hdu 3622 Bomb Game(二分+2-SAT)
Bomb Game Time Limit: 10000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- CF 363B One Bomb(枚举)
题目链接: 传送门 One Bomb time limit per test:1 second memory limit per test:256 megabytes Description ...
- hdu3555 Bomb (记忆化搜索 数位DP)
http://acm.hdu.edu.cn/showproblem.php?pid=3555 Bomb Time Limit: 2000/1000 MS (Java/Others) Memory ...
- [HDU3555]Bomb
[HDU3555]Bomb 试题描述 The counter-terrorists found a time bomb in the dust. But this time the terrorist ...
- hdu 5934 Bomb
Bomb Problem Description There are N bombs needing exploding.Each bomb has three attributes: explodi ...
- HDOJ 3555 Bomb
数位DP的DFS写法.... Bomb Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Oth ...
随机推荐
- mobx源码解读4
这节介绍一下mobx的变动因子的稳定性. mobx整个系统是由ObservableValue, ComputedValue, Reaction这三个东西构建的 ObservableValue 是最小的 ...
- 使用C#的反射机制读取类的字段名称及值
using System.Windows.Forms;using System.Reflection; foreach (FieldInfo fi in typeof(SystemInformatio ...
- 论Segmentation fault
刚开始学c的时候,最头疼的事情是编译总是通不过,郁闷的要死,只要编译通过了,就兴奋的要死.现在,最头疼的事情是什么呢,编译没问题,但是程序跑的时候会出现Segmentation fault! 这个东西 ...
- CCS3.3下执行优化
最近想研究一下CCS下的程序优化,之前也了解一些,现在查阅一下资料,整理一下. 当然优化有很多种,我本次先说的是执行优化方式. 首先打开我们自己的工程.工程文件若显示为黄色的,则表示此文件默认优化选项 ...
- Servlet学习五——流的分发
在上一节中有提到,流的传输,可以考虑Stream,但如果需要同时分发流和其它信息,,就需要再考虑其它方式了. 在coding中,服务端查询结果都是以gson进行传输,当需要传输一个语音并且同时需要传输 ...
- 写css时要注意数字的浮动方向
写css时要注意数字的浮动方向 当数字位数增加时他的方向才是正确的 text-align:right;padding-right:29px;
- windows 下 gvim打开默认全屏显示
在_vimrc文件中加入如下配置即可: autocmd GUIEnter * simalt ~x
- java环境基础步骤 jdk tomcat eclipse
1.下载jdk,安装jdk 2.设置环境变量 1)打开我的电脑--属性--高级--环境变量 2)系统变量→新建 JAVA_HOME 变量 变量值填写jdk的安装目录(本人是 D:\java\jdk1. ...
- 转载 --ios 模型-setValuesForKeysWithDictionary
应用场景:app请求后端数据,返回的数据是JSON形式,如: { "is_favor" = 0; "is_follow" = 0; "is_prais ...
- Activity之间传递参数(二)
------siwuxie095 传递数据包 1.传递数据包要用到Bundle,MainActivity.java中: package com.siwuxie095.sendargs; import ...