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. 360doc的文章不能复制的解决办法

    按F12打开控制台,执行: document.body.oncopy=null;

  2. 帝国cms栏目自定义字段首页调用

    例如:增加栏目自定义字段:chushi_bpic 用下面的灵动标签和调用: [e:loop={"select C.classid,C.classname,C.classimg,D.chush ...

  3. [Android] HttpURLConnection & HttpClient & Socket

    Android的三种网络联接方式 1.标准Java接口:java.net.*提供相关的类//定义地址URL url = new URL("http://www.google.com" ...

  4. python zip()

    >>> help(zip) Help on built-in function zip in module __builtin__: zip(...) zip(seq1 [, seq ...

  5. [转]如何启用Ubuntu的休眠模式

    大家都知道 Windows 有休眠模式,其实 Ubuntu 也有.休眠模式简单来说,就是可以在用户暂时离开时将内存中的所有内容都写入到硬盘当中,当用户下次开机时,就可以直接启动到上次保存的时间状态. ...

  6. Nginx个人简单理解

    首先我们来补充下一些基本知识: 什么是代理服务器? 先举个简单的例子,现在我们在百度访问谷歌的网站,发现现在进不去,这个时候我们可以FQ(关于FQ,可以借鉴下这个博文:http://zhangge.n ...

  7. IBatis一对多查询

    public class User { public int UserId { get; set; } public string UserName { get; set; } } public cl ...

  8. 关于centos的yum代理设置

    10. 有代理服务器时使用 yum默认情况下,yum 通过 HTTP 连接到网络上的仓库.所有 yum HTTP 操作都使用 HTTP/1.1,与支持这一标准的 web 代理服务器兼容.您也可以连接到 ...

  9. PL/SQL 中查询CLOB字段内容

    oracle中的clob类型字段不能直接显示出来,需要借助Oracle系统dbms_lob中substr方法处理,如select dbms_lob.substr(clobField) from tab ...

  10. 9月23日JavaScript作业----用DIV做下拉列表

    例题二.用div做下拉列表 <title>无标题文档</title> <style type="text/css"> *{ margin:0px ...