POJ - Problem 2282 - The Counting Problem
整体思路:对于每一位,先将当前未达到$limit$部分的段 [如 $0$ ~ $10000$] 直接处理好,到下一位时再处理达到$limit$的部分。
· $1 × 10 ^ n$以内每个数(包括$0$)的出现次数的计算 [即已知$bitnum[n - 1]$,求$bitnum[n]$]: 将$bitnum[n - 1]$乘以$10$,代表$n - 1$处最为每个小段在$n$处出现$10$次,再加上$power10[n - 1]$,即加上新增的最高位该数出现次数。
· $1 × 10 ^ n$以内$0$的出现次数的计算 [即已知$bitzero[n - 1]$,求$bitzero[n]$]: 将$bitzero[n - 1] + power10[n - 2] - 1$ ··· ①,表示在$n - 1$的所有数($power10[n - 2] - 1$)个前都加上一个前导零,再将① $* 10$,表示该小段出现$10$次。
· 处理$limit$位: 首先保存前$n + 1$位的$limit$位,那么前$n + 1$位的$limit$为在当前第$n$位的贡献即它们在$0$ ~ $limit × 10 ^ n$中每一大段停留的次数,即 [它们的出现次数 × $power10[n - 1]$]。
· 处理非$limit$位: 直接加上$bitnum$或$bitzero$(前导零情况)即可。
· 代码:
#include <iostream>
#include <cstdio>
#include <cstring> using namespace std; typedef long long LL; const int MAXN = + ;
const int MAXM = ; LL power10[MAXN]; LL bitnum[MAXN];
LL bitzero[MAXN]; void Preparation () {
power10[] = ;
for (int i = ; i <= MAXM; i ++)
power10[i] = power10[i - ] * ; bitnum[] = ;
for (int i = ; i <= MAXM; i ++)
bitnum[i] = bitnum[i - ] * + power10[i - ]; bitzero[] = , bitzero[] = ;
for (int i = ; i <= MAXM; i ++)
bitzero[i] = (bitzero[i - ] + power10[i - ] - ) * ;
} LL Answers[][MAXM]; int L, R; int bit[MAXN];
int N; int cobit[MAXN]; void separ (int p) {
memset (cobit, , sizeof (cobit)); if (! p)
cobit[] = ;
while (p) {
cobit[p % ] ++; p /= ;
}
} void Solve (int M, int type) {
if (M <= ) {
if (M == )
Answers[type][] = ; return ;
} int tmpM = M; N = ;
while (tmpM) {
bit[++ N] = tmpM % ; tmpM /= ;
} int bits = ; for (int i = N; i >= ; i --) {
bits *= ; for (int j = ; j < bit[i]; j ++) {
if (bits + j || i == ) { // 处理limit位
separ (bits + j); for (int k = ; k <= ; k ++)
Answers[type][k] += (LL) cobit[k] * power10[i - ];
} for (int k = ; k <= ; k ++)
Answers[type][k] += bitnum[i - ];
Answers[type][] += (bits + j ? bitnum[i - ] : bitzero[i - ]);
} bits += bit[i];
} separ (M); for (int k = ; k <= ; k ++)
Answers[type][k] += cobit[k];
} int main () {
Preparation (); while (~ scanf ("%d%d", & L, & R) && L + R) {
if (L > R)
swap (L, R); memset (Answers, , sizeof (Answers)); Solve (L - , );
Solve (R, ); for (int i = ; i <= ; i ++) {
if (i)
putchar (' '); printf ("%lld", Answers[][i] - Answers[][i]);
} puts ("");
} return ;
} /*
1 10
44 497
346 542
1199 1748
1496 1403
1004 503
1714 190
1317 854
1976 494
1001 1960
0 0
*/
POJ - Problem 2282 - The Counting Problem的更多相关文章
- [POJ 2282] The Counting Problem
[题目链接] http://poj.org/problem?id=2282 [算法] 数位DP [代码] #include <algorithm> #include <bitset& ...
- (Problem 73)Counting fractions in a range
Consider the fraction, n/d, where n and d are positive integers. If nd and HCF(n,d)=1, it is called ...
- (Problem 72)Counting fractions
Consider the fraction, n/d, where n and d are positive integers. If nd and HCF(n,d)=1, it is called ...
- UVA 1640 The Counting Problem UVA1640 求[a,b]或者[b,a]区间内0~9在里面各个数的数位上出现的总次数。
/** 题目:UVA 1640 The Counting Problem UVA1640 链接:https://vjudge.net/problem/UVA-1640 题意:求[a,b]或者[b,a] ...
- foj Problem 2282 Wand
Problem 2282 Wand Accept: 432 Submit: 1537Time Limit: 1000 mSec Memory Limit : 262144 KB Prob ...
- 尺取法 POJ 3320 Jessica's Reading Problem
题目传送门 /* 尺取法:先求出不同知识点的总个数tot,然后以获得知识点的个数作为界限, 更新最小值 */ #include <cstdio> #include <cmath> ...
- POJ 3100 Root of the Problem || 1004 Financial Management 洪水!!!
水两发去建模,晚饭吃跟没吃似的,吃完没感觉啊. ---------------------------分割线"水过....."--------------------------- ...
- 『The Counting Problem 数位dp』
The Counting Problem Description 求 [L,R]内每个数码出现的次数. Input Format 若干行,一行两个正整数 L 和 R. 最后一行 L=R=0,表示输入结 ...
- POJ2282 The Counting Problem
题意 Language:DefaultEspañol The Counting Problem Time Limit: 3000MS Memory Limit: 65536K Total Submis ...
随机推荐
- 【bzoj5197】[CERC2017]Gambling Guide 期望dp+堆优化Dijkstra
题目描述 给定一张n个点,m条双向边的无向图. 你要从1号点走到n号点.当你位于x点时,你需要花1元钱,等概率随机地买到与x相邻的一个点的票,只有通过票才能走到其它点. 每当完成一次交易时,你可以选择 ...
- 解数独(Python)
0.目录 1.介绍 2.一些通用函数 3.全局变量(宏变量) 4.数独预处理(约束传播) 5.解数独(深度优先搜索+最小代价优先) 6.主函数 7.总代码 1.介绍 数独是一个非常有趣味性的智力游戏, ...
- 【BZOJ4889】不勤劳的图书管理员(树套树)
[BZOJ4889]不勤劳的图书管理员(树套树) 题面 又是权限题,烦死了 洛谷真好 题解 分开考虑每一次交换产生的贡献. 假设交换\((x,y)\) 检查\(x\)与\(y\)对于区间\([x+1, ...
- BZOJ2331:[SCOI2011]地板——题解
http://www.lydsy.com/JudgeOnline/problem.php?id=2331 题面复制于洛谷 题目描述 lxhgww的小名叫”小L“,这是因为他总是很喜欢L型的东西.小L家 ...
- Combining HTML5 Web Applications with OpenCV
The Web Dev Zone is brought to you by Stormpath—offering a pre-built Identity API for developers. Ea ...
- Hystrix : 解决请求会被拒绝和抛出异常并且fallback不会被调用的问题
启动脚本增加参数:-Dserver.tomcat.max-http-header-size=102400 增大Hystrix,ribbon的各项参数; hystrix: threadpool: def ...
- vue2.0 安装及项目搭建(一)
基本环境安装 1.安装node:从node.js官网下载并安装node.测试:win+R(打开命令行)-------输入cmd-------敲入node -v.如果出现相应版本号,即安装成功: 2.测 ...
- Hdu1255 覆盖的面积
覆盖的面积 Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...
- Centos7单机部署ELK
一. 简介 1.1 介绍 ELK是三个开源工具组成,简单解释如下: Elasticsearch是个开源分布式搜索引擎,它的特点有:分布式,零配置,自动发现,索引自动分片,索引副本机制,restful风 ...
- linux 内存计算
原文: http://www.open-open.com/lib/view/open1424325362577.html Linux中的Cache Memory 什么是Cache Memory(缓存内 ...