首页 > 其他 > 详细

F Find the AFei Numbers

时间:2019-01-06 19:43:12      阅读:165      评论:0      收藏:0      [点我收藏+]

链接:https://ac.nowcoder.com/acm/contest/338/F
来源:牛客网

题目描述

AFei loves numbers. He defines the natural number containing "520" as the AFei number, such as 1234520, 8752012 and 5201314. Now he wants to know how many AFei numbers are not greater than n.

输入描述:

The first line contains an integer T (1 <= T <= 100 ).

The following T lines contain an interger n ( 0 <= n <= 1e18 ).

输出描述:

For the last T lines, output the total numbers of AFei numbers that are not greater than n.
示例1

输入

复制
2
1000
5520

输出

复制
1
16

说明

For the first case, only 520 is AFei number.

For the second case, 520,1520, 2520, 3520, 4520, 5200, 5201, 5202, 5203, 5204, 5205, 5206, 5207, 5208, 5209 and 5520 are AFei number. So there are 16 AFei numbers.

技术分享图片
#include<bits/stdc++.h>
#define ll long long
using namespace std;

int temp[20];
ll dp[20][4];
ll dfs(int len, int s, bool limit)
{
    if(len == 0)
        return s==3;
    if(!limit && dp[len][s] != -1)
        return dp[len][s];
    int up = limit ? temp[len] : 9;
    ll sum = 0;
    for(int i = 0; i <= up; i ++)
    {
        int ss;
        if(s==3)
            ss=3;
        else if(i==5)
            ss=1;
        else if(s==1&&i==2)
            ss=2;
        else if(s==2&&i==0)
            ss=3;
        else
            ss=0;
        sum += dfs(len - 1, ss, limit && (i == up));
    }
    if(!limit)
        dp[len][s] = sum;
    return sum;
}
ll solve(ll x)
{
    int len = 0;
    while(x)
    {
        temp[++ len] = x % 10;
        x /= 10;
    }
    return dfs(len, 0, 1);
}
int main()
{
    memset(dp,-1,sizeof(dp));
    int T;
    scanf("%d", &T);
    while(T --)
    {
        ll b;
        scanf("%lld", &b);
        printf("%lld\n", solve(b));
    }
    return 0;
}
View Code

 

F Find the AFei Numbers

原文:https://www.cnblogs.com/DWVictor/p/10229999.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!