Bomb
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 23077 Accepted Submission(s): 8683
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 "49", the power of the blast would add one point.
Now the counter-terrorist knows the number N. They want to know the final points of the power. Can you help them?
Input
The first line of input consists of an integer T (1 <= T <= 10000), indicating the number of test cases. For each test case, there will be an integer N (1 <= N <= 2^63-1) as the description.
The input terminates by end of file marker.
Output
For each test case, output an integer indicating the final points of the power.
Sample Input
3 1 50 500
Sample Output
0 1 15
Hint
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.
Author
fatboy_cw@WHU
Source
2010 ACM-ICPC Multi-University Training Contest(12)——Host by WHU
问题链接:HDU3555 Bomb
问题简述:
计算[1,n]中含有49数的个数。
问题分析:
这是一个数位DP问题。
数位DP一般应用于求出在给定区间[A,B]内,符合条件P(i)的数i的个数,条件P(i)一般与数的大小无关,而与数的组成有关。
实际计算时,可以采用记忆化搜索实现,已经搜索过的就不再搜索。这种计算也可以称为记忆化计算,已经计算过的就不再计算,可以避免重复的计算,加快计算速度。
数位DP可以采用直接搜索和记忆化搜索两种方式来处理。
程序说明:
记忆化搜索:
设计数组dp[][]是一个关键!不同的问题略有不同,有经验后就简单了。
数组dp[][]的元素初始化为-1,表示其值尚未计算得到,需要用函数dfs()进行计算。初始化应该放在主函数中循环处理之前进行,可以最大限度避免重复计算。
函数solve(n)的功能是计算(0,n]的满足条件的数的个数。做法是将n的各位分解成数字位0-9,放入数组digits[]中,个位放 在digits[0]中,即低位放在下标小的数组元素中,高位放在下标大的数组元素中。然后通过深度优先搜索函数dfs(),根据数组digits[]指 定的数去搜索。
有关limit变量,以n=5676为例,简单说明如下:
1.开始时从最高位开始搜索,即从千位5开始,可取的数字只能是0-5(首次调用函数limit的实参是1,即只能取0-5);
2.千位若取0-4,百位可取的值则为0-9;
3.千位若取5,百位可取的值就只能取1-6(6是n的百位数字);
4.根据前2条,就用参数limit来控制下一个数位的取值范围,在搜索的时候就看是否i==digits[pos],不等的话limit取值 false,下一位(低一位)取值范围则为0-9,否则limit取值true,下一位(低一位)取值范围则为0-x,x为下一位的数字。
题记:(略)
参考链接:(略)
AC的C++语言程序(数位DP+记忆化搜索,简洁易懂)如下:
/* HDU3555 Bomb */
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 20; // 位数,long long类型实际不超过19位,这里用20
const int D = 10; // 10进制数字的个数
int digits[N + 1];
LL dp[N][D]; // dp[i][d]-共i位,前导为数字d的满足条件(不含49)数的数量
/*
* 参数:
* pos - 数位位置,即当前处理数的第几位,从高位开始
* pre - 前导,即前一位数字
* limit - 是否为数位上界(最大数字)
*/
LL dfs(int pos, int pre, bool limit)
{
if(pos == -1) // 递归边界,已经枚举结束,则1个数满足条件
return 1;
if(!limit && dp[pos][pre] != -1) // 已经搜索过的不再搜索,直接使用之前的计算结果
return dp[pos][pre];
// 计数
LL ans = 0;
int maxd = limit ? digits[pos] : 9; // 枚举数字,如果数字不同则枚举0-9
for(int i = 0; i <= maxd; i++) {
if(pre == 4 && i == 9)
;
else
ans += dfs(pos - 1, i, limit && i == digits[pos]);
}
if(!limit)
dp[pos][pre] = ans;
return ans;
}
// 计算[0,n]中不含49的数的数量
LL solve(LL n)
{
int len = 0;
while(n) {
digits[len++] = n % 10;
n /= 10;
}
return dfs(len - 1, 0, 1);
}
int main()
{
memset(dp, -1, sizeof(dp));
int t;
scanf("%d", &t);
while(t--) {
LL n;
scanf("%lld", &n);
printf("%lld\n", n - (solve(n) - 1)); // 需要去除0,所以要减去1
}
return 0;
}
AC的C++语言程序(数位DP+记忆化搜索)如下:
/* HDU3555 Bomb */
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 20;
int digits[N + 1];
LL dp[N][3];
// dp[i][0]-共i位,不含49数的数量
// dp[i][1]-共i位,不含49,高位为9数的数量
// dp[i][2]-共i位,含有49数的数量
/*
* 参数:
* pos - 数位位置,即当前处理数的第几位,从高位开始
* pre - 前导,即前一位数字
* limit - 是否为数位上界(最大数字)
*/
LL dfs(int pos, int pre, bool limit)
{
if(pos == -1) // 递归边界,已经枚举结束,这个数含有49则1个
return pre == 2 ? 1 : 0;
if(!limit && dp[pos][pre] != -1) // 已经搜索过的不再搜索,直接使用之前的计算结果
return dp[pos][pre];
// 计数
LL ans = 0;
int maxd = limit ? digits[pos] : 9; // 枚举数字,如果数字不同则枚举0-9
for(int i = 0; i <= maxd; i++) {
if(pre == 2 || (pre == 1 && i == 9))
ans += dfs(pos - 1, 2, limit && i == maxd);
else if(i == 4)
ans += dfs(pos - 1, 1, limit && i == maxd);
else
ans += dfs(pos - 1, 0, limit && i == maxd);
}
if(!limit)
dp[pos][pre] = ans;
return ans;
}
LL solve(LL n)
{
int len = 0;
while(n) {
digits[len++] = n % 10;
n /= 10;
}
return dfs(len - 1, 0, 1);
}
int main()
{
memset(dp, -1, sizeof(dp));
int t;
scanf("%d", &t);
while(t--) {
LL n;
scanf("%lld", &n);
printf("%lld\n", solve(n));
}
return 0;
}
AC的C++语言程序(数位DP)如下: