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.

InputProcess till EOF. In each line, there is one positive integer n(1 <= n <= 1000000000).OutputPrint each answer in a single line.Sample Input

13
100
200
1000

Sample Output

1
1
2
2

心里没有点13数吗,233?

第一次提交代码:

#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
#define LL long long
const int N=;
int dp[N][N][][][],n;
int a[N],cnt;
void _divide(LL v){
cnt=;
while(v){a[++cnt]=v%;v/=;}
}
int _dfs(int pos,int Mod,bool limit,bool pre,bool stat)
{
if(pos==) return stat&&!Mod;
int tmp=;
if(!limit&&dp[pos][Mod][limit][pre][stat]) return dp[pos][Mod][limit][pre][stat];
int Up=limit?a[pos]:;
for(int i=;i<=Up;i++)
tmp+=_dfs(pos-,(Mod*+i)%,limit&&i==Up,i==,stat||(pre&&i==));
dp[pos][Mod][limit][pre][stat]=tmp;
return tmp;
}
int main()
{
int i,T;
while(~scanf("%d",&n)){
memset(dp,,sizeof(dp));
_divide(n);
printf("%d\n",_dfs(cnt,,true,false,false));
}
return ;
}

 时间长,是因为memset次数太多。

优化:去掉memset,加上limit限制

第二次提交代码:

#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
#define LL long long
const int N=;
int dp[N][N][][][],n;
int a[N],cnt;
void _divide(LL v){
cnt=;
while(v){a[++cnt]=v%;v/=;}
}
int _dfs(int pos,int Mod,bool limit,bool pre,bool stat)
{
if(pos==) return stat&&!Mod;
int tmp=;
if(!limit&&dp[pos][Mod][limit][pre][stat]) return dp[pos][Mod][limit][pre][stat];
int Up=limit?a[pos]:;
for(int i=;i<=Up;i++)
tmp+=_dfs(pos-,(Mod*+i)%,limit&&i==Up,i==,stat||(pre&&i==));
dp[pos][Mod][limit][pre][stat]=tmp;
return tmp;
}
int main()
{
int i,T;
while(~scanf("%d",&n)){
_divide(n);
printf("%d\n",_dfs(cnt,,true,false,false));
}
return ;
}

HDU3652 B-number 数位DP第二题的更多相关文章

  1. 多校5 HDU5787 K-wolf Number 数位DP

    // 多校5 HDU5787 K-wolf Number 数位DP // dp[pos][a][b][c][d][f] 当前在pos,前四个数分别是a b c d // f 用作标记,当现在枚举的数小 ...

  2. HDU 2089 不要62(数位dp模板题)

    http://acm.hdu.edu.cn/showproblem.php?pid=2089 题意:求区间内不包含4和连续62的数的个数. 思路: 简单的数位dp模板题.给大家推荐一个好的讲解博客.h ...

  3. HDU 3709 Balanced Number (数位DP)

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

  4. BNU 13024 . Fi Binary Number 数位dp/fibonacci数列

    B. Fi Binary Number     A Fi-binary number is a number that contains only 0 and 1. It does not conta ...

  5. hdu 5898 odd-even number 数位DP

    传送门:hdu 5898 odd-even number 思路:数位DP,套着数位DP的模板搞一发就可以了不过要注意前导0的处理,dp[pos][pre][status][ze] pos:当前处理的位 ...

  6. codeforces Hill Number 数位dp

    http://www.codeforces.com/gym/100827/attachments Hill Number Time Limits:  5000 MS   Memory Limits: ...

  7. HDU 5787 K-wolf Number 数位DP

    K-wolf Number Problem Description   Alice thinks an integer x is a K-wolf number, if every K adjacen ...

  8. Fzu2109 Mountain Number 数位dp

    Accept: 189    Submit: 461Time Limit: 1000 mSec    Memory Limit : 32768 KB  Problem Description One ...

  9. HDU5787 K-wolf Number 数位dp

    分析:赛场上也知道是裸的数位dp,但是无奈刷数位dp题刷的太少了,并不能写出来 一点感想:赛后补题,看了题解的map记录状态,一脸蒙逼,也是非常的不爽,然后想看别人写的,不是递归就是写的比较乱 而且我 ...

随机推荐

  1. 数据结构实习 - problem K 用前序中序建立二叉树并以层序遍历和后序遍历输出

    用前序中序建立二叉树并以层序遍历和后序遍历输出 writer:pprp 实现过程主要是通过递归,进行分解得到结果 代码如下: #include <iostream> #include &l ...

  2. wireshark抓包分析

    TCP协议首部: 分析第一个包: 源地址:我自己电脑的IP,就不放上来了 Destination: 222.199.191.33 目的地址 TCP:表明是个TCP协议 Length:66 表明包的长度 ...

  3. vc 导出函数/调用

    loader(exe): #include "stdafx.h" #include <Windows.h> #include <stdio.h> #defi ...

  4. 数据可视化——matplotlib(2)

    导入相关模块 import matplotlib.pyplot as plt import numpy as np import pandas as pd 图表设置 添加X.Y轴标签以及图标标题 a ...

  5. 互联网公司面试必问的Redis题目

    Redis是一个非常火的非关系型数据库,火到什么程度呢?只要是一个互联网公司都会使用到.Redis相关的问题可以说是面试必问的,下面我从个人当面试官的经验,总结几个必须要掌握的知识点. 介绍:Redi ...

  6. HTML5如何做横屏适配

    在移动端中我们经常碰到横屏竖屏的问题,那么我们应该如何去判断或者针对横屏.竖屏来写不同的代码呢. 首先在head中加入如下代码: 1 <meta name="viewport" ...

  7. panda 函数-处理空值

    今天这里谈的函数,以后进行数据分析的时候会经常用到. import numpy as npimport pandas as pdfrom pandas import DataFrame , Serie ...

  8. 使用Bind读取配置到C#的实例

    在之前的一篇二级域名绑定的文章<.Net Core 二级域名绑定到指定的控制器>中,有一个小的地方是关于读取Json文件的配置信息的,当时是用了读取文件流的方式,一直以来觉得该方法太Low ...

  9. [转]基于Visual Studio 2010 进行敏捷/Scrum模式开发

    http://www.infoq.com/cn/articles/visual-studio-2010-agile-scrum-development 根据Forrester Research今年第二 ...

  10. 学习opencv(持续更新)

    redhat安装,报错解决方法 1 升级GCC,http://mirrors.kernel.org/gnu/gcc/ 2 更换稳定版本 #!/bin/bash yum -y install gcc g ...