题目链接:https://vjudge.net/problem/HDU-3652

B-number

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

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

题解:

求一段区间内既含有“13”也能被13整除的数的个数,数位DP。

1.dp[pos][status][num]:第pos位,状态为status(2为含有“13”, 1为不含有“13”但末尾为“1”, 0为什么都不满足),num为当前的数除以13的余数。

2.题目的其中一个要求是:该数能够被13整除,那么为什么在dp的数组中,只用记录:到当前位所形成的数除以13的余数呢?答:

比如 “14XXX” :

即当前位为4,上一位为1,后面位的数待定。所以到当前位为止,数字为14,那么14%13 = 1 。

由于当前位为第四位,即最小单元为1000,所以上面的 14%13 = 1 实际是: 14000 % 13000 = 1000。由于13000是13的倍数,故14000 除以 13, 至少能减少13000,剩下1000,这个1000只能到下一位才能继续处理了。也就是说: “14XXX” 和 “01XXX” 对于模13来说是等价的,因为“14XXX”可以减掉13000得到“01XXX”。

故:对于位置相同,且到当前位所形成的数字%13相等的状态,实际上可以归为一类。

代码如下:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int mod = 1e9+;
const int maxn = +; int a[maxn], dp[maxn][][]; int dfs(int pos, int status, int num, bool lim)
{
if(!pos) return (status==)&&(num==); //含有“13”,且能够被“13”整除
if(!lim && dp[pos][status][num]!=-) return dp[pos][status][num]; int ret = ;
int m = lim?a[pos]:;
for(int i = ; i<=m; i++)
{
int tmp_status;
if(status== || status== && i==) //含有“13”
tmp_status = ;
else if(i==) //前面的位不含有“13”, 但当前位(末尾)为“1"。(满足了一部分)
tmp_status = ;
else //前面的位不含有“13”,且当前位不是“1”。
tmp_status = ; // (num*10+i)%13:只需记录%13的余数,而不用记录完整的数
ret += dfs(pos-, tmp_status, (num*+i)%, lim&&(i==m));
} if(!lim) dp[pos][status][num] = ret;
return ret;
} int main()
{
int n;
memset(dp,-, sizeof(dp));
while(scanf("%d",&n)!=EOF)
{
int p = ;
while(n)
{
a[++p] = n%;
n /= ;
}
LL ans = dfs(p, , , );
printf("%d\n",ans);
}
}

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 5898 odd-even number 数位DP

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

  3. codeforces Hill Number 数位dp

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

  4. 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 ...

  5. Fzu2109 Mountain Number 数位dp

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

  6. HDU 3709 Balanced Number (数位DP)

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

  7. beautiful number 数位DP codeforces 55D

    题目链接: http://codeforces.com/problemset/problem/55/D 数位DP 题目描述: 一个数能被它每位上的数字整除(0除外),那么它就是beautiful nu ...

  8. FZU - 2109 Mountain Number 数位dp

    Mountain Number One integer number x is called "Mountain Number" if: (1) x>0 and x is a ...

  9. 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 ...

  10. hdu 5898 odd-even number(数位dp)

    Problem Description For a number,if the length of continuous odd digits is even and the length of co ...

随机推荐

  1. 互不侵犯King(bzoj 1087)

    Description 在N×N的棋盘里面放K个国王,使他们互不攻击,共有多少种摆放方案.国王能攻击到它上下左右,以及左上左下右上右下八个方向上附近的各一个格子,共8个格子. Input 只有一行,包 ...

  2. 【HDOJ5948】Thickest Burger(签到)

    题意:不知道 思路:队友写的 #include <stdio.h> #include <vector> #include <algorithm> #include ...

  3. oracle怎么查看表空间里有哪些表

    select TABLE_NAME,TABLESPACE_NAME from dba_tables where TABLESPACE_NAME='表空间名'; 注意:表空间名要大写

  4. csv文件导出

    参考博客:http://www.cnblogs.com/mingforyou/p/4103132.html 导入jar包javacsv.jar 链接:http://pan.baidu.com/s/1i ...

  5. 533. Lonely Pixel II

    Given a picture consisting of black and white pixels, and a positive integer N, find the number of b ...

  6. raspi扩展板

    1.Ciseco Slice 2.Adafruit(Plate) 3.Fen的Gertboard:(详见F盘下的使用手册) 12个缓冲输入输出端口 LED状态指示灯 3个按钮开关 6个开路集成电极 1 ...

  7. PostgreSQL SystemTap on Linux

    http://digoal126.wap.blog.163.com/w2/blogDetail.do;jsessionid=3949B03DE151DA0E55D807466C5E630B.yqblo ...

  8. SpringMVC拦截器详解[附带源码分析](转)

    本文转自http://www.cnblogs.com/fangjian0423/p/springMVC-interceptor.html 感谢作者 目录 前言 重要接口及类介绍 源码分析 拦截器的配置 ...

  9. 【lombok】使用lombok注解,在代码编写过程中可以调用到get/set方法,但是在编译的时候无法通过,提示找不到get/set方法

    错误如题:使用lombok注解,在代码编写过程中可以调用到get/set方法,但是在编译的时候无法通过,提示找不到get/set方法 报错如下: 解决方法: 1.首先查看你的lombok插件是否下载安 ...

  10. ETCD 单机安装

    由于测试的需要,有时需要搭建一个单机版的etcd 环境,为了方便以后搭建查看,现在对单机部署进行记录. 一.部署单机etcd 下载 指定版本的etcd下载地址 ftp://ftp.pbone.net/ ...