整体思路:对于每一位,先将当前未达到$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的更多相关文章

  1. [POJ 2282] The Counting Problem

    [题目链接] http://poj.org/problem?id=2282 [算法] 数位DP [代码] #include <algorithm> #include <bitset& ...

  2. (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 ...

  3. (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 ...

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

  5. foj Problem 2282 Wand

     Problem 2282 Wand Accept: 432    Submit: 1537Time Limit: 1000 mSec    Memory Limit : 262144 KB Prob ...

  6. 尺取法 POJ 3320 Jessica's Reading Problem

    题目传送门 /* 尺取法:先求出不同知识点的总个数tot,然后以获得知识点的个数作为界限, 更新最小值 */ #include <cstdio> #include <cmath> ...

  7. POJ 3100 Root of the Problem || 1004 Financial Management 洪水!!!

    水两发去建模,晚饭吃跟没吃似的,吃完没感觉啊. ---------------------------分割线"水过....."--------------------------- ...

  8. 『The Counting Problem 数位dp』

    The Counting Problem Description 求 [L,R]内每个数码出现的次数. Input Format 若干行,一行两个正整数 L 和 R. 最后一行 L=R=0,表示输入结 ...

  9. POJ2282 The Counting Problem

    题意 Language:DefaultEspañol The Counting Problem Time Limit: 3000MS Memory Limit: 65536K Total Submis ...

随机推荐

  1. BZOJ 1066:[SCOI2007]蜥蜴(最大流)

    蜥蜴Description在一个r行c列的网格地图中有一些高度不同的石柱,一些石柱上站着一些蜥蜴,你的任务是让尽量多的蜥蜴逃到边界外. 每行每列中相邻石柱的距离为1,蜥蜴的跳跃距离是d,即蜥蜴可以跳到 ...

  2. MariaDB插入中文出现???情况

    本来打算创建一个测试表进行一个简单的实验,发现创建完python_test表后插入数据后,select发现所有中文都变成问号了,这一看就是出现了乱码 MariaDB [lhc]> create ...

  3. 创建Qt项目

    创建Qt项目 1     创建Qt项目 2.1 使用向导创建 打开Qt Creator 界面选择 New Project或者选择菜单栏 [文件]-[新建文件或项目]菜单项 弹出New Project对 ...

  4. 【BZOJ2306】幸福路径(动态规划,倍增)

    [BZOJ2306]幸福路径(动态规划,倍增) 题面 BZOJ 题解 不要求确切的值,只需要逼近 显然可以通过移动\(\infty\)步来达到逼近的效果 考虑每次的一步怎么移动 设\(f[i][j]\ ...

  5. ZJOI 2017 二试 day1 4.26

    day0,11:30熄灯,又因为在房间里太浪,空调开了28度,过了好久才成功降温,导致睡得不太好QaQ. 于是早上昏昏欲睡,也没怎么听懂(orz孙耀峰). 中午大家一致提议下午不去听课,回到房间浪了好 ...

  6. 【NOIP考试范围】

    ※号为可能考察的算法[历年有出现过,但概率小,但最好掌握] [本图片仅作参考] 梦想总是要有的,万一实现了呢?

  7. mac pro电脑怎么安装rabbitmq

    第一:依次执行以下命令: 1.  /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/inst ...

  8. Python基础之文件处理、函数、内置函数 (三)

    内置函数 一 详细见python文档,请点击 文件操作 操作文件时,一般需要经历如下步骤: 打开文件 操作文件 一.打开文件 文件句柄 = file('文件路径', '模式') 注:python中打开 ...

  9. golang channel状态表

    如果我们查看该表,可以察觉到在操作中可能产生问题的地方.这里有三个可能导致阻塞的操作,以及三 个可能导致程序恐慌的操作. 乍看之下,通道的使用上限制很多,但在检查了这个限制产生的动机并熟悉 了通道的使 ...

  10. 1.5 Scipy:高级科学计算

    sklearn实战-乳腺癌细胞数据挖掘(博主亲自录制视频教程) https://study.163.com/course/introduction.htm?courseId=1005269003&am ...