zoukankan      html  css  js  c++  java
  • 【DP练习】B-number(HDU3652)

    Description

    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.
    求从1到N,含有13且能被13整除的数的个数

    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题

    代码:

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    using namespace std;
    int bit[15],dp[14][14][2][2];
    int dfs(int x,int mod,int last,int have,int flag)//
    {
    	if(x==1) return (!mod&&have);
    	if(!flag&&dp[x][mod][last][have]!=-1) return dp[x][mod][last][have];
    	int count=flag?bit[x-1]:9,ans=0;
    	for(int i=0;i<=count;++i)
    	{
    		if(last&&i==3) ans+=dfs(x-1,(mod*10+i)%13,0,1,flag&&i==count);
    		else ans+=dfs(x-1,(mod*10+i)%13,i==1,have,flag&&i==count);
    	}
    	return flag?ans:dp[x][mod][last][have]=ans;
    }
    int start(int x)
    {
    	int len=0;
    	for(;x;x/=10) bit[++len]=x%10;
    	memset(dp,-1,sizeof(dp));
    	return dfs(len+1,0,0,0,1);
    }
    int main()
    {
    	int n;
    	while(~scanf("%d",&n)) printf("%d
    ",start(n));
    	return 0;
    }
    
  • 相关阅读:
    hdu2476
    zoj3469 区间dp好题
    区间dp好题cf149d 括号匹配
    cf1108e 线段树区间更新+扫描线
    完全背包记录路径poj1787 好题
    cf1104d二分+数学
    01背包专题
    hdu1069线性dp
    有源汇的上下界最大流
    有源汇的上下界最大流
  • 原文地址:https://www.cnblogs.com/wuwendongxi/p/13307151.html
Copyright © 2011-2022 走看看