hdu3555

3295 단어 dp

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

이 문제는 롱롱에 의해 시원해졌다.
앞의 문제가 너무 비슷해서 자세히 말하지 않겠어요...주로 전에 브라우저에서 한 번 뽑았는데 쓴 것이 없어서 더 이상 치고 싶지 않아요...
#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;
}

좋은 웹페이지 즐겨찾기