题目大意:求区间$[l,r]$中数字$0\sim9$出现个数

题解:数位$DP$

卡点:

C++ Code:

#include <cstdio>
#include <iostream>
struct node {
long long s[10], sum;
inline node() {for (register int i = 0; i < 10; i++) s[i] = 0; sum = 0;}
inline node(int x) {for (register int i = 0; i < 10; i++) s[i] = 0; sum = x;}
inline friend node operator + (const node &lhs, const node &rhs) {
node res;
res.sum = lhs.sum + rhs.sum;
for (register int i = 0; i < 10; i++) res.s[i] = lhs.s[i] + rhs.s[i];
return res;
}
inline friend node operator - (const node &lhs, const node &rhs) {
node res;
res.sum = lhs.sum - rhs.sum;
for (register int i = 0; i < 10; i++) res.s[i] = lhs.s[i] - rhs.s[i];
return res;
}
inline friend std::ostream & operator << (std::ostream &Fout, const node __node) {
for (register int i = 0; i < 10; i++) {
Fout << __node.s[i];
Fout << (i == 9 ? '\n' : ' ');
}
return Fout;
}
}; int num[20], tot;
node f[20];
bool vis[20];
node calc(int x, int lim, int lead) {
if (!x) return node(1);
if (!lim && lead && vis[x]) return f[x];
node F;
for (int i = lim ? num[x] : 9, op = 1; ~i; i--, op = 0) {
node tmp = calc(x - 1, lim && op, lead || i);
F = F + tmp;
if (i || lead) F.s[i] += tmp.sum;
}
if (!lim && lead) f[x] = F, vis[x] = true;
return F;
}
node solve(long long x) {
if (x < 0) return node();
tot = 0;
while (x) {
num[++tot] = x % 10;
x /= 10;
}
return calc(tot, 1, 0);
}
long long l, r;
int main() {
std::ios::sync_with_stdio(false), std::cin.tie(0), std::cout.tie(0);
std::cin >> l >> r;
std::cout << solve(r) - solve(l - 1) << std::endl;
return 0;
}

  

[洛谷P2602][ZJOI2010]数字计数的更多相关文章

  1. 洛谷P2602 [ZJOI2010]数字计数 题解 数位DP

    题目链接:https://www.luogu.com.cn/problem/P2602 题目大意: 计算区间 \([L,R]\) 范围内 \(0 \sim 9\) 各出现了多少次? 解题思路: 使用 ...

  2. 洛谷 P2602 [ZJOI2010]数字计数

    洛谷 第一次找规律A了一道紫题,写篇博客纪念一下. 这题很明显是数位dp,但是身为蒟蒻我不会呀,于是就像分块打表水过去. 数据范围是\(10^{12}\),我就\(10^6\)一百万一百万的打表. 于 ...

  3. 洛谷P2602 [ZJOI2010]数字计数(数位dp)

    数字计数 题目传送门 解题思路 用\(dp[i][j][k]\)来表示长度为\(i\)且以\(j\)为开头的数里\(k\)出现的次数. 则转移方程式为:\(dp[i][j][k] += \sum_{t ...

  4. 洛谷P2602 [ZJOI2010]数字计数 题解

    题目描述 输入格式 输出格式 输入输出样例 输入样例 1 99 输出样例 9 20 20 20 20 20 20 20 20 20 说明/提示 数据规模与约定 分析 很裸的一道数位DP的板子 定义f[ ...

  5. 洛谷P2602 [ZJOI2010] 数字计数 (数位DP)

    白嫖的一道省选题...... 1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 usin ...

  6. BZOJ1833或洛谷2602 [ZJOI2010]数字计数

    BZOJ原题链接 洛谷原题链接 又是套记搜模板的时候.. 对\(0\sim 9\)单独统计. 定义\(f[pos][sum]\),即枚举到第\(pos\)位,前面枚举的所有位上是当前要统计的数的个数之 ...

  7. 【洛谷P2602】数字计数

    题目大意:求 [a,b] 中 0-9 分别出现了多少次. 题解:看数据范围应该是一个数位dp. 在 dfs 框架中维护当前的位置和到当前位置一共出现了多少个 \(x,x\in [0,9]\).因此,用 ...

  8. P2602 [ZJOI2010]数字计数(递推)

    P2602 [ZJOI2010]数字计数 思路: 首先考虑含有前导0的情况,可以发现在相同的\(i\)位数中,每个数的出现次数都是相等的.所以我们可以设\(f(i)\)为\(i\)位数每个数的出现次数 ...

  9. P2602 [ZJOI2010]数字计数&P1239 计数器&P4999 烦人的数学作业

    P2602 [ZJOI2010]数字计数 题解 DFS 恶心的数位DP 对于这道题,我们可以一个数字一个数字的求 也就是分别统计区间 [ L , R ] 内部数字 i 出现的次数 (0<=i&l ...

随机推荐

  1. css实现未知元素宽高垂直居中和水平居中的方法

    第一种:display:table-cell的方式 .container { /*父级容器*/ display:table-cell; text-align:center; vertical-alig ...

  2. centos安装zabbix(server+agent)

    本文包含zabbix_server编译安装,zabbix_agent编译安装,中文字体修正 Mysql模板监控,Nginx模板监控,以及简单的web页面的使用 中文乱码的解决方案 zabbix乱码是字 ...

  3. STL——list

    1.关键概述 list 是定义在 namespace::std 的模板,声明在 <list> ,存储结构是 双向链表, 提供的 正向和反向迭代器. 2.构造list对象 list<i ...

  4. COURSES POJ1469(模板)

    Description Consider a group of N students and P courses. Each student visits zero, one or more than ...

  5. linux io 学习笔记(01)---锁,信号量

    1.采用信号量访问:当有段临界代码,需要保证排他的访问一个资源. 2.sudo  dmesg -c 消除dmesg缓冲 3.互斥锁:代表的是一种锁资源,互斥锁的工作原理是:保证对共享资源操作的原子性 ...

  6. EIP权限工作流平台总结-1总体说明

      预览地址:www.eipflow.com (1) 权限工作流:www.demo.eipflow.com/Account/Login (2) 基础权限版:www.auth.eipflow.com/A ...

  7. ActiveMQ测试实例

    ActiveMQ的安装与启动 1 下载ActiveMQ:http://activemq.apache.org/download.html 2 下载后解压到任意文件夹,解压后文件夹内的目录为: 3 进入 ...

  8. Java - 问题集 - linux下,jar: command not found

    linux下的找不到jar命令解决方法如下: 1. 确认jdk是否已安装 2. 检查jdk环境变量是否已设置,并且确认该设置已生效 3. 1,2两步均正常时,建立jar的软链接 # cd /usr/b ...

  9. Linux 下 PHP 扩展Soap 编译安装

    1.进入 PHP 的软件包 pdo 扩展目录中(注:不是 PHP 安装目录) [root@tester /]# /home/tdweb/php-5.4.34/ext/soap 执行 phpize 命令 ...

  10. leetcode笔记--3 Niim game

    question: You are playing the following Nim Game with your friend: There is a heap of stones on the ...