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.
Output
For each test case, output an integer indicating the final points of the power.
Sample Input
3
1
50
500
1
50
500
Sample Output
0
1
15
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=,INF=1e9+;
typedef long long ll;
inline ll read(){
char c=getchar();ll x=,f=;
while(c<''||c>''){if(c=='-')f=-;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
return x*f;
}
ll n;
ll d[N][];
void dp(){
d[][]=;
for(int i=;i<=;i++){
d[i][]=*d[i-][]-d[i-][];//!
d[i][]=d[i-][];
d[i][]=*d[i-][]+d[i-][];
//printf("d %d %d\n",d[i][0],d[i][2]);
}
}
ll sol(ll n){
int a[N],len=,flag=;
ll ans=;
while(n) a[++len]=n%,n/=;
a[len+]=;
for(int i=len;i>=;i--){
ans+=d[i-][]*a[i]; if(flag) ans+=a[i]*d[i-][];
else if(a[i]>) ans+=d[i-][];//maybe 49
if(a[i+]==&&a[i]==) flag=;//cannot +d[i-1][0],cause skyline and flag=1
//printf("%d %d %d\n",i,ans,flag);
}
if(flag) ans++;
return ans;
}
int main(){
dp();
int T=read();
while(T--){
n=read();
printf("%lld\n",sol(n));
}
}