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 \leq T \leq 10000)$, indicating the number of test cases. For each test case, there will be an integer $N (1 \leq N \leq 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
1 | 3 |
Sample Output
1 | 0 |
Analysis
数位DP的模板题。题意是给出数N,求1到N内包含数字”49”的数的个数。
将数N按从低位到高位的顺序逐位存储在数组a中,从最高位开始往最低位搜索。
解释一下搜索的函数
1 | ll dfs(int pos, bool state, bool limit) |
pos表示当前dfs正在搜索的数位,state存储了搜索的前一位是不是”4”,如果true则表明是”4”,limit表示当前位是否存在最高位,例如1275,在前面为12时第三位最高只能取7,但是前面为11时第三位就可以取1到9,这时就需要用limit判断它否取到9。因为从最高位开始搜索,所以一开始传入limit为true。
这里用dp[20][2]来记忆化搜索结果,其中dp[i][0]表示搜索的前一位不是”4”的情况下,当前位有多少取法使其不含”49”,而dp[i][1]表示前一位是”4”的情况下,当前位有多少种取法使其不含”49”。
最后搜索出的结果是从0到N中不包含”49”的数字的个数,显然0是不包含的,所以要求1到N内包含”49”的数的个数的最终的结果应该是
1 | N-solve(N)+1 |
代码如下。
Code
1 |
|