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.数位dp,找子序列‘49’
记一下有没有出现过'4',是否已经出现过"49"
1 #include<cstdio> 2 #include<iostream> 3 #include<cstring> 4 #include<cstdlib> 5 #include<algorithm> 6 #include<cmath> 7 #include<queue> 8 #include<deque> 9 #include<set> 10 #include<map> 11 #include<ctime> 12 #define LL long long 13 #define inf 0x7ffffff 14 #define pa pair<int,int> 15 #define mkp(a,b) make_pair(a,b) 16 #define pi 3.1415926535897932384626433832795028841971 17 using namespace std; 18 inline LL read() 19 { 20 LL x=0,f=1;char ch=getchar(); 21 while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} 22 while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} 23 return x*f; 24 } 25 LL n,len; 26 LL f[63][10][2]; 27 int d[63]; 28 inline LL dfs(int now,int dat,int sat,int fp) 29 { 30 if (now==1)return sat; 31 if (!fp&&f[now][dat][sat]!=-1)return f[now][dat][sat]; 32 LL ans=0,mx=(fp?d[now-1]:9); 33 for (int i=0;i<=mx;i++) 34 { 35 if (sat||!sat&&dat==4&&i==9)ans+=dfs(now-1,i,1,fp&&mx==i); 36 else ans+=dfs(now-1,i,0,fp&&mx==i); 37 } 38 if (!fp&&f[now][dat][sat]==-1)f[now][dat][sat]=ans; 39 return ans; 40 } 41 inline LL calc(LL x) 42 { 43 LL xxx=x; 44 len=0; 45 while (xxx) 46 { 47 d[++len]=xxx%10; 48 xxx/=10; 49 } 50 LL sum=0; 51 for (int i=0;i<=d[len];i++) 52 sum+=dfs(len,i,0,i==d[len]); 53 return sum; 54 } 55 int main() 56 { 57 memset(f,-1,sizeof(f)); 58 int T=read(); 59 while (T--)n=read(),printf("%lld ",calc(n)); 60 }