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 )

最新文章

  1. [ 高并发]Java高并发编程系列第二篇--线程同步
  2. ThinkPHP框架之验证码
  3. 更改ubuntu多系统启动顺序
  4. ROS学习笔记(六)——创建、编译包
  5. JVM之数据类型
  6. Redis(7)Creating and Using Cluster Mode
  7. Machine Learning for hackers读书笔记(五)回归模型:预测网页访问量
  8. Cross Site Request Forgery (CSRF)--spring security -转
  9. CM3存储器系统
  10. Hadoop在Windows下的安装配置
  11. Ext Js详解指南
  12. jQuery中的$.ajax()方法
  13. 利用Python进行数据分析——Numpy基础:数组和矢量计算
  14. MySQL 数据库字符集 utf8 和 utf8mb4 的区别
  15. 7——ThinkPhp中的响应和重定向:
  16. LITTLE-ENDIAN(小字节序、低字节序) BOM——Byte Order Mark 字节序标记 数据在内存中的存放顺序
  17. OO Summary Ⅱ
  18. Redis-audit工具使用(转)
  19. Eclipse初次java开发问题总结-2
  20. 关于Cocos2d-x中字体的使用

热门文章

  1. CSS基础知识真难啊-background-渐变
  2. iOS WebView调用JS的一个小坑
  3. PHP设计模式(一)
  4. 加州大学伯克利分校Stat2.2x Probability 概率初步学习笔记: Section 1 The Two Fundamental Rules (1.5-1.6)
  5. 【Alpha阶段】第十次Scrum例会
  6. Python基本数据类型之set
  7. 利用 Django REST framework 编写 RESTful API
  8. IP地址、子网掩码、网关、DNS的关系
  9. Objective-C学习笔记类目、协议
  10. js日期计算及快速获取周、月、季度起止日,获取指定日期周数以及星期几的小例子