HDU - 3555 Bomb(数位dp)
题目链接:点击查看
题目大意:给定一个整数n,求从1到n的闭区间内含有相邻“49”的数字的个数。
题目分析:裸的数位dp,这里说一下两种做法,第一种是正着求,也就是求含有49的数字的个数,第二种是反着求,求不含49的数字的个数,最后再和n做差就好了。
正着求:
规定dp[pos][pre][state]为第pos位上,前一个数为pre,目前状态为state(所枚举的这个数字是否含有相邻的49)时的数字数量。
代码:
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#include<stack>
#include<queue>
#include<map>
#include<sstream>
#include<cmath>
using namespace std;typedef long long LL;const int inf=0x3f3f3f3f;const int N=800;LL dp[25][10][2];int b[25];LL dfs(int pos,int pre,bool state,bool limit)
{if(pos==-1)return state;if(!limit&&dp[pos][pre][state]!=-1)return dp[pos][pre][state];int up=limit?b[pos]:9;LL ans=0;for(int i=0;i<=up;i++){ans+=dfs(pos-1,i,state||i==9&&pre==4,limit&&i==b[pos]);}if(!limit)dp[pos][pre][state]=ans;return ans;
}LL solve(LL n)
{int cnt=0;while(n){b[cnt++]=n%10;n/=10;}return dfs(cnt-1,-1,false,true);
}int main()
{
// freopen("input.txt","r",stdin);memset(dp,-1,sizeof(dp));int w;cin>>w;while(w--){LL a;cin>>a;cout<<solve(a)<<endl;}return 0;
}
反着求:
规定dp[pos][state]为第pos位上,目前状态为state(前一位是否为4)时的数字数量。
代码:
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#include<stack>
#include<queue>
#include<map>
#include<sstream>
#include<cmath>
using namespace std;typedef long long LL;typedef unsigned long long ULL;const int inf=0x3f3f3f3f;const int N=30;LL dp[N][2];int b[N];LL dfs(int pos,int state,bool limit)
{if(pos==-1)return 1;if(!limit&&dp[pos][state]!=-1)return dp[pos][state];int up=limit?b[pos]:9;LL ans=0;for(int i=0;i<=up;i++){if(sta&&i==9)continue;ans+=dfs(pos-1,i==4,limit&&i==b[pos]);}if(!limit)dp[pos][state]=ans;return ans;
}LL solve(LL n)
{int cnt=0;while(n){b[cnt++]=n%10;n/=10;}return dfs(cnt-1,false,true);
}int main()
{
// freopen("input.txt","r",stdin);int w;cin>>w;memset(dp,-1,sizeof(dp));while(w--){LL n;cin>>n;cout<<n-solve(n)+1<<endl;}return 0;
}
HDU - 3555 Bomb(数位dp)相关推荐
- HDU 3555 Bomb (数位DP)
数位dp,主要用来解决统计满足某类特殊关系或有某些特点的区间内的数的个数,它是按位来进行计数统计的,可以保存子状态,速度较快.数位dp做多了后,套路基本上都差不多,关键把要保存的状态给抽象出来,保存下 ...
- Bomb HDU - 3555【数位dp】
Bomb HDU - 3555 The counter-terrorists found a time bomb in the dust. But this time the terrorists i ...
- HDU 3555 Bomb (数位DP-记忆化搜索模板)
题意 求区间[1,n]内含有相邻49的数. 思路 比较简单的按位DP思路.这是第一次学习记忆化搜索式的数位DP,确实比递推形式的更好理解呐,而且也更通用~可以一般化: [数位DP模板总结] int d ...
- HDU 3555 Bomb(数位DP模板啊两种形式)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3555 Problem Description The counter-terrorists found ...
- HDU 3555 - Bomb
第一道数位dp,属于基础模板,又自卑小时没学好数数了,只是不清楚为什么大家的dp定义都是相同的,很显然么,难道我写的是怪胎... /* ID:esxgx1 LANG:C++ PROG:hdu3555 ...
- HDU 3555: Bomb
///@link http://acm.hdu.edu.cn/showproblem.php?pid=3555///@author Sycamore///@date Sep, 14///@ref ku ...
- HDU 3652 B-number (数位DP)
Description 统计区间\([1,n]\)中是\(13\)的倍数且数字中含有"13"的数的个数. Input 多组用例,处理到文件尾.每组用例给出一个整数\(n\).\(1 ...
- 【hdu3555】Bomb 数位dp
题目描述 求 1~N 内包含数位串 "49" 的数的个数. 输入 The first line of input consists of an integer T (1 <= ...
- HDU - 6899 Xor(数位dp)
题目链接:点击查看 题目大意:给出四个整数 A , B , K , W ,问满足下列条件的二元对的个数: x , y 是整数 x <= A , y <= B abs( x - y ) &l ...
最新文章
- Boghe连接FreeSwitch的配置
- ubuntu: Authentication failure的解决办法
- python计算图解_图解NumPy,看这一篇就够了!
- Kali Linux下安装VMware
- 计算机科学与技术专业机遇与挑战,科学网—填报专业大类志愿:机遇与挑战 - 雒运强的博文...
- 一不小心节约了 591 台机器!
- Web常见漏洞修复建议
- 如何完成知识问答环节中的前期设置,题目准备及现场操作等主要流程?
- Untiy3D里用C#做出连线题目~
- 如何设计出令人惊叹的关卡:来自策划、美术与程序的标准
- java list随机取_java list随机抽取元素的案例
- Mybatis注解用法
- shell提示Algorithm negotiation fail
- unity运行时修改光源的颜色,变成白色
- html搜索栏背景透明,Win10秘籍:让Cortana搜索框“透明”给你看
- 都过度疲劳了,还谈什么效率?
- 自动驾驶惯性传感器中的基本原理笔记
- C语言小游戏,数字炸弹,手动设置炸弹的值和回合数。
- Windows: 如何给USB设备安装驱动?
- 十天学习Unity3D脚本(一)九个回调