Bomb
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 16362 Accepted Submission(s): 5979
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?
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.
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.和 不要62 类似
注意:
1. long long
2.if(a[i+1]==4&&a[i]>=9) flag=1;
不能+d[i-1][0],因为9的特殊性不可能有比9大的个位数使后面可能出现49,天际线就是49了
#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include <map> using namespace std; const int N=25,INF=1e9+5; typedef long long ll; inline ll read(){ char c=getchar();ll x=0,f=1; while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();} while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();} return x*f; } ll n; ll d[N][4]; void dp(){ d[0][0]=1; for(int i=1;i<=20;i++){ d[i][0]=10*d[i-1][0]-d[i-1][1];//! d[i][1]=d[i-1][0]; d[i][2]=10*d[i-1][2]+d[i-1][1]; //printf("d %d %d ",d[i][0],d[i][2]); } } ll sol(ll n){ int a[N],len=0,flag=0; ll ans=0; while(n) a[++len]=n%10,n/=10; a[len+1]=0; for(int i=len;i>=1;i--){ ans+=d[i-1][2]*a[i]; if(flag) ans+=a[i]*d[i-1][0]; else if(a[i]>4) ans+=d[i-1][1];//maybe 49 if(a[i+1]==4&&a[i]==9) flag=1;//cannot +d[i-1][0],cause skyline and flag=1 //printf("%d %d %d ",i,ans,flag); } if(flag) ans++; return ans; } int main(){ dp(); int T=read(); while(T--){ n=read(); printf("%lld ",sol(n)); } }