HDU-3555 Bomb

http://acm.hdu.edu.cn/showproblem.php?pid=3555

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

 

题意:统计[1,n]有多少个数含有'49'。

思路:数位dp,pre记录上一个数是多少,枚举到49时分limit或!limit直接计算后边的数总数

#include<iostream>
#include<cstring>
using namespace std;
typedef long long ll;

ll t,n,a[70],f[70][2];

ll fac(int x)
{
	ll ret=1;
	while(x--)ret*=10;
	return ret;
}

ll calc(int pos)
{
	ll ret=1;
	for(ll v=1,i=0;i<=pos;i++,v*=10)
	{
		ret+=a[i]*v;
	}
	return ret;
}

ll dfs(int pos,bool pre,bool limit)
{
	if(pos==-1)return 0;
	if(!limit && f[pos][pre]!=-1)return f[pos][pre];
	int up=(limit?a[pos]:9);
	ll temp=0;
	for(int i=0;i<=up;i++)
	{
		if(pre&&i==9&&!limit)temp+=fac(pos);
		else if(pre&&i==9)temp+=calc(pos-1);
		else temp+=dfs(pos-1,i==4,limit&&i==a[pos]);
	}
	if(!limit)f[pos][pre]=temp;
	return temp;
}

ll solve(ll n)
{
	int pos=0;
	while(n)
	{
		a[pos++]=n%10;
		n/=10;
	}
	return dfs(pos-1,0,1);
}

int  main()
{
//	freopen("input.in","r",stdin);
	memset(f,-1,sizeof(f));
	cin>>t;
	while(t--)
	{
		cin>>n;
		cout<<solve(n)<<endl;
	}
	return 0;
}

 

全部评论

相关推荐

02-22 20:28
重庆大学 Java
程序员牛肉:首先不要焦虑,你肯定是有希望的。 首先我觉得你得好好想一想自己想要什么。找不到开发岗就一定是失败的吗?那开发岗的35岁危机怎么说?因此无论是找工作还是考公我觉得你都需要慎重的想一想。但你一定要避开这样一个误区:“我是因为找不到工作所以不得不选择考公”。 千万不要这么想。你这个学历挺好的了,因此你投后端岗肯定是有面试机会的。有多少人简历写的再牛逼,直接连机筛简历都过不去有啥用?因此你先保持自信一点。 以你现在的水平的话,其实如果想要找到暑期实习就两个月:一个月做项目+深挖,并且不断的背八股。只要自己辛苦一点,五月份之前肯定是可以找到暑期实习的,你有点太过于高看大家之间的技术差距了。不要焦虑不要焦虑。 除此之外说回你这个简历内容的话,基本可以全丢了。如果想做后端,先踏踏实实做两个项目再说+背八股再说。如果想考公,那就直接备战考公。 但是但是就像我前面说的:你考公的理由可以是因为想追求稳定,想追求轻松。但唯独不能是因为觉得自己找不到工作。不能这么小瞧自己和自己的学历。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务