HDU 3555 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 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

题目分析:

f[i][j]数组表示前i位,以j开头的数中,不含49串的数的个数:
因为是闭区间 所以要n++ 否则没有计算n本身有49的情况

#include <cstdio>
#include <cstring>
#include <cmath>
#include <queue>
#include <algorithm>
using namespace std;
long long f[100][20];
long long n;
int t;
void init()
{
	f[0][0]=1;
	for(int i=1;i<=20;i++)
		for(int j=0;j<=9;j++)
			for(int k=0;k<=9;k++)
				if(j!=4||k!=9)
					f[i][j]+=f[i-1][k];
}
int a[10000];
void check(long long x)
{
	a[0]=0;
	while(x)
	{
		a[++a[0]]=x%10;
		x/=10;
	}
	long long ans=0;
	for(int i=a[0];i>=1;i--)
	{
		for(int j=0;j<a[i];j++)
			if(a[i+1]!=4||j!=9)
				ans+=f[i][j];
		if(a[i+1]==4&&a[i]==9) break;
	}
	printf("%lld\n",n+1-ans);
}

int main()
{
	scanf("%d",&t);
	init();
	while(t)
	{
		t--;
		scanf("%lld",&n);
		check(n+1);  
	}
    return 0;
}

 

发表评论

邮箱地址不会被公开。 必填项已用*标注