zoukankan      html  css  js  c++  java
  • B-number HDU

    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.

    Input

    Process till EOF. In each line, there is one positive integer n(1 <= n <= 1000000000).

    Output

    Print each answer in a single line.

    Sample Input

    13
    100
    200
    1000

    Sample Output

    1
    1
    2
    2

    与其叫数位dp,我觉得这种题其实就是搜索

    此题我开了4维数组

    分别记录当前位数,是否被限制,前面是否为1,前面是否出现了13

    #include<stdio.h>
    #include<string.h>
    #include<algorithm>
    #include<iostream>
    using namespace std;
    int dp[20][20][2][2],a[20];
    int dfs(int pos,int mod,int lim,int sta,int bef)
    {
    
        int s,ans=0;
        if(pos==0) return mod%13==0&&sta==1;
        if(!lim&&dp[pos][mod][bef][sta]!=-1) return dp[pos][mod][bef][sta];
        s=lim==1?a[pos]:9;
        int temp;
        for(int i=0; i<=s; i++)
        {
            temp=(mod*10+i)%13;
            //cout<<pos<<" "<<lim<<" "<<bef<<" "<<"temp="<<temp<<" "<<s<<" "<<i<<endl;
            if(i==1)
            {
                ans+=dfs(pos-1,temp,lim&&i==s,sta,1);
            }
            else if(bef&&i==3)
            {
                ans+=dfs(pos-1,temp,lim&&i==s,1,0);
            }
            else
            {
                ans+=dfs(pos-1,temp,lim&&i==s,sta,0);
            }
        }
        if(!lim) dp[pos][mod][bef][sta]=ans;
        //cout<<ans<<" "<<pos<<" "<<bef<<endl;
        return ans;
    }
    int solve(int x)
    {
        int cnt=0;
        while(x!=0)
        {
            cnt++;
            a[cnt]=x%10;
            x/=10;
        }
        return dfs(cnt,0,1,0,0);
    }
    int main()
    {
        int n;
        memset(dp,-1,sizeof(dp));
        while(~scanf("%d",&n))
        {
    
            cout<<solve(n)<<endl;
           //printf("%d
    ",ans);
        }
        return 0;
    }
  • 相关阅读:
    ASP.NET常用技巧方法代码断
    130道ASP.NET面试题
    ASP.NET 数据绑定常用代码及其性能分析
    asp.net C# 时间格式大全
    ASP.NET 日期 时间 年 月 日 时 分 秒 格式及转换
    经典算法,每个语言都出现的算法
    Asp.net 后台添加CSS、JS、Meta标签
    ASP.NET 窗体间传值实现方法详解
    asp.net 常用的几种调用存储过程的方法
    codeforces 868C
  • 原文地址:https://www.cnblogs.com/caowenbo/p/11852312.html
Copyright © 2011-2022 走看看