hdu3555 Bomb(数位dp)
题目传送门
Bomb
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 23853 Accepted Submission(s): 8990
Now the counter-terrorist knows the number N. They want to know the final points of the power. Can you help them?
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.
#include<iostream>
#include<string.h>
#include<algorithm>
#include<stdio.h>
#include<queue>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long long ll;
typedef pair<int,int> PII;
#define mod 1000000007
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
//head
ll n;
int bit[];
ll dp[][];
ll dfs(int pos,int pre,int sta,bool limit)
{
if(pos==-)return ;
if(!limit&&dp[pos][sta]!=-)return dp[pos][sta];
int up=limit?bit[pos]:;
ll ans=;
for(int i=;i<=up;i++)
{
if(pre==&&i==)
continue;
ans+=dfs(pos-,i,i==,limit&&i==bit[pos]);
}
if(!limit)dp[pos][sta]=ans;
return ans;
}
ll solve(ll x)
{
int len=;
while(x)
{
bit[len++]=x%;
x/=;
}
return dfs(len-,-,,true);
}
int main()
{
int T;
scanf("%d",&T);
while(T--){
scanf("%lld",&n);
memset(dp,-,sizeof(dp));
printf("%lld\n",n+-solve(n));
}
return ;
}
下面的是直接法做的:
pre=0: 没有49; pre=1: 前一位为4; pre=2: 前几位中有49
#include<iostream>
#include<string.h>
#include<algorithm>
#include<stdio.h>
#include<queue>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long long ll;
typedef pair<int,int> PII;
#define mod 1000000007
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
//head
ll n;
int bit[];
ll dp[][];
ll dfs(int pos,int pre,bool limit)
{
if(pos==-)return pre==;
if(!limit&&dp[pos][pre]!=-)return dp[pos][pre];
int up=limit?bit[pos]:;
ll ans=;
for(int i=;i<=up;i++)
{
if(pre==||pre==&&i==)
ans+=dfs(pos-,,limit&&i==bit[pos]);
else if(i==)
ans+=dfs(pos-,,limit&&i==bit[pos]);
else
ans+=dfs(pos-,,limit&&i==bit[pos]);
}
if(!limit)dp[pos][pre]=ans;
return ans;
}
ll solve(ll x)
{
int len=;
while(x)
{
bit[len++]=x%;
x/=;
}
return dfs(len-,,true);
}
int main()
{
int T;
scanf("%d",&T);
memset(dp,-,sizeof(dp));
while(T--){
scanf("%lld",&n);
printf("%lld\n",solve(n));
}
return ;
}
hdu3555 Bomb(数位dp)的更多相关文章
- 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 Bomb Time Limit: 2000/1000 MS (Java/Others) M ...
- hdu3555 Bomb 数位DP入门
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3555 简单的数位DP入门题目 思路和hdu2089基本一样 直接贴代码了,代码里有详细的注释 代码: ...
- HDU3555 Bomb[数位DP]
Bomb Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)Total Submi ...
- HDU3555 Bomb 数位DP第一题
The counter-terrorists found a time bomb in the dust. But this time the terrorists improve on the ti ...
- hdu3555 Bomb (数位dp入门题)
Bomb Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)Total Submi ...
- 【hdu3555】Bomb 数位dp
题目描述 求 1~N 内包含数位串 “49” 的数的个数. 输入 The first line of input consists of an integer T (1 <= T <= 1 ...
- HDU 3555 Bomb 数位dp
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3555 Bomb Time Limit: 2000/1000 MS (Java/Others) Mem ...
- hud 3555 Bomb 数位dp
Bomb Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others) Total Subm ...
随机推荐
- harbar仓库的接口测试
一.接口测试命令 api接口文档:https://github.com/goharbor/harbor/blob/release-1.7.0/docs/swagger.yaml 1)查看所属项目的信息 ...
- vue 踩坑之组件传值
Vue 报错[Vue warn]: Avoid mutating a prop directly since the value will be overwritten whenever the pa ...
- Windows下svn使用教程
SVN简介: 为什么要使用SVN? 程序员在编写程序的过程中,每个程序员都会生成很多不同的版本,这就需要程序员有效的管理代码,在需要的时候可以迅速,准确取出相应的版本. Subversion是什么? ...
- 2018-8-10-win10-uwp-MetroLog-入门
title author date CreateTime categories win10 uwp MetroLog 入门 lindexi 2018-08-10 19:16:53 +0800 2018 ...
- linux设置python虚拟环境的环境变量
针对 linux系统中 python虚拟环境 设置环境变量 2种方法: 1.在建好的虚拟环境的 venv/bin/active 文件中,写入需要的环境变量,再进入虚拟环境: 如 配置文件路径 JERR ...
- K8S命令大总结
一.k8s-kubectl命令大全 Kubectl命令行管理对象类型 命令 描述 基础命令 create 通过文件名或标准输入创建资源. expose 将一个资源公开为一个新的Kubernetes服务 ...
- 数据结构 java概况
数据结构可以分为三种结构: 线性结构: 数组:栈:队列:链表:哈希表 树结构: 二叉树,二分搜索树,AVL,红黑树,Treap,Splay,堆,Trie,线段树,K-D树,并查集,哈夫曼树 图结构 邻 ...
- 从1<2<3的语法糖说起
python有一个很有意思的语法糖你可以直接写1<2<3. 这复合我们通常意义上的数学不等式,但对学过C等语言其实是有疑惑的. 我们知道不等式返回的其实是个Bool值,在C中是1,0因此C ...
- tracert显示为超时
1.那一跳禁PING2.那一跳不对TTL超时做响应处理,直接丢弃3.MPLS VPN网络
- tensorflow函数介绍(2)
参考:tensorflow书 1.模型的导出: import tensorflow as tf v1=tf.Variable(tf.constant(2.0),name="v1") ...