B-number

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

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
 

Source
 

Recommend
lcy
 

加一个参数记录%13的余数。。。。newres=(res×10+i)%13

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

int dp[20][20][3],n,bit[20],len;
/*
    dp『位』『余数』『状态』
    0--->没有13
    1--->没有13但是前面一位是1
    2--->有13
*/
int dfs(int pos,int res,int s,bool limit)
{
    if(pos==-1)
        return (s==2)&&(res==0);
    if(!limit&&~dp[pos][res][s])
        return dp[pos][res][s];
    int end=limit?bit[pos]:9;
    int ans=0;
    for(int i=0;i<=end;i++)
    {
        int newres=(res*10+i)%13;
        int news=s;
        if(s==0&&i==1)
            news=1;
        else if(s==1&&i==3)
            news=2;
        else if(s==1&&i==1)
            news=1;
        else if(s==1&&i!=1)
            news=0;
        ans+=dfs(pos-1,newres,news,limit&&i==end);
    }
    if(!limit)
        dp[pos][res][s]=ans;
    return ans;
}

int main()
{
    memset(dp,-1,sizeof(dp));
    while(scanf("%d",&n)!=EOF)
    {
        len=0;
        while(n)
        {
            bit[len++]=n%10;
            n/=10;
        }
        printf("%d\n",dfs(len-1,0,0,true));
    }
    return 0;
}

* This source code was highlighted by YcdoiT. ( style: Pastie )

HDOJ 3652 B-number的更多相关文章

  1. 水题 HDOJ 4727 The Number Off of FFF

    题目传送门 /* 水题:判断前后的差值是否为1,b[i]记录差值,若没有找到,则是第一个出错 */ #include <cstdio> #include <iostream> ...

  2. 【HDOJ 3652】B-number

    [HDOJ 3652]B-number 给一整数n 找<=n的整数中能被13整除且含有13的 数位dp 记忆化! . 一入记忆化深似海. ..再也不想用递推了...发现真的非常好想 仅仅要保证满 ...

  3. HDOJ 3709 Balanced Number

    数位DP... Balanced Number Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java ...

  4. 如何运用同余定理求余数【hdoj 1212 Big Number【大数求余数】】

    Big Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  5. HDOJ 1018 Big Number(大数位数公式)

    Problem Description In many applications very large integers numbers are required. Some of these app ...

  6. HDOJ 1266 Reverse Number(数字反向输出题)

    Problem Description Welcome to 2006'4 computer college programming contest! Specially, I give my bes ...

  7. HDOJ 4937 Lucky Number

    当进制转换后所剩下的为数较少时(2位.3位),相应的base都比較大.能够用数学的方法计算出来. 预处理掉转换后位数为3位后,base就小于n的3次方了,能够暴力计算. . .. Lucky Numb ...

  8. HDOJ 2665 Kth number

    静态区间第K小....划分树裸题 Kth number Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K ( ...

  9. HDOJ 1755 - A Number Puzzle 排列数字凑同余,状态压缩DP

    dp [ x ] [ y ] [ z ] 表示二进制y所表示的组合对应的之和mod x余数为z的最小数... 如可用的数字为 1 2 3 4...那么 dp [ 7 ] [ 15 ] [ 2 ] = ...

随机推荐

  1. C#产生随机颜色

    在.net Framework中提供了一个专门用来产生随机数的类System.Random.C#可以用Random产生随机的R.G.B值,从而生成随机的颜色. 对于随机数,计算机不可能产生完全随机的数 ...

  2. MyBatis详解 与配置MyBatis+Spring+MySql

    MyBatis 是一个可以自定义SQL.存储过程和高级映射的持久层框架.MyBatis 摒除了大部分的JDBC代码.手工设置参数和结果集重获.MyBatis 只使用简单的XML 和注解来配置和映射基本 ...

  3. PhyLab2.0需求与功能分析改进文档(NABCD)

    PhyLab1.0需求规格说明文档 1. 概述 1.1 项目概述 软剑攻城队小组于2015学年开发了PhyLab物理实验网站,一经发布好评如潮.网站的核心功能是提供预习报告和自动数据处理,而后加入了论 ...

  4. primefaces 上传文件尺寸受限制 Connection terminated as request was larger than

    standalone.xml like this: <http-listener name="default" socket-binding="http" ...

  5. iOS - Availability.h

    >for 'dispatch' application inner to begin note `#include <Availability.h>` These macros ar ...

  6. 通过rsync搭建一个远程备份系统(二)

    Rsync+inotify实时备份数据 rsync在同步数据的时候,需要扫描所有文件后进行对比,然后进行差量传输,如果文件达到了百万或者千万级别以上是,扫描文件的时间也很长,而如果只有少量的文件变更了 ...

  7. mysql php query steps

    1.mysql connect $db=new mysqli ('localhost','database','user','password'); 2. query $query=select id ...

  8. vmware下linux系统的安装过程

    虚拟机VMware下CentOS6.6安装教程图文详解 [日期:2016-05-24] 来源:Linux社区  作者:Sungeek [字体:大 中 小]   分享下,虚拟机VMware下CentOS ...

  9. 将文件路径以"\"隔开

    将文件路径以"\"隔开,这货搞了我一小时...C++返回一维数组,字符串数组还是要再看看 ]) { ; //string s_array[30]; //局部变量,如果使用retur ...

  10. IBatis一对多嵌套查询

    1)类 public class AppData { // public int ModuleId { get; set; } public int DataId { get; set; } publ ...