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?
InputThe 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.
OutputFor 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.
这道题放在写过找0和找1的后面。。。我在写这题之前刚写过62的那道题,结果就感觉比较简单了。。。果然水题比较好写,至少比上一道好写。。。
#include <stdio.h> #include <stdlib.h> #include <iostream> #include <algorithm> #include <queue> #include <vector> #include <math.h> #include <string.h> using namespace std; #define INF 0x3f3f3f3f #define N 20 #define PI acos(-1) #define mod 2520 #define LL long long /********************************************************/ LL dp[80][20][5],d[80]; LL dfs(int now, int up, int p, int fp) { if(now==1) return p; if(!fp&&dp[now][up][p]!=-1) return dp[now][up][p]; LL ans=0; int len=fp?d[now-1]:9; for(int i=0;i<=len;i++) { if((up==4&&i==9)||p==1) ans+=dfs(now-1, i, 1, fp&&i==len); else ans+=dfs(now-1, i, 0, fp&&i==len); } if(!fp) dp[now][up][p]=ans; return ans; } LL solve(LL X) { int len=0; memset(dp,-1,sizeof(dp)); while(X) { d[++len]=X%10; X/=10; } LL sum=0; for(int i=0;i<=d[len];i++) sum+=dfs(len, i, 0, i==d[len]); return sum; } int main() { LL n,m; int T; scanf("%d", &T); while(T--) { scanf("%lld",&n), printf("%lld\n", solve(n)); } return 0; }