HDOJ 1663 The Counting Problem 打表

先打出0~8位数,分别可以被整十/百/千/万...整除时 , 各个数字出现了几次的表

先把每要查询的数字的每一位在表里查询得到一个结果

但是这样是不全面的,考虑这样的情况: 例如2345这样的数 234* 这种情况下 4出现了5次 23**这种情况下3出现了45次 2***中2出现了345次等.....从后往前扫一遍即可

其中0的情况比较特殊,简单的扫一遍会漏掉很多可能 比如 5050时: 500*的情况下,第2个0就没有考虑到,所以还要进行一次补0的操作.

排除首位从前往后扫,遇到每一个不为0的数就考虑将这位变成0,如果这个数后面还有1个数就有9中可能,后面有2个数就有108种可能(9+99) 有3个数就有(9+99+999)种可能...依次类推.....

结果加到一起就是答案

The Counting Problem

Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 421    Accepted Submission(s): 169

Problem Description

Given two integers a and b, we write the numbers between a and b, inclusive, in a list. Your task is to calculate the number of occurrences of each digit. For example, if a = 1024 and b = 1032, the list will be

1024 1025 1026 1027 1028 1029 1030 1031 1032

there are ten 0‘s in the list, ten 1‘s, seven 2‘s, three 3‘s, and etc.

Input

