HDU 3652 B-number(数位DP)

HDU 3652 B-number(数位DP)

大家好,又见面了,我是你们的朋友全栈君。

B-number

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1231    Accepted Submission(s): 651

 

Problem 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.
 
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
 
Author
wqb0039
 
 
题意
找包含13且被13整除的个数。
 
分析
能被13整除,只需要记录%13的值就好。那么如何找包含13的呢,我们需要记录当前最后一位的值,这样才可以从1 –》13。
dp[i][j][k][z]:i:处理的数位,j:该数对13取模以后的值,k:是否已经包含13,z结尾的数
#include <iostream>
#include <string.h>
#include <algorithm>
#include <stdio.h>
using namespace std;
int dp[12][15][2][10];
int bit[12];
int dfs(int pos,int num,bool t,int e,bool flag)
{
    if(pos==-1)return t&&(num==0);
    if(!flag && dp[pos][num][t][e]!=-1)
        return dp[pos][num][t][e];
    int end=flag?bit[pos]:9;
    int ans=0;
    for(int i=0;i<=end;i++)
        ans+=dfs(pos-1,(num*10+i)%13,t||(e==1&&i==3),i,flag&&(i==end));
    if(!flag)dp[pos][num][t][e]=ans;
    return ans;
}
int calc(int n)
{
    int pos=0;
    while(n)
    {
        bit[pos++]=n%10;
        n/=10;
    }
    return dfs(pos-1,0,0,0,1);
}
int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    int n;
    memset(dp,-1,sizeof(dp));
    while(scanf("%d",&n)==1)
    {
        printf("%d\n",calc(n));
    }
    return 0;
}


 

 

转载于:https://www.cnblogs.com/fht-litost/p/8973679.html

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/107552.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • xaxis python_Python Matplotlib.axes.Axes.invert_xaxis()用法及代码示例

    xaxis python_Python Matplotlib.axes.Axes.invert_xaxis()用法及代码示例Matplotlib是Python中的一个库,它是数字的-NumPy库的数学扩展。轴类包含大多数图形元素:Axis,Tick,Line2D,Text,Polygon等,并设置坐标系。Axes实例通过callbacks属性支持回调。matplotlib.axes.Axes.invert_xaxis()功能matplotlib库的axiss模块中的Axes.invert_xaxis()函数用于反转x…

    2025年6月11日
    2
  • Scala之隐式转换「建议收藏」

    Scala之隐式转换「建议收藏」概述简单说,隐式转换就是:当Scala编译器进行类型匹配时,如果找不到合适的候选,那么隐式转化提供了另外一种途径来告诉编译器如何将当前的类型转换成预期类型。隐式转换有四种常见的使用场景:将某一类型转换成预期类型类型增强与扩展模拟新的语法类型类语法隐式转换有新旧两种定义方法,旧的定义方法指是的“implictdef”形式,这是Scala2.10版本之前的写法,在Scala2.10版本之

    2022年10月11日
    3
  • Centos7关闭selinux命令「建议收藏」

    关闭selinux步骤0x01用vi修改selinux的配置文件vi/etc/selinux/config0x02修改#SELINUX=enforcing为SELINUX=disabled这里就不修改了,注释掉好了,再直接复制修改为SELINUX=disabledselinux的工作模式enforcing强制模式permissive宽容模式disabled关闭什么是selinux?selinux是Linux的一种安全子系统 Linux中的权限管..

    2022年4月18日
    230
  • android实现点餐功能「建议收藏」

    android实现点餐功能「建议收藏」近期项目中有点餐功能要求中午和晚上一起点餐,中午和晚上的餐品加载的都是一样的有炒菜和面点废话不多说直接上代码activity页面代码:publicclassAnimationActivityextendsBaseActivityimplementsView.OnClickListener{privateListViewrecyclerView;privateImag…

    2022年6月19日
    25
  • Android在API推荐的方式来实现SQLite数据库的增长、删除、变化、检查操作

    Android在API推荐的方式来实现SQLite数据库的增长、删除、变化、检查操作

    2022年1月1日
    54
  • oracle fetch into用法_基于绑定的数据库访问

    oracle fetch into用法_基于绑定的数据库访问protectedvoidrp1_ItemDataBound(objectsender,RepeaterItemEventArgse)       {             if(e.Item.ItemType==ListItemType.Item||e.Item.ItemType==ListItemType.AlternatingItem)          …

    2022年8月31日
    4

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号