题意:

统计[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

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 using namespace std;
 5
 6 int pow10[10], cnt[10];
 7
 8 int f(int d, int n)
 9 {
10     char s[15];
11     sprintf(s, "%d", n);
12     int len = strlen(s);
13     int ans = 0;
14
15     for(int i = 1; i < len; i++)
16     {
17         if(i == 1) { ans++; continue; }
18         ans += 9 * cnt[i - 1];
19         if(d > 0) ans += pow10[i - 1];
20     }
21
22     int pre[10];
23     for(int i = 0; i < len; i++)
24     {
25         pre[i] = (int)(s[i] - '0' == d);
26         if(i) pre[i] += pre[i - 1];
27     }
28
29     for(int i = 0; i < len; i++)
30     {
31         int maxd = s[i] - '0' - 1;
32         int mind = 0;
33         if(i == 0 && len > 1) mind = 1;
34         for(int digit = mind; digit <= maxd; digit++)
35         {
36             ans += cnt[len - i - 1];
37             if(i) ans += pre[i - 1] * pow10[len - i - 1];
38             if(digit == d) ans += pow10[len - i - 1];
39         }
40     }
41     return ans;
42 }
43
44 int main()
45 {
46     //freopen("in.txt", "r", stdin);
47
48     pow10[0] = 1;
49     for(int i = 1; i <= 8; i++)
50     {
51         pow10[i] = pow10[i - 1] * 10;
52         cnt[i] = pow10[i - 1] * i;
53     }
54
55     int a, b;
56     while(scanf("%d%d", &a, &b) == 2 && a && b)
57     {
58         if(a > b) swap(a, b);
59         for(int d = 0; d <= 9; d++)
60         {
61             if(d) printf(" ");
62             printf("%d", f(d, b+1) - f(d, a));
63         }
64         printf("\n");
65     }
66
67     return 0;
68 }

代码君

转载于:https://www.cnblogs.com/AOQNRMGYXLMV/p/4318926.html

UVa 1640 (计数) The Counting Problem相关推荐

  1. C语言计数排序Counting sort 算法(附完整源码)

    计数排序Counting sort 算法 计数排序Counting sort 算法的完整源码(定义,实现,main函数测试) 计数排序Counting sort 算法的完整源码(定义,实现,main函 ...

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

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

  3. Numpy 排序(sorting)、查询(searching)、计数(counting)

    #排序(Sorting) Function Describe sort(a[, axis, kind, order]) Return a sorted copy of an array. lexsor ...

  4. UVA - 1640 The Counting Problem (数位dp)

    题意:统计l-r中每种数字出现的次数 很明显的数位dp问题,虽然有更简洁的做法但某人已经习惯了数位dp的风格所以还是选择扬长避短吧(说白了就是菜啊) 从高位向低位走,设状态$(u,lim,ze)$表示 ...

  5. 人群计数Crowd counting 和 Swin Transformer

    文章题目:CCST: crowd counting with swin transformer 文章链接:https://link.springer.com/article/10.1007/s0037 ...

  6. UVA - 524:Prime Ring Problem

    Prime Ring Problem 来源:UVA 题目 A ring is composed of n (even number) circles as shown in diagram. Put ...

  7. 计数排序/Counting Sort

    计数排序的算法思想: 对于每一个元素x,只要确定了元素x有多少个比它小的元素,那么就可以知道其最终的位置. 记输入数组为A[n],存放最后排序输出的数组为B[n],提供临时存储空间的中间数组记为C[k ...

  8. UVA 10564 计数DP

    也是经典的计数DP题,想练练手,故意不写记忆化搜索,改成递推,还是成功了嘞...不过很遗憾一开始WA了,原来是因为判断结束条件写个 n或s为0,应该要一起为0的,搞的我以为自己递推写挫了,又改了一下, ...

  9. 计数排序Counting sort

    注意与基数排序区分,这是两个不同的排序 计数排序的过程类似小学选班干部的过程,如某某人10票,作者9票,那某某人是班长,作者是副班长 大体分两部分,第一部分是拉选票和投票,第二部分是根据你的票数入桶 ...

最新文章

  1. leetcode-回文数(简单)
  2. IntelliJ IDEA 环境常用设置整理
  3. Eclipse+Maven+SpringMVC+Mybatis+MySql搭建总结
  4. 解决非controller使用@Autowired注解注入报错为java.lang.NullPointerException问题
  5. 北京五环以内将全面禁止新建和扩建数据中心
  6. db2 空值转换函数_Hive常见函数的使用
  7. 【Kafka】Confluent Schema Registry
  8. SpringCloud学习笔记019---Windows 平台安装 MongoDB
  9. 《通信原理》awgn信道仿真
  10. Easy Connect 当前IE代理启用了自动配置脚本,不允许使用CS客户端登录
  11. oppo小布机器人_看这一篇就够了,1分钟带你了解OPPO小布的隐藏玩法!
  12. 微信流量主几个月几年没结算,无月份结算单解决方案
  13. 解决网页中文字无法选中的问题
  14. 超级内存NVDIMM--要和内存说再见?Intel将推存储界新物种NVDIMM内存
  15. 【GZH逸佳君】:科技感膨爆,观赏性极强:送你PS粒子飞溅特效插件,1秒瞬间爆开
  16. 货郎问题java_货郎问题
  17. 网络工程师——Private VLAN
  18. Spark Mllib 下的决策树二元分类 —— 网站分类(1)
  19. 计蒜客题解——T1214:鸣人和佐助
  20. 1700人参加的钢铁行业盛会,下面是你必须知道的!

热门文章

  1. 循环队列(循环数组)中元素个数的计算
  2. 数据字符集mysql主从数据库,分库分表等笔记
  3. IIS6架设网站常见问题及症状答疑
  4. Page.RegisterClientScriptBlock和Page.RegisterStartupScript有何区别
  5. iframe框架及优缺点
  6. java blockqueue_[Java基础] Java多线程-工具篇-BlockingQueue
  7. 开源战略游戏源码_开源的历史告诉我们关于战略优势的知识
  8. 字扩展,位扩展,和字位扩展_6个有用的LibreOffice扩展
  9. 面试官 | Java转List三种方式,你说说吧。我。。懵逼。啥时候有三种了
  10. 设计模式 迪米特法则