Bomb
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 876 Accepted Submission(s): 319
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
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
Recommend
zhouzeyong
题目大意:
在小于等于N的自然数中统计有多少含有49序列;比较简单的数位dp:
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
__int64 dp[20][3];
int digit[20];
int len;
void num_input()
{
__int64 n;
len =0;
scanf("%I64d",&n);
n++;
while(n){digit[++len]=n%10;n/=10;}
}
void Init()
{
memset(dp,0,sizeof(dp));
dp[0][0]=1;
for(int k=1;k<20;k++)
{
dp[k][0]=dp[k-1][0]*10-dp[k-1][1];//not include 49
dp[k][1]=dp[k-1][0];//not include 49 with a head 9
dp[k][2]=dp[k-1][1]+dp[k-1][2]*10;//include 49
//cout<<dp[k][0]<<' '<<dp[k][1]<<' '<<dp[k][2]<<endl;
}
}
void solve()
{
__int64 ans=0;
bool flag=false;
int last=digit[len];
for(int i=len;i>=0;i--)
{
//cout<<i<<' '<<flag<<' '<<digit[i]<<' '<<dp[i-1][0]<<endl;
ans=ans+digit[i]*dp[i-1][2];
if(flag)
ans=ans+dp[i-1][0]*digit[i];
if(!flag&&digit[i]>4)
ans=ans+dp[i-1][1];
if(last==4&&digit[i]==9)
flag=1;
last=digit[i];
}
printf("%I64d\n",ans);
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
num_input();
Init();
solve();
}
return 0;
}
分享到:
相关推荐
搞acm的没有谁不知道杭电题库,但是杭电上的OJ到底该怎么用,对于某些初学者确实一个难题,所以How_to_Use_HDOJ.rar应运而生
杭电OJ(1000-1099) AC 代码
杭电acm解题报告 详细解析2000-2099 适合acm初学者
HDOJ 源代码 包含几百道HDOJ题目源码
codj,hdoj的源码(50-60题)
some code of HDOJ of mine
杭电oj4405,一道简单的概率dp题目
杭州电子科技大学的离线版OJ,可用于断网情况下练习ACM
一些HDOJ上的DP题目的小总结,但愿能帮到那些想专攻DP的人吧
HDOJ题目分类HDOJ题目分类HDOJ题目分类
ACM ICPC HDOJ1002
ACM ICPC HDOJ1001
hdoj1001标程
hdoj上的资源,代码有注释,很不错的哦
hdoj1004,解题代码,答案代码,欢迎下载
ACM ICPC HDOJ1008
ACM ICPC HDOJ1003
杭州电子科技大学hdoj1002,大整数相加问题
杭州电子科大HDOJ
ACM ICPC HDOJ1000