B-number HDU - 3652

A wqb-number, or B-number for short, is a non-negative integer whose decimal form contains the sub- string "13" and can be divided by 13. For example, 130 and 2613 are wqb-numbers, but 143 and 2639 are not. Your task is to calculate how many wqb-numbers from 1 to n for a given integer n.

InputProcess till EOF. In each line, there is one positive integer n(1 <= n <= 1000000000).OutputPrint each answer in a single line.Sample Input

13
100
200
1000

Sample Output

1
1
2
2
基本和上一题一样,就是加了一点点的东西,居然一下子就A了,想想已经好久没有一下子A过题了。。。
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#include <queue>
#include <vector>
#include <math.h>
#include <string.h>
using namespace std;
#define INF 0x3f3f3f3f
#define N 20
#define PI acos(-1)
#define mod 2520
#define LL long long
/********************************************************/
LL dp[20][20][20][5],d[20];

LL dfs(int now, int up, int num, int p, int fp)
{
    if(now==1) return p&&num==0;
    if(!fp&&dp[now][up][num][p]!=-1) return dp[now][up][num][p];
    LL ans=0;
    int len=fp?d[now-1]:9;

    for(int i=0;i<=len;i++)
    {
        if((up==1&&i==3)||p==1)
            ans+=dfs(now-1, i, (num*10+i)%13, 1, fp&&i==len);
        else
            ans+=dfs(now-1, i, (num*10+i)%13, 0, fp&&i==len);
    }

    if(!fp) dp[now][up][num][p]=ans;
    return ans;
}

LL solve(LL X)
{
    int len=0;
    memset(dp,-1,sizeof(dp));

    while(X)
    {
        d[++len]=X%10;
        X/=10;
    }

    LL sum=0;
    for(int i=0;i<=d[len];i++)
        sum+=dfs(len, i, i, 0, i==d[len]);
    return sum;
}

int main()
{
    LL n,m;
    while(scanf("%lld",&n)!=EOF)
    {
        printf("%lld\n", solve(n));
    }
    return 0;
}

 

posted on 2018-01-31 17:53  小春天  阅读(97)  评论(0编辑  收藏  举报

导航