Bomb
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.
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. 这个题被longlong坑爽了……没改完,wa了若干次
跟前一个题太像了,不细说了哈……主要是因为之前浏览器抽了一次,写的东西没有了,不想再打了……
#include <iostream>
#include <string.h>
#include <stdio.h>
using namespace std;
long t;
long long n;
long long f[23][3];
///f[i][0] i位的数字 没有不符合条件的数
///f[i][1] i位的数字 没有不符合条件的数但最高位是9
///f[i][1] i位的数字 有不符合条件的数
void prepare()
{
memset(f, 0, sizeof(f));
f[0][0] = 1;
for (long i = 1; i <= 20; i++)
{
f[i][0] = f[i - 1][0] * 10LL - f[i - 1][1];
f[i][1] = f[i - 1][0];
f[i][2] = f[i - 1][2] * 10LL + f[i - 1][1];
}
}
long long calc(long long n)
{
long len = 0;
long long bit[30] = {0};
long long tmp = n;
while (n)
{
bit[++len] = n % 10;
n /= 10;
}
long long re = 0;
bool b = false;
for (long i = len; i > 0; i--)
{
re += f[i - 1][2] * bit[i];
if (b) re += f[i - 1][0] * bit[i];
if ((!b) && (bit[i] > 4)) re += f[i - 1][1];
if (bit[i] == 9 && bit[i + 1] == 4) b = true;
}
return re;
}
int main()
{
scanf("%d", &t);
prepare();
while (t--)
{
cin >> n;
cout << calc(n + 1) << endl;
}
return 0;
}