【数位dp】hdu3555 Bomb

题意就是找0到n有多少个数中含有49。数据范围接近10^20

DP的状态是2维的dp[len][3]
dp[len][0] 代表长度为len不含49的方案数
dp[len][1] 代表长度为len不含49但是以9开头的数字的方案数
dp[len][2] 代表长度为len含有49的方案数

状态转移如下
dp[i][0] = dp[i-1][0] * 10 - dp[i-1][1];  // not include 49  如果不含49且,在前面可以填上0-9 但是要减去dp[i-1][1] 因为4会和9构成49
dp[i][1] = dp[i-1][0];  // not include 49 but starts with 9  这个直接在不含49的数上填个9就行了
dp[i][2] = dp[i-1][2] * 10 + dp[i-1][1]; // include 49  已经含有49的数可以填0-9,或者9开头的填4

接着就是从高位开始统计

在统计到某一位的时候,加上 dp[i-1][2] * digit[i] 是显然对的,因为这一位可以填 0 - (digit[i]-1)
若这一位之前挨着49,那么加上 dp[i-1][0] * digit[i] 也是显然对的。
若这一位之前没有挨着49,但是digit[i]比4大,那么当这一位填4的时候,就得加上dp[i-1][1]

——http://www.cnblogs.com/jackge/archive/2013/05/15/3080958.html

#include<cstdio>
using namespace std;
typedef long long ll;
ll f[21][3],n;
void init()
{
	f[0][0]=1;
	for(int i=1;i<=20;++i)
	  {
	  	f[i][0]=f[i-1][0]*10-f[i-1][1];
	  	f[i][1]=f[i-1][0];
	  	f[i][2]=f[i-1][2]*10+f[i-1][1];
	  }
}
int T;
int main()
{
	init();
//	freopen("hdu3555.in","r",stdin);
	scanf("%d",&T);
	int bit[23];
	for(;T;--T)
	  {
	  	int m=0; ll ans=0; bool flag=0;
	  	scanf("%I64d",&n); ++n;
	  	while(n)
	  	  {
	  	  	bit[++m]=n%10;
	  	  	n/=10;
	  	  }
	  	bit[m+1]=bit[m+2]=0;
	  	for(int i=m;i;--i)
	  	  {
	  	  	if(bit[i+1]==9 && bit[i+2]==4)
	  	  	  flag=1;
	  	  	ans+=f[i-1][2]*(ll)bit[i];
	  	  	if(flag)
	  	  	  ans+=(ll)bit[i]*f[i-1][0];
	  	  	else if(bit[i] > 4)
	  	  	  ans+=f[i-1][1];
	  	  }
	  	printf("%I64d\n",ans);
	  }
	return 0;
}
时间: 2025-01-06 15:09:46

【数位dp】hdu3555 Bomb的相关文章

数位dp hdu3555

#include <cstdio> #include <cstring> #include <set> #include <iostream> #include <map> #include <math.h> #include <algorithm> using namespace std; typedef long long ll; const int N = 1000005; int t,cnt,num[25]; ll

hdu3555 Bomb (记忆化搜索 数位DP)

http://acm.hdu.edu.cn/showproblem.php?pid=3555 Bomb Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others) Total Submission(s): 7316    Accepted Submission(s): 2551 Problem Description The counter-terrorists found a time

hdu---(3555)Bomb(数位dp(入门))

Bomb Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)Total Submission(s): 7921    Accepted Submission(s): 2778 Problem Description The counter-terrorists found a time bomb in the dust. But this time the terrorists

hdu3555 Bomb(数位dp)

Bomb                                                                                     Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)                                                                             

HDU3555 Bomb 题解 数位DP

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3555 题目大意:求 \([1,n]\) 范围内有多少数包含"49". 解题思路: 这个问题我们可以分两种解法来考虑:第一种是求不包含"49"的数的数量,用后减一下:另一种就是直接求包含"49"的数的数量. 解法1:求多少数不包含"49" 这种方法我们先通过数位DP求出 \([0,n]\) 区间范围内有多少数不包含"49&

hdu3555 数位dp

http://acm.hdu.edu.cn/showproblem.php?pid=3555 Problem Description The 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 nu

hdu 3555 Bomb(数位dp)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3555 题目大意:就是给你一个数n,判断从0到n有多少个数含有数字49...... 是不是觉得跟hdu2089很相似呀... 思路:跟hdu2089一样的,注意给出的数比较大,所以这儿用__int64  .... code: #include<cstdio> #include<iostream> #include<cstring> #include<algorithm&

[ACM] hdu 3555 Bomb (数位DP,统计1-N中含有“49”的总数)

Bomb Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others) Total Submission(s): 7187 Accepted Submission(s): 2512 Problem Description The counter-terrorists found a time bomb in the dust. But this time the terrorists impro

HDU - 3555 Bomb (数位DP)

题意:求1-n里有多少人包含"49"的数字 思路:数位DP,分三种情况:到第i位没有49的情况,到第i位没有49且最高位是9的情况,到第i位有49的情况,将三种情况都考虑进去就是了 #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; long long dp[30][3], n; int arr

hud 3555 Bomb 数位dp

Bomb Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others) Total Submission(s): 19122    Accepted Submission(s): 7068 Problem Description The counter-terrorists found a time bomb in the dust. But this time the terrorist