Bomb

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)

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
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.

Solution：
很裸的数位dp，记忆化搜索好啊，不过数组开小了，炸了几次。
1 #include <cstdio>
2 #include <cstring>
3 #include <iostream>
4 #include <algorithm>
5 using namespace std;
6 long long T,dp,bit;
7 #define int long long
8 long long n;
9 long long dfs(int pos,int pre,bool Limit) {
10     if(pos<=0&&pre>=0) return 1;
11     if(pos<=0) return 0;
12     if(pre>=0&&!Limit&&dp[pos][pre]!=-1) return dp[pos][pre];
13     int End=Limit?bit[pos]:9;
14     long long ans=0;
15     for(int now,i=0; i<=End; ++i) {
16         if(pre==4&&i==9) continue;
17         now=i;
18         if(pre<0&&!i) now=pre;
19         ans+=dfs(pos-1,now,Limit&&(i==End));
20     } if(pre>=0&&!Limit) dp[pos][pre]=ans;
21     return ans;
22 }
23 signed main() {
24     //freopen("data.in","r",stdin);
25     //freopen("my.out","w",stdout);
26     scanf("%lld",&T);
27     for(int t=1; t<=T; ++t,bit=0) {
28         memset(dp,0xff,sizeof(dp));
29         scanf("%lld",&n);
30         long long x=n;
31         while(n) bit[++bit]=n%10,n/=10;
32         printf("%lld\n",x-dfs(bit,-10,true));
33     } return 0;
34 }

posted @ 2017-11-01 13:23  Forever_goodboy  阅读(121)  评论(0编辑  收藏  举报