题意:

统计[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的更多相关文章

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

  2. UVA 10245 The Closest Pair Problem 最近点问题 分治算法

    题意,给出n个点的坐标,找出两点间最近的距离,如果小于10000就输出INFINITY. 纯暴力是会超时的,所以得另辟蹊径,用分治算法. 递归思路将点按坐标排序后,分成两块处理,最近的距离不是在两块中 ...

  3. 洛谷 P1596 [USACO10OCT]湖计数Lake Counting

    题目链接 https://www.luogu.org/problemnew/show/P1596 题目描述 Due to recent rains, water has pooled in vario ...

  4. 《算法导论》——计数排序Counting Sort

    今天贴出的算法是计数排序Counting Sort.在经过一番挣扎之前,我很纠结,今天这个算法在一些scenarios,并不是最优的算法.最坏情况和最好情况下,时间复杂度差距很大. 代码Countin ...

  5. 洛谷——P1596 [USACO10OCT]湖计数Lake Counting

    P1596 [USACO10OCT]湖计数Lake Counting 题目描述 Due to recent rains, water has pooled in various places in F ...

  6. 【题解】晋升者计数 Promotion Counting [USACO 17 JAN] [P3605]

    [题解]晋升者计数 Promotion Counting [USACO 17 JAN] [P3605] 奶牛们又一次试图创建一家创业公司,还是没有从过去的经验中吸取教训.!牛是可怕的管理者! [题目描 ...

  7. 『The Counting Problem 数位dp』

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

  8. POJ2282 The Counting Problem

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

  9. 【概率论】1-2:计数方法(Counting Methods)

    title: [概率论]1-2:计数方法(Counting Methods) categories: Mathematic Probability keywords: Counting Methods ...

随机推荐

  1. PHP的会话处理函数session

    (๑•ᴗ•๑) PHP Session 变量 当运行一个应用程序时,你会打开它,做些更改,然后关闭它.这很像一次会话.计算机清楚你是谁.它知道你何时启动应用程序,并在何时终止.但是在因特网上,存在一个 ...

  2. 移植linux4.7.2与ubifs到jz2440

    前言 整个暑假跟着韦东山的视频和书籍移植了linux2.3.6到jz2440,现在自己尝试移植linux4.7.2到板子上,并使用ubifs文件系统代替旧的jffs2文件系统. 下载交叉编译工具链 工 ...

  3. UML: CIM & PIM

    CIM-1:定义业务流程 定义及分析业务流程(Business Process)是为了尽快理清系统范围,以便估算开发成本及时间,可不是为了要改造业务流程.系统分析员千万别误解了此步骤的目的.所以,系统 ...

  4. WinForm点击按钮在对应的panel里画图

    panel在form1里,button在form1上方,panel在下面. 主要是在button1的click时间获取panel的画笔. 下面的不行,在panel里获取画笔,然后传到button1,根 ...

  5. Oracle 10G 安装后 不能卸载, 不能打开Web管理....

    1. 不能卸载. 点击这个卸载.   提示:   解决方法: 右键卸载, 设置兼容性为XP. 就可以使用了.    2. oracle10g登录em后,提示"java.lang.Except ...

  6. 【unity3d游戏开发之基础篇】unity3d射线的原理用法以及一个利用射线实现简单拾取的小例子

    原地址:http://www.cnblogs.com/xuling/archive/2013/03/04/2943154.html 最近开始研究U3D,它的强大就不多说了, 今天研究了研究射线相关东西 ...

  7. SPOJ Lexicographical Substring Search 后缀自动机

    给你一个字符串,然后询问它第k小的factor,坑的地方在于spoj实在是太慢了,要加各种常数优化,字符集如果不压缩一下必t.. #pragma warning(disable:4996) #incl ...

  8. 递归算法实现10进制到N进制的转换

    #include<iostream> using namespace std; int BaseTrans(int data,int B){ int s; ) ; //结束递归算法 s=d ...

  9. javascript-权威指南读书笔记(1)

    <script type="text/javascript"> //数组与object的关系 var obj = { name: "刘建伟", Ag ...

  10. SDUT1574组合数的计算(组合数)

    http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=1574 这个题,比较奇怪,是用递推去做的,我试了 ...