The input consists of up to 500 lines. Each line contains two numbers a and b where 0 < a, b < 100000000. The input is terminated by a line `0 0‘, which is not considered as part of the input.

Output

For each pair of input, output a line containing ten numbers separated by single spaces. The first number is the number of occurrences of the digit 0, the second is the number of occurrences of the digit 1, etc.

Sample Input

1 10
44 497
346 542
1199 1748
1496 1403
1004 503
1714 190
1317 854
1976 494
1001 1960
0 0

Sample Output

1 2 1 1 1 1 1 1 1 1
85 185 185 185 190 96 96 96 95 93
40 40 40 93 136 82 40 40 40 40
115 666 215 215 214 205 205 154 105 106
16 113 19 20 114 20 20 19 19 16
107 105 100 101 101 197 200 200 200 200
413 1133 503 503 503 502 502 417 402 412
196 512 186 104 87 93 97 97 142 196
398 1375 398 398 405 499 499 495 488 471
294 1256 296 296 296 296 287 286 286 247

Source

2004 Asia Regional Shanghai

/* ***********************************************
Author        :CKboss
Created Time  :2015年02月01日 星期日 13时58分50秒
File Name     :HDOJ1663_3.cpp
************************************************ */

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <queue>
#include <set>
#include <map>

using namespace std;

/// 不补0的表
int no[][10] = {
1,0,0,0,0,0,0,0,0,0,
1,1,0,0,0,0,0,0,0,0,
1,1,1,0,0,0,0,0,0,0,
1,1,1,1,0,0,0,0,0,0,
1,1,1,1,1,0,0,0,0,0,
1,1,1,1,1,1,0,0,0,0,
1,1,1,1,1,1,1,0,0,0,
1,1,1,1,1,1,1,1,0,0,
1,1,1,1,1,1,1,1,1,0,
1,1,1,1,1,1,1,1,1,1,
0,0,0,0,0,0,0,0,0,0,
1,2,1,1,1,1,1,1,1,1,
2,12,3,2,2,2,2,2,2,2,
3,13,13,4,3,3,3,3,3,3,
4,14,14,14,5,4,4,4,4,4,
5,15,15,15,15,6,5,5,5,5,
6,16,16,16,16,16,7,6,6,6,
7,17,17,17,17,17,17,8,7,7,
8,18,18,18,18,18,18,18,9,8,
9,19,19,19,19,19,19,19,19,10,
0,0,0,0,0,0,0,0,0,0,
11,21,20,20,20,20,20,20,20,20,
31,140,41,40,40,40,40,40,40,40,
51,160,160,61,60,60,60,60,60,60,
71,180,180,180,81,80,80,80,80,80,
91,200,200,200,200,101,100,100,100,100,
111,220,220,220,220,220,121,120,120,120,
131,240,240,240,240,240,240,141,140,140,
151,260,260,260,260,260,260,260,161,160,
171,280,280,280,280,280,280,280,280,181,
0,0,0,0,0,0,0,0,0,0,
192,301,300,300,300,300,300,300,300,300,
492,1600,601,600,600,600,600,600,600,600,
792,1900,1900,901,900,900,900,900,900,900,
1092,2200,2200,2200,1201,1200,1200,1200,1200,1200,
1392,2500,2500,2500,2500,1501,1500,1500,1500,1500,
1692,2800,2800,2800,2800,2800,1801,1800,1800,1800,
1992,3100,3100,3100,3100,3100,3100,2101,2100,2100,
2292,3400,3400,3400,3400,3400,3400,3400,2401,2400,
2592,3700,3700,3700,3700,3700,3700,3700,3700,2701,
0,0,0,0,0,0,0,0,0,0,
2893,4001,4000,4000,4000,4000,4000,4000,4000,4000,
6893,18000,8001,8000,8000,8000,8000,8000,8000,8000,
10893,22000,22000,12001,12000,12000,12000,12000,12000,12000,
14893,26000,26000,26000,16001,16000,16000,16000,16000,16000,
18893,30000,30000,30000,30000,20001,20000,20000,20000,20000,
22893,34000,34000,34000,34000,34000,24001,24000,24000,24000,
26893,38000,38000,38000,38000,38000,38000,28001,28000,28000,
30893,42000,42000,42000,42000,42000,42000,42000,32001,32000,
34893,46000,46000,46000,46000,46000,46000,46000,46000,36001,
0,0,0,0,0,0,0,0,0,0,
38894,50001,50000,50000,50000,50000,50000,50000,50000,50000,
88894,200000,100001,100000,100000,100000,100000,100000,100000,100000,
138894,250000,250000,150001,150000,150000,150000,150000,150000,150000,
188894,300000,300000,300000,200001,200000,200000,200000,200000,200000,
238894,350000,350000,350000,350000,250001,250000,250000,250000,250000,
288894,400000,400000,400000,400000,400000,300001,300000,300000,300000,
338894,450000,450000,450000,450000,450000,450000,350001,350000,350000,
388894,500000,500000,500000,500000,500000,500000,500000,400001,400000,
438894,550000,550000,550000,550000,550000,550000,550000,550000,450001,
0,0,0,0,0,0,0,0,0,0,
488895,600001,600000,600000,600000,600000,600000,600000,600000,600000,
1088895,2200000,1200001,1200000,1200000,1200000,1200000,1200000,1200000,1200000,
1688895,2800000,2800000,1800001,1800000,1800000,1800000,1800000,1800000,1800000,
2288895,3400000,3400000,3400000,2400001,2400000,2400000,2400000,2400000,2400000,
2888895,4000000,4000000,4000000,4000000,3000001,3000000,3000000,3000000,3000000,
3488895,4600000,4600000,4600000,4600000,4600000,3600001,3600000,3600000,3600000,
4088895,5200000,5200000,5200000,5200000,5200000,5200000,4200001,4200000,4200000,
4688895,5800000,5800000,5800000,5800000,5800000,5800000,5800000,4800001,4800000,
5288895,6400000,6400000,6400000,6400000,6400000,6400000,6400000,6400000,5400001,
0,0,0,0,0,0,0,0,0,0,
5888896,7000001,7000000,7000000,7000000,7000000,7000000,7000000,7000000,7000000,
12888896,24000000,14000001,14000000,14000000,14000000,14000000,14000000,14000000,14000000,
19888896,31000000,31000000,21000001,21000000,21000000,21000000,21000000,21000000,21000000,
26888896,38000000,38000000,38000000,28000001,28000000,28000000,28000000,28000000,28000000,
33888896,45000000,45000000,45000000,45000000,35000001,35000000,35000000,35000000,35000000,
40888896,52000000,52000000,52000000,52000000,52000000,42000001,42000000,42000000,42000000,
47888896,59000000,59000000,59000000,59000000,59000000,59000000,49000001,49000000,49000000,
54888896,66000000,66000000,66000000,66000000,66000000,66000000,66000000,56000001,56000000,
61888896,73000000,73000000,73000000,73000000,73000000,73000000,73000000,73000000,63000001,
};

int bulin[20]={0,9,108,1107,11106,111105,1111104,11111103,111111102,1111111101};

int l,r;
int num[2][10];
int wei[20],wn;

void solve(int id,int x)
{
	memset(wei,0,sizeof(wei)); wn=0;
	memset(num[id],0,sizeof(num[id]));
	if(x==0) { num[id][0]++; return ;}
	while(x) { wei[wn++]=x%10; x/=10; }
	reverse(wei,wei+wn);
	for(int i=0;i<wn;i++)
	{
		int row = (wn-i-1)*10+wei[i];
		for(int j=0;j<10;j++)
		{
			num[id][j]+=no[row][j];
		}
	}
	int res=0,e=0;
	for(int i=wn-1;i>0;i--)
	{
		if(i==wn-1) res=res+wei[i];
		else res=res+wei[i]*(int)(pow(10,e));
		e++;
		if(i-1>=0) num[id][wei[i-1]]+=res;
	}
	for(int i=1;i<wn;i++)
	{
		if(wei[i])
		{
			int t = wn - i - 1;
			num[id][0]+=bulin[t];
		}
	}
}

int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);

	while(scanf("%d%d",&l,&r)!=EOF)
	{
		if(l==0&&r==0) break;
		if(l>r) swap(l,r); l--;
		solve(1,r); solve(0,l);
		for(int i=0;i<10;i++)
		{
			printf("%d%c",num[1][i]-num[0][i],(i==9)?'\n':' ');
		}
	}
    return 0;
}
时间: 2024-12-15 07:03:55

HDOJ 1663 The Counting Problem 打表的相关文章

HDOJ 3518 Boring counting

SAM基本操作 拓扑求每个节点的  最左出现left,最右出现right,出现了几次num ...... 对于每一个出现两次以上的节点,对其所对应的一串子串的长度范围 [fa->len+1,len] 和其最大间距 right-left比较 即可...... Boring counting Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s):

HDOJ 1002 A + B Problem II (Big Numbers Addition)

题目链接在此?http://acm.hdu.edu.cn/showproblem.php?pid=1002 这题也比较简单,只需要开三个长度为1000的char数组来分别储存a.b.ans,再利用我们加法的算法,先向右对齐再相加.注意一下进位时的特殊情况就好了. 不过笔者的代码写好后提交上去,两次Presentation Error,然后才发现只是最后多输出一个空行的问题  =.= Orz /**  * HDOJ 1002 A + B Problem II  * Big Numbers Addi

hdoj 2089 不要62 【打表】

题意:.. 水题 代码: #include <stdio.h> #include <string.h> #include <algorithm> using namespace std; int s[10]; int ans[1000005] = {0, 1}; int is(int n){ //n = 62; int pre = 0, cur = 0, i; while(n){ pre = cur; cur = n%10; n /= 10; if(cur == 4||

POJ 2282 The Counting Problem,组合数学

POJ 2282 The Counting Problem,组合数学 ACM 题目地址:POJ 2282 题意: 给出俩数n,m,求从n~m中0~9分别出现的次数. 分析: 组合数学. 只要能快速算出0~n中各个数的出现次数就能解决问题了. 要把数拆开来看,比如3456=3000+400+50+6. 然后就只要考虑后面都是0的数就行了. 0~3000中,我们要分为两部分来考虑: 在第一位中,0\1\2都出现了1000次. 假设不管第一位,后面那些位数出现0~9的几率是均等的(先不考虑前导0).那

UVALive3261 UVA1640 POJ2282 HDU1663 ZOJ2392 The Counting Problem

Regionals 2004 >> Asia - Shanghai 问题链接:UVALive3261 UVA1640 POJ2282 HDU1663 ZOJ2392 The Counting Problem. 问题简述:输入m和n,计算m到n(包括m和n)之间各个数中包含多少个0-9数字. 问题分析:先分别计算0到m-1和0到n之间数的数字个数,结果=0到n之间数的数字个数-0到m-1之间数的数字个数.计算0到n之间数的数字个数时,先考虑1位数.2位数.......,在小于n的区间逐步统计.

POJ 2282-The Counting Problem(组合数学_区间计数)

The Counting Problem Time Limit:3000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 2282 Appoint description:  System Crawler  (2015-04-15) Description Given two integers a and b, we write the numbers between

HDOJ 5371 Hotaru&#39;s problem manacher+优先队列+二分

先用求回文串的Manacher算法,求出以第i个点和第i+1个点为中心的回文串长度,记录到数组c中 比如 10 9 8 8 9 10 10 9 8 我们通过运行Manacher求出第i个点和第i+1个点为中心的回文串长度 0 0 6 0 0 6 0 0 0 两个8为中心,10 9 8 8 9 10是个回文串,长度是6. 两个10为中心,8 9 10 10 9 8是个回文串,长度是6. 要满足题目所要求的内容,需要使得两个相邻的回文串,共享中间的一部分,比如上边的两个字符串,共享 8 9 10这一

The Counting Problem poj2282

/************************************************************************/ /* 递归问题: 给定一个区间,求其中所有0~9数字总计出现次数 思路: 设f(a),f(b)为各自从1开始到a,b中所有0~9数字总计出现次数. 则ans=f(b)-f(a-1);递归的话就是建立f(k)和f(10*k+x)的关系. 对于一个大数,先处理到末尾为0,然后再认为是从0开始10个10个数,数到这个数. 例如,f(2984)就先从298

hdoj 5371 Hotaru&#39;s problem

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5371 这道题用到了Manacher算法,首先简单介绍一下Manacher算法: ---------------------------------------------------------------------------------------------- [转]http://blog.csdn.net/yzl_rex/article/details/7908259 一个专门针对回文子串