hdu_3555 bomb

数位动态规划 
    数位动态规划是求解一个大区间[L, R]中间满足条件Q的所有数字的个数(或者和,或其他)的一种方法。它通过分析每一位上的数字,一般用 dp[len][digit][...] 来表示状态“len位长的数字,最高位数字为digit所具有的xx特性”,利用记忆化搜索保存中间结果,从而加快求解速度。 
    通过求 f(n) 从0到n中满足条件Q的数字的个数,则所求的结果为 f(R) - f(L-1).

题目大意 
    给定数字n,找出从0到n中满足条件“数字k中有49(4和9连续)存在”的数字的个数。

题目分析 
    直接数位dp即可。

实现

#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
typedef long long ll;
ll dp[30][10]; //dp[len][digit]表示长度为len,且最高位为digit的满足条件Q的数字个数
ll base[30];
int bits[30];
ll Dfs(int len, int digit, bool end_flag, ll n){
	if (len <= 1)
		return 0;
	if (!end_flag && dp[len][digit] != -1)
		return dp[len][digit];

	ll ans = 0;
	int end = end_flag ? bits[len - 2] : 9;
	for (int i = 0; i <= end; i++){
		if (digit == 4 && i == 9){
			if (end_flag)
				ans += (1 + n % base[len - 2]);
			else
				ans += base[len - 2];
		}else
			ans += Dfs(len - 1, i, end_flag && (i == end), n);
	}
	if (!end_flag)
		dp[len][digit] = ans;
	return ans;
}
int Init(ll n){
	memset(bits, 0, sizeof(bits));
	int k = 0;
	base[0] = 1;
	while (n){
		bits[k++] = n % 10;
		base[k] = base[k - 1] * 10;
		n /= 10;
	}
	return k;
}
ll Solve(ll n){
	int len = Init(n);
	return Dfs(len + 1, 0, true, n);
}
int main(){
	int T;
	ll num;
	scanf("%d", &T);
	memset(dp, -1, sizeof(dp));
	while (T--){
		scanf("%I64d", &num);
		ll ret = Solve(num);
		printf("%I64d\n", ret);
	}
	return 0;
}

在数据范围较大的时候,使用 long long int类型,但要注意所有使用long long int类型的变量被调用的函数中,参数形式均保持一致为long long int。

时间: 2024-12-13 09:50:55

hdu_3555 bomb的相关文章

CSAPP 3e: Bomb lab (secret_phase)

这是秘密关卡,需要通过主动调用secret_phase函数才能触发,可以通过call secret 或者jump *0x地址来调用. 贴出函数:(fun7函数部分没有注释,后边续上了手写的图来解析这个函数了) 0000000000401204 <fun7>: 401204: 48 83 ec 08 sub $0x8,%rsp 401208: 48 85 ff test %rdi,%rdi 40120b: 74 2b je 401238 <fun7+0x34>;如果%rdi==0,r

CSAPP 3e: Bomb lab (phase_6)

这一关很复杂,需要非常耐心.如果感觉容易在循环中绕晕,可以参考一下我最后附上的画图分析法2333,小把戏,不过挺有用的. 先看函数phase_6: 00000000004010f4 <phase_6>: 4010f4: 41 56 push %r14 4010f6: 41 55 push %r13 4010f8: 41 54 push %r12 4010fa: 55 push %rbp 4010fb: 53 push %rbx 4010fc: 48 83 ec 50 sub $0x50,%rs

CSAPP 3e: Bomb lab (phase_5)

调出phase_5函数: 0000000000401062 <phase_5>: 401062: 53 push %rbx 401063: 48 83 ec 20 sub $0x20,%rsp 401067: 48 89 fb mov %rdi,%rbx 40106a: 64 48 8b 04 25 28 00 mov %fs:0x28,%rax ;此处搞不懂 401071: 00 00 401073: 48 89 44 24 18 mov %rax,0x18(%rsp) 401078: 31

HDU 3622 Bomb Game(二分+2-SAT)

Bomb Game Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 5396    Accepted Submission(s): 1925 Problem Description Robbie is playing an interesting computer game. The game field is an unbounde

[HDU3555]Bomb

试题描述 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 number sequence includes the sub-sequence "49", the power o

HDU 3555 —— Bomb

Bomb 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 number sequence includes the sub-sequence "4

HDU 5653 Bomber Man wants to bomb an Array. DP

Bomber Man wants to bomb an Array. Problem Description Given an array and some positions where to plant the bombs, You have to print the Total Maximum Impact. Each Bomb has some left destruction capability L and some right destruction capability R wh

Bomb

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 number sequence includes the sub-sequence "49", the

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&