UVa 1640 (计数) The Counting Problem
题意:
统计[a, b]或[b, a]中0~9这些数字各出现多少次。
分析:
这道题可以和UVa 11361比较来看。
同样是利用这样一个“模板”,进行区间的分块,加速运算。
因为这里没有前导0,所以分块的时候要多分几种情况。
以2345为例,这是一个四位数,首先要计算一下所有的一位数、两位数以及三位数各个数字出现的个数。
对应的模板分别为n,n*,n**,其中n代表非零数字,*代表任意数字。
考虑这样一个长为l的模板****(l个*),这样的数共10l个,而且各个数字都是等频率出现,所以每个数字出现的次数为l * 10l-1
统计完三位数一下的数字之后,就开始统计四位数字:1***,20**,21**,22**,230*,231*,232*,233*,2340,2341,2342,2343,2344,2345
在统计每个模板时,分这样两块计算:
- **中该数字出现的次数
- 前面该数出现的次数,比如22**,前面两个2会重复102次
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; int pow10[], cnt[]; int f(int d, int n)
{
char s[];
sprintf(s, "%d", n);
int len = strlen(s);
int ans = ; for(int i = ; i < len; i++)
{
if(i == ) { ans++; continue; }
ans += * cnt[i - ];
if(d > ) ans += pow10[i - ];
} int pre[];
for(int i = ; i < len; i++)
{
pre[i] = (int)(s[i] - '' == d);
if(i) pre[i] += pre[i - ];
} for(int i = ; i < len; i++)
{
int maxd = s[i] - '' - ;
int mind = ;
if(i == && len > ) mind = ;
for(int digit = mind; digit <= maxd; digit++)
{
ans += cnt[len - i - ];
if(i) ans += pre[i - ] * pow10[len - i - ];
if(digit == d) ans += pow10[len - i - ];
}
}
return ans;
} int main()
{
//freopen("in.txt", "r", stdin); pow10[] = ;
for(int i = ; i <= ; i++)
{
pow10[i] = pow10[i - ] * ;
cnt[i] = pow10[i - ] * i;
} int a, b;
while(scanf("%d%d", &a, &b) == && a && b)
{
if(a > b) swap(a, b);
for(int d = ; d <= ; d++)
{
if(d) printf(" ");
printf("%d", f(d, b+) - f(d, a));
}
printf("\n");
} return ;
}
代码君
UVa 1640 (计数) The Counting Problem的更多相关文章
- 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] ...
- UVA 10245 The Closest Pair Problem 最近点问题 分治算法
题意,给出n个点的坐标,找出两点间最近的距离,如果小于10000就输出INFINITY. 纯暴力是会超时的,所以得另辟蹊径,用分治算法. 递归思路将点按坐标排序后,分成两块处理,最近的距离不是在两块中 ...
- 洛谷 P1596 [USACO10OCT]湖计数Lake Counting
题目链接 https://www.luogu.org/problemnew/show/P1596 题目描述 Due to recent rains, water has pooled in vario ...
- 《算法导论》——计数排序Counting Sort
今天贴出的算法是计数排序Counting Sort.在经过一番挣扎之前,我很纠结,今天这个算法在一些scenarios,并不是最优的算法.最坏情况和最好情况下,时间复杂度差距很大. 代码Countin ...
- 洛谷——P1596 [USACO10OCT]湖计数Lake Counting
P1596 [USACO10OCT]湖计数Lake Counting 题目描述 Due to recent rains, water has pooled in various places in F ...
- 【题解】晋升者计数 Promotion Counting [USACO 17 JAN] [P3605]
[题解]晋升者计数 Promotion Counting [USACO 17 JAN] [P3605] 奶牛们又一次试图创建一家创业公司,还是没有从过去的经验中吸取教训.!牛是可怕的管理者! [题目描 ...
- 『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 ...
- 【概率论】1-2:计数方法(Counting Methods)
title: [概率论]1-2:计数方法(Counting Methods) categories: Mathematic Probability keywords: Counting Methods ...
随机推荐
- 《C++Primer》复习——with C++11 [4]
考虑到STL的掌握主要靠的是练习,所以对于STL这部分,我把书中的练习都做一遍,加深印象.这些练习是第9.10.11.17章的,分别是顺序容器.泛型算法和关联容器等. ——10月22日 /*----- ...
- Getting Started with Java
“学前”说明:<Learn Java for Android>这本书内容很多,都是精华,建议大家看英文版的.在这里我不打算一一总结书中的内容,书中每章节后面的exercises都很好,非常 ...
- html+css学习笔记 3[浮动]
inline-block/float(浮动) 回顾:inline-block 特性: 1.块在一排显示 2.内联支持宽高 3.默认内容撑开宽度 4.标签之间的换行间隙被解析(问题) 5.ie ...
- pos机套现是怎么回事
POS机是商家为了促进消费,向银行申请的刷卡机它的主要功能是转账就是通过客户的刷卡,把相对的金额转入商户的帐户银行会根据笔数或金额向商户收取手续费非法套现就是客户并未和商户产生贸易往来,单纯通过pos ...
- 用hibernate自动创建mysql表,添加失败org.hibernate.exception.SQLGrammarException
今天遇到了一个很坑人的问题,从昨晚一直搞到今天早上,终于发现了,先整理下: [背景]:利用hibernate自动给mysql创建一个表,然后为表里面添加一行记录,非常的简单(当然其中还涉及到sprin ...
- SCI杂志更名时,如何计算影响因子?
- Eclipse插件开发 swt ComboBoxCellEditor CCombo 下拉框高度
效果图: 代码如下 bindingPageTableViewer.setCellModifier(new ICellModifier() { public boolean canModify( ...
- Embedding Lua in C: Using Lua from inside C.
Requirments: 1: The Lua Sources. 2: A C compiler - cc/gcc/g++ for Unix, and Visual C++ for Wi ...
- Eclipse Error: Unable to set localhost. This prevents creation of a GUID.
Symptoms The following error appears in the atlassian-confluence.log: 2011-03-16 18:20:03,021 ERROR ...
- Url重写和伪静态
这里是URL重写的精华:http://msdn.microsoft.com/zh-cn/library/ms972974.aspx感觉写的非常棒. 其实URL重写操作起来也是挺简单的,只要你在前台写好 ...