题意:给定两个数m, n,求从 m 到 n 中0-9数字各出现了多少次。

析:看起来挺简单的,其实并不好做,因为有容易想乱了。主要思路应该是这样的,分区间计数,先从个位进行计,一步一步的计算过来。都从0开始,最后用大数减小数的即可。

举个例子吧,容易理解。比如0-1234。

先计算个位数字,有1-4,然后计算123各出现了5次,注意是这里是5次,不是4次,因为我们要加上那个0,然后就剩下那个1230了,我们想那么现在个位数从开始到这,

重复了123次,然后再进行下一位,依次进行,直到0.

代码如下:

  1. #pragma comment(linker, "/STACK:1024000000,1024000000")
  2. #include <cstdio>
  3. #include <string>
  4. #include <functional>
  5. #include <cstdlib>
  6. #include <cmath>
  7. #include <iostream>
  8. #include <cstring>
  9. #include <set>
  10. #include <queue>
  11. #include <algorithm>
  12. #include <vector>
  13. #include <deque>
  14. #include <map>
  15. #include <cctype>
  16. #include <stack>
  17. #include <sstream>
  18. #include <cstdlib>
  19. #define freopenr freopen("in.txt", "r", stdin)
  20. #define freopenw freopen("out.txt", "w", stdout)
  21. using namespace std;
  22. #include <ctime>
  23.  
  24. typedef long long LL;
  25. typedef pair<int, int> P;
  26. const int INF = 0x3f3f3f3f;
  27. const LL LNF = 0x3f3f3f3f3f;
  28. const double inf = 0x3f3f3f3f3f3f;
  29. const double PI = acos(-1.0);
  30. const double eps = 1e-8;
  31. const int maxn = 1e5 + 5;
  32. const int mod = 1e9 + 7;
  33. const char *mark = "+-*";
  34. const int dr[] = {-1, 0, 1, 0};
  35. const int dc[] = {0, 1, 0, -1};
  36. int n, m;
  37. inline int Min(int a, int b){ return a < b ? a : b; }
  38. inline int Max(int a, int b){ return a > b ? a : b; }
  39. inline LL Min(LL a, LL b){ return a < b ? a : b; }
  40. inline LL Max(LL a, LL b){ return a > b ? a : b; }
  41. int ans[15];
  42.  
  43. void dfs(int n, int m, int ok){
  44. if(-1 == n) return ;
  45. int x = n / 10;
  46. int y = n % 10;
  47. for(int i = 1; i <= y; ++i) ans[i] += ok * m;
  48. for(int i = 0; i < 10; ++i) ans[i] += ok * m * x;
  49. int tmp = x;
  50. while(tmp){
  51. ans[tmp%10] += ok * (y+1) * m;
  52. tmp /= 10;
  53. }
  54. dfs(x-1, m*10, ok);
  55. }
  56.  
  57. int main(){
  58. while(scanf("%d %d", &m, &n) == 2){
  59. if(!m && !n) break;
  60. if(m < n) swap(m, n);
  61. --n;
  62. memset(ans, 0, sizeof ans);
  63. dfs(m, 1, 1);
  64. dfs(n, 1, -1);
  65.  
  66. for(int i = 0; i < 10; ++i){
  67. if(i) putchar(' ');
  68. printf("%d", ans[i]);
  69. }
  70. printf("\n");
  71. }
  72. return 0;
  73. }

  

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.1640.The Counting Problem / BZOJ.1833.[ZJOI2010]数字计数(数位DP)

    题目链接 \(Description\) 求\([l,r]\)中\(0,1,\cdots,9\)每个数字出现的次数(十进制表示). \(Solution\) 对每位分别DP.注意考虑前导0: 在最后统 ...

  3. UVa 1640 - The Counting Problem(数论)

    链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  4. UVA 1640 The Counting Problem

    https://vjudge.net/problem/UVA-1640 题意:统计区间[l,r]中0——9的出现次数 数位DP 注意删除前导0 #include<cmath> #inclu ...

  5. UVA 1640 The Counting Problem(按位dp)

    题意:给你整数a.b,问你[a,b]间每个数字分解成单个数字后,0.1.2.3.4.5.6.7.8.9,分别有多少个 题解:首先找到[0,b]与[0,a-1]进行区间减法,接着就只是求[0,x] 对于 ...

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

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

  7. Codeforces ECR86 C. Yet Another Counting Problem(规律,区间)

    题意:给你两个正整数a和b,询问q次,每次给你一个区间[l,r],问[l,r]中有多少数字满足:x%a%b!=a%b%a. 题解:看公式无从下手的题,一般都是要找规律的.首先,我们知道,假如x%a%b ...

  8. The Counting Problem

    The Counting Problem 询问区间\([a,b]\)中\(1\sim 9\)出现的次数,0 < a, b < 100000000. 解 显然为数位递推,考虑试填法,现在关键 ...

  9. POJ2282 The Counting Problem

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

随机推荐

  1. hdu 4861 Couple doubi (找规律 )

    题目链接 可以瞎搞一下,找找规律 题意:两个人进行游戏,桌上有k个球,第i个球的值为1i+2i+⋯+(p−1)i%p,两个人轮流取,如果DouBiNan的值大的话就输出YES,否则输出NO. 分析:解 ...

  2. centos的版本和内核查看

    查看linu的内核信息 查看distrubution,centos属于哪个release

  3. UVa 10305 (拓扑排序) Ordering Tasks

    题意: 经典的拓扑排序.有n个任务,然后某些任务必须安排在某些任务前面完成,输出一种满足要求的序列. 分析: 拓扑排序用离散里面的话来说就是将偏序关系拓展为全序关系.我们将“小于”这种关系看做一条有向 ...

  4. bzoj1567: [JSOI2008]Blue Mary的战役地图

    将矩阵hash.s[0]忘了弄成0,输出中间过程发现了. hash.sort.判重.大概这样子的步骤吧. #include<cstdio> #include<cstring> ...

  5. 页面多个Jquery版本共存的冲突问题,解决方法!

    示例如下: <script type="text/javascript" src="jquery.js"></script> <s ...

  6. dagli最早干了这样一件事儿 Localization of Cardiac-Induced Signal Change in fMRI

    Localization of Cardiac-Induced Signal Change in fMRI 这篇文章是最早做生理噪声相关组织的定位的. 很奇特,因为,这位学者甚至得出了,血管心动等变化 ...

  7. 【大数模板】C++大数类 大数模板

    分别使用C++中的运算符重载的方法来实现大数之间的数学运算,包括加法.减法.乘法.除法.n次方.取模.大小比较.赋值以及输入流.输出流的重载. 感觉很麻烦... [代码] #include<io ...

  8. YUV采样及存储格式

    YUV,分为三个分量,“Y”表示明亮度(Luminance或Luma),也就是灰度值:而“U”和“V” 表示的则是色度(Chrominance或Chroma),作用是描述影像色彩及饱和度,用于指定像素 ...

  9. [Everyday Mathematics]20150206

    $$\bex \sen{fg}_{L^1}\leq C\sen{f}_{L^{r,\al}}\sen{g}_{L^{r',\al'}}, \eex$$ 其中 $$\bex f\in L^{r,\al} ...

  10. Grep 命令 用法大全

    查找x文件 find / -name "x*" -ls 查找文件中x所在的行数 grep -n "x" -r *find . -name "*.jav ...