您的位置:首页 > 其它

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

2015-04-21 21:27 459 查看
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 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


题意:

统计从a数到b中,数过的数里面各个位置上0-9的个数。

分析:

从这道题上更体现了数学的魅力。。。

比如算4123中有多少个2

按位统计,,,先算各位,,个位是2的情况有413种,,,因为各位左边可以0~412,,,而右边没有数字,,,

然后是十位,,,十位是2的有41*10 + 1*4种,,当左边从0~40时,,,右边可以从0~9,,,而左边为41时,,右边只能从0~3

然后是百位,,,,百位有4*100种,,,,即左边从0~3,,右边从0~99

千位有 1*1000,,,左边没有数字,,,右边0~999,,,,

上面是计算1~9,,,,计算0的时候比较特殊,,,,原因是除了0这一个数字之外,,,,0不能做开头,,,

可以看到在求1~9的个数的时候,,,都是分为2部分相乘,,,这样0的处理也很简单,,只需把相乘的左半部分-1,,,,

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <set>
#include <queue>
#include <stack>
#include <map>
using namespace std;
typedef long long LL;
const int inf=0x3f3f3f3f;
const double pi= acos(-1.0);
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
int b[9]={1,10,100,1000,10000,100000,1000000,10000000,100000000};
LL get_res(int n,int site)
{
int i;
LL left,m;
LL cnt=0;
for(i=1;i<9;i++){
if(site==0)//因为没有前导零的情况所以要特判一下
left=n/b[i]-1;
else
left=n/b[i];
cnt+=left*b[i-1];
m=(n%b[i]-n%b[i-1])/b[i-1];//求出从低到高的第i位上的具体数字
if(m>site)
cnt+=b[i-1];
else if(m==site)
cnt+=n%b[i-1]+1;
if(n<b[i])
break;
}
return cnt;
}

int main()
{
int n,m,i;
while(~scanf("%d %d",&n,&m)){
if(!n&&!m) break;
if(n>m) swap(n,m);
for(i=0;i<9;i++){
printf("%lld ",get_res(m,i)-get_res(n-1,i));
}
printf("%lld\n",get_res(m,9)-get_res(n-1,9));
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: