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 ...
随机推荐
- BZOJ 2427 软件安装(强连通分量+树形背包)
题意:现在我们的手头有N个软件,对于一个软件i,它要占用Wi的磁盘空间,它的价值为Vi.我们希望从中选择一些软件安装到一台磁盘容量为M计算机上,使得这些软件的价值尽可能大(即Vi的和最大).但是现在有 ...
- 【Java】数据库查询的数据直接以指定文件类型下载到本地(弹出下载框)
欲实现的功能目标:当点击下图的导出数据文件时弹出文件下载框,默认csv格式,用户自定义下载的本地路径 遇到的问题: 1.项目之前做过一次下载,但是是使用了本地文件模板.用输入流读取文件模板,插入数据, ...
- "strcmp()" Anyone? UVA - 11732(trie出现的次数)
给你n个单词,让他们两两比较,要求他们运用strcmp时,进行比较的次数. 边建树边统计 #include <iostream> #include <cstdio> #incl ...
- Debug快捷键
Debug快捷键 1. F5单步调试进入函数内部2. F6单步调试不进入函数内部3. F7由函数内部返回到调用处4. F8一直执行到下一个断点5. F11 重新运行debug
- 【刷题】BZOJ 1951 [Sdoi2010]古代猪文
Description "在那山的那边海的那边有一群小肥猪.他们活泼又聪明,他们调皮又灵敏.他们自由自在生活在那绿色的大草坪,他们善良勇敢相互都关心--" --选自猪王国民歌 很久 ...
- [TJOI2015]线性代数 网络流
题面 题面 题解 先化一波式子: \[D = (A \cdot B - C)A^T \] \[ = \sum_{i = 1}^{n}H_{1i}\cdot A^T_{i1}\] \[H_{1i} = ...
- 【BZOJ3242】【NOI2013】快餐店(动态规划)
[BZOJ3242][NOI2013]快餐店(动态规划) 题面 BZOJ 题解 假设我们要做的是一棵树,那么答案显然是树的直径的一半. 证明? 假设树的直径是\(2d\),那么此时最远点的距离是\(d ...
- 【BZOJ4152】The Captain(最短路)
[BZOJ4152]The Captain(最短路) 题面 BZOJ Description 给定平面上的n个点,定义(x1,y1)到(x2,y2)的费用为min(|x1-x2|,|y1-y2|),求 ...
- 2656: [Zjoi2012]数列(sequence)(递归+高精度)
好久没写题了T T NOIP 期中考双血崩 显然f(x)=f(x>>1)+f((x>>1)+1),考虑每次往x>>1递归,求出f(x),复杂度O(logN) 我们设 ...
- 51nod 1684 子集价值
lyk最近在研究位运算. 它发现除了xor,or,and外还有很多运算. 它新定义了一种运算符“#”. 具体地,可以由4个参数来表示. ai,j表示 i#j. 其中i,j与a的值均∈[0,1]. 当然 ...