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 ...
随机推荐
- BUUCTF--easyer
测试文件下载:https://buuoj.cn/files/b66a080016da04abfc002a336c0132e5/easyre.zip?token=eyJ0ZWFtX2lkIjpudWxs ...
- Spike Your CPU’s Processor in .Net
using System.Threading; using System.Runtime.InteropServices; // Target a specific processor for the ...
- rtp/rtcp
rtp/rtcp stack custom rtp ORTP UCL Common RTP library Bell Labs RTP Library jrtplib 1.custom rtp sen ...
- 四 shell基本命令
一 内置命令 hlep 命令 帮助 help test help -s printf 显示内置命令的语法格式 echo 用来显示一行文字 echo "hello world&quo ...
- Maven POM中的各种scope的行为总结
compile:默认的scope.任何定义在compile scope下的依赖将会在所有的class paths下可用.maven工程会将其打包到最终的artifact中.如果你构建一个WAR类型的a ...
- 输出匹配项:grep
命令格式: grep pattern [file...] When grep encounters a "pattern" in the file, it prints out t ...
- 51nod 1605:棋盘问题
题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1605 题目中最有用的点其实还是x必为奇数 #include& ...
- 为什么js的"关联数组"不能转成json字符串而对象可以?
定义这么一个js的“关联数组”: var arr = new Array(); arr[; arr[; alert(JSON.stringify(arr)); 得到的结果如图: [] 一句话,你的 a ...
- gensim word2vec |来自渣渣硕的学习笔记
最近写论文跑模型,要用到word2vec,但是发现自己怎么也看不懂网上的帖子,还是自己笨吧,所以就有了我的第一篇博客!!! 关于word2vec工具打算写一个系列的,当然今天这篇文章只打算写: 如何 ...
- 提高wifi速度的设置办法
系列的提高wifi速度的设置办法 在DNS一栏有你们家的地址,在你们家的地址前输入“114.114.114.114”并以“,”结尾(注意:要用英文输入法哦.) 设置完后点击左上角的“无线局域网”回到初 ...