题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3652

题目大意:

求区间 \([1, n]\) 范围内包含连续的数位“13”并且能被13整数的数的数量。

解题思路:

使用 数位DP 进行求解。

开一个状态 \(f[pos][pre][num][flag]\) ,用于表示:

  • 当前所处的数位为第 pos 位;
  • 之前所有的数位模13的余数为 pre
  • 当前数位的前一位(即第 pos+1 位)为 num
  • flag 用于表示当前数位的前面那些位中是否有连续的两位为‘13’(flag==true表示有过,flag==false表示还没有过)

时的方案总数。

函数 dfs(int pos, int pre, int num, int flag, bool limit) 用于求解答案,其中:

  • posprenumflag 的含义同上;
  • limit 用于表示当前是否处于限制状态。

实现代码如下:

#include <bits/stdc++.h>
using namespace std;
int f[33][13][10][2], a[33];
void init() {
memset(f, -1, sizeof(f));
}
int dfs(int pos, int pre, int num, int flag, bool limit) {
if (pos < 0) return flag && pre==0;
if (!limit && f[pos][pre][num][flag] != -1) return f[pos][pre][num][flag];
int up = limit ? a[pos] : 9;
int tmp = 0;
for (int i = 0; i <= up; i ++) {
tmp += dfs(pos-1, (pre*10+i)%13, i, flag || num==1&&i==3, limit && i==up);
}
if (!limit) f[pos][pre][num][flag] = tmp;
return tmp;
}
int get_num(int x) {
int pos = 0;
while (x) {
a[pos++] = x % 10;
x /= 10;
}
return dfs(pos-1, 0, 0, 0, true);
}
int main() {
init();
int n;
while (cin >> n) {
cout << get_num(n) << endl;
}
return 0;
}

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

  1. HDU3709 Balanced Number 题解 数位DP

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3709 题目大意: 求区间 \([x, y]\) 范围内"平衡数"的数量. 所谓平衡 ...

  2. HDU5179 beautiful number 题解 数位DP

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5179 题目大意: 给你一个数 \(A = a_1a_2 \cdots a_n\) ,我们称 \(A\) ...

  3. HDU 3565 Bi-peak Number(数位DP)题解

    题意:我们定义每一位先严格递增(第一位不为0)后严格递减的数为峰(比如1231),一个数由两个峰组成称为双峰,一个双峰的价值为每一位位数和,问L~R双峰最大价值 思路:数位DP.显然这个问题和pos有 ...

  4. HDU 3709 Balanced Number(数位DP)题解

    思路: 之前想直接开左右两边的数结果爆内存... 枚举每次pivot的位置,然后数位DP,如果sum<0返回0,因为已经小于零说明已经到了pivot右边,继续dfs只会越来越小,且dp数组会炸 ...

  5. HDU 5787 K-wolf Number (数位DP)

    K-wolf Number 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5787 Description Alice thinks an integ ...

  6. HDU 5179 beautiful number (数位dp / 暴力打表 / dfs)

    beautiful number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  7. HDU3709 Balanced Number (数位dp)

     Balanced Number Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Descript ...

  8. fzu2109--Mountain Number(数位dp)

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

  9. 【HDU 3709】 Balanced Number (数位DP)

    Balanced Number Problem Description A balanced number is a non-negative integer that can be balanced ...

随机推荐

  1. oracle函数 UPPER(c1)

    [功能]将字符串全部转为大写 [参数]c1,字符表达式 [返回]字符型 [示例] SQL> select upper('AaBbCcDd') upper from dual; UPPER --- ...

  2. python selenium 获取对象输入的属性值

    .get_attribute("value") from selenium import webdriver import time driver=webdriver.Firefo ...

  3. 图表echarts折线图,柱状图,饼状图

    总体就是有折线图相关图标的设置,x,y轴的设置,x,y轴或者数据加上单位的设置.饼状图如何默认显示几个数据中的某个数据 折线图:legend(小标题)中间默认是圆圈 改变成直线 在legend设置的时 ...

  4. poj 3384 Feng Shui (Half Plane Intersection)

    3384 -- Feng Shui 构造半平面交,然后求凸包上最远点对. 这题的题意是给出一个凸多边形区域,要求在其中放置两个半径为r的圆(不能超出凸多边形区域),要求求出两个圆心,使得多边形中没有被 ...

  5. python 字符串方法isdigit()

    python isdigit() 方法检测字符串是否只有数字组成. 语法: isdigit()方法语法: str.isdigit() 参数:无 返回值: 如果字符串中只含有数字则返回True,否则返回 ...

  6. (超级详细版)利用ThinkPHP3.2.3+PHPExcel实现将表格数据导入到数据库

    请先阅读以下步骤再到结尾下载源码 第一步:下载 thinkphp_3.2.3 和 PHPExcel_1.8.0 并解压 对应的网站分别为: http://www.thinkphp.cn/down.ht ...

  7. linux readv 和 writev

    Unix 系统已经长时间支持名为 readv 和 writev 的 2 个系统调用. 这些 read 和 write 的"矢量"版本使用一个结构数组, 每个包含一个缓存的指针和一个 ...

  8. PHP mysql扩展整理,操作数据库的实现过程分析

    相关文章:PHP mysqli扩展整理,包括面向过程和面向对象的比较\事务控制\批量执行\预处理   PHPmysqli扩展整理,包括面向过程和面向对象的比较\事务控制\批量执行\预处理 从某种程度上 ...

  9. artDialog4.0.5

    artDialog4.0.5 2011-08-22 11:54:36 haiwei_sun 阅读数 4109  收藏 更多 分类专栏: jquery   下载 Google Code 项目主页 最新版 ...

  10. HDU 1711 Number Sequence (KMP 入门)

    Number Sequence Problem Description Given two sequences of numbers : a[1], a[2], ...... , a[N], and ...