数位DP HDU3555
Bomb
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 15025 Accepted Submission(s): 5427
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?
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.
1
50
500
1
15
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.
/*
本题与HDU2089相似,把那道题的代码改了一下找出了不含49的,然后用总数减去,简单粗暴,当然有更好的方法。
*/
#include<iostream>
#include<string>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<vector>
#include<iomanip>
#include<queue>
#include<stack>
using namespace std;
int t;
long long dp[][],n;
void init()
{
for(int i=;i<;i++)
{
for(int j=;j<;j++)
{
for(int k=;k<;k++)
{
if(j==&&k==) continue;
dp[i][j]+=dp[i-][k];
}
}
}
}
long long insum(long long n)
{
long long sum=;
int lne=,c[]={};
while(n)
{
c[lne++]=n%;
n/=;
}
for(int i=lne-;i>;i--)
{
for(int j=;j<c[i];j++) //j不能等于c[i],因为dp[i][j]中的j代表第i位取j时的总数,而j后面的数
//要全部遍历一遍,这里j后面并非取全部数而是取到给出的数的后几位
{
if(j==&&c[i+]==) continue;
sum+=dp[i][j];
}
if(c[i]==&&c[i+]==) break;
}
return sum;
}
int main()
{
scanf("%d",&t);
while(t--)
{
memset(dp,,sizeof(dp));
dp[][]=;
cin>>n;
n+=;
init();
long long a=insum(n);
cout<<n-a<<endl;
}
return ;
} /*
别人的一个更好的方法。很难想到。
*/
#include<iostream>
#include<string>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<vector>
#include<iomanip>
#include<queue>
#include<stack>
using namespace std;
int t;
long long dp[][],n; //dp[i][0]表示到第i位没有49的个数,dp[i][1]表示到第i位没有49但第i位是9的个数,
//dp[i]][2]表示到第i位包含49的个数。
void init()
{
dp[][]=; //初始化
dp[][]=;
dp[][]=;
for(int i=;i<;i++)
{
dp[i][]=*dp[i-][]-dp[i-][]; //到第i位没有49的个数等于第i位依次取0~9连上后面没有49的,
//然后去掉第i位取4,i-1位为9的情况。
dp[i][]=dp[i-][]; //第i位是9时
dp[i][]=*dp[i-][]+dp[i-][]; //到第i位包含49的个数等于第i位依次取0~9连上后面包含49的,
//再加上第i位取4时,i-1位是9的情况。
}
}
long long insum(long long n)
{
long long sum=;
int c[]={};
int cnt=;
while(n)
{
c[cnt++]=n%;
n/=;
}
bool flag=false;
for(int i=cnt-;i>;i--) //从高位到低位依次枚举
{
sum+=dp[i-][]*c[i]; //到第i-1位包含49,就加上0~c[i]个
if(flag) sum+=dp[i-][]*c[i];
else
{
if(c[i]>) sum+=dp[i-][]; //如果第i位大于4了,并且i-1是9,则一定包含49.
}
if(c[i+]==&&c[i]==) flag=true; //当从高位出现49之后后面dp[i][0]就无意义了,全加上
}
return sum;
}
int main()
{
scanf("%d",&t);
{
while(t--)
{
scanf("%lld",&n);
init();
printf("%lld\n",insum(n+)); //计算结果不包含n本身
}
}
return ;
}
//记忆化搜索法
#include<iostream>
#include<string>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<vector>
#include<iomanip>
#include<queue>
#include<stack>
using namespace std;
long long n;
int t;
long long dp[][];
int c[];
long long dfs(int lne,int have,int lim)
{
if(lne<=)
return have==;
if(!lim&&dp[lne][have]!=-)
return dp[lne][have];
long long ans=;
int nnum=lim?c[lne]:;
for(int i=;i<=nnum;i++)
{
int nhave=have;
if(have==&&i==) nhave=;
if(have==&&i!=&&i!=) nhave=;
if(have==&&i==) nhave=;
ans+=dfs(lne-,nhave,lim&&i==nnum);
}
if(!lim)
dp[lne][have]=ans;
return ans;
}
int main()
{
scanf("%d",&t);
while(t--)
{
scanf("%lld",&n);
memset(dp,-,sizeof(dp));
int cnt=;
while(n)
{
c[++cnt]=n%;
n/=;
}
c[cnt+]=;
printf("%lld\n",dfs(cnt,,));
}
return ;
}
数位DP HDU3555的更多相关文章
- [暑假集训--数位dp]hdu3555 Bomb
The counter-terrorists found a time bomb in the dust. But this time the terrorists improve on the ti ...
- 数位dp浅谈(hdu3555)
数位dp简介: 数位dp常用于求区间内某些特殊(常关于数字各个数位上的值)数字(比如要求数字含62,49): 常用解法: 数位dp常用记忆化搜索或递推来实现: 由于记忆化搜索比较好写再加上博主比较蒟, ...
- hdu3555 数位dp
Bomb Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others) Total Subm ...
- hdu3555 Bomb (记忆化搜索 数位DP)
http://acm.hdu.edu.cn/showproblem.php?pid=3555 Bomb Time Limit: 2000/1000 MS (Java/Others) Memory ...
- hdu---(3555)Bomb(数位dp(入门))
Bomb Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)Total Submi ...
- hdu3555 Bomb 数位DP入门
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3555 简单的数位DP入门题目 思路和hdu2089基本一样 直接贴代码了,代码里有详细的注释 代码: ...
- 【Hdu3555】 Bomb(数位DP)
Description 题意就是找0到N有多少个数中含有49. \(1\leq N \leq2^{63}-1\) Solution 数位DP,与hdu3652类似 \(F[i][state]\)表示位 ...
- 【hdu3555】Bomb 数位dp
题目描述 求 1~N 内包含数位串 “49” 的数的个数. 输入 The first line of input consists of an integer T (1 <= T <= 1 ...
- [Hdu3555] Bomb(数位DP)
Description 题意就是找0到N有多少个数中含有49. \(1\leq N \leq2^{63}-1\) Solution 数位DP,与hdu3652类似 \(F[i][state]\)表示位 ...
随机推荐
- MapKit 添加大头针
#import "ViewController.h" #import <MapKit/MapKit.h> #import "MYAnnotation.h&qu ...
- .NET C# Tostring() format 格式化字符串大全
C 货币 2.5.ToString("C") ¥2.50 D 十进制数 25.ToString("D5") 00025 E 科学型 25000.ToString ...
- Jmeter测试环境搭建(一)
一.工具描述 Apache JMeter是 100%的java桌面应用程序.它可以被用来测试包括基于静态和动态资源程序的性能,例如静态文件,Java Servlets,Java 对象,数据库,F ...
- 编译报错dereferencing pointer to incomplete type
关于编译报错“dereferencing pointer to incomplete type... 多是没找到结构体的定义,可以在本地复制其定义试试. 参考: http://my.oschina.n ...
- JQuery学习之其他
1.noConflict()方法:释放会$标识符的控制,这样其他也用$的脚本就可以使用它了 **全名代替简写的方法使用jQuery $.noConflict(); jQuery(document).r ...
- POJ 3274 HASH
题目链接:http://poj.org/problem?id=3274 题意+思路: 点击这里 补充:因为有减法运算,所以可能会造成运算后结果为负数,所以需要把结果统一转换成正数[不然数组下标访问不到 ...
- mongodb学习03 操作详解
插入文档 db.test.insert({"name":"jinks"}); 批量插入 db.test.insert([{}, {}, {}]); 一次批量插入 ...
- [工作中的设计模式]原型模式prototype
一.模式解析 提起prototype,最近看多了js相关的内容,第一印象首先是js的原型 var Person=function(name){ this.name=name; } Person.pro ...
- maven 各种用途
1.maven 管理项目编译 作为项目编译代码管理工具,可以方便的进行编译集成. 2. maven 扩展单元测试 扩展对接junit可以方便进行单元测试 3.maven profiles各种devel ...
- storm安装笔记以及提交拓扑任务
Storm -- Distributed and fault-tolerant realtime computation 这是一个分布式的.容错的实时计算系统 把Storm依赖组件的版本贴出来供各位参 ...