POJ 3286 How many 0's? / 2282 The Counting Problem 排列组合统计数字
2012-04-05 20:06
417 查看
比如算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,,,,
POJ 3286 题意:输入m,n,求[m,n]之间的所有数中0出现的次数。
POJ 2282 题意:输入m,n,求[m,n]之间的所有数中0,1,2,3,4,5,6,7,8,9出现的次数。
按位统计,,,先算各位,,个位是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,,,,
POJ 3286 题意:输入m,n,求[m,n]之间的所有数中0出现的次数。
#include<cstdio> #include<algorithm> using namespace std; #define lint __int64 lint b[12] = { 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000, 10000000000, 100000000000 }; lint count ( lint n ) { lint left, m, sum = 0; for ( int i = 1; i < 12; i++ ) { left = n / b[i] - 1; sum += left * b[i-1]; m = (n % b[i] - n % b[i-1]) / b[i-1]; //求出从第到高的第i位上的具体数字 if ( m > 0 ) sum += b[i-1]; else if ( m == 0 ) sum += n % b[i-1] + 1; if ( n < b[i] ) break; } return sum; } int main() { lint m, n; while ( scanf("%I64d%I64d",&m,&n) && (n>=0) ) printf("%I64d\n",count(n)-count(m-1)); return 0; }
POJ 2282 题意:输入m,n,求[m,n]之间的所有数中0,1,2,3,4,5,6,7,8,9出现的次数。
#include<cstdio> #include<algorithm> using namespace std; #define lint __int64 int b[9] = { 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000 }; lint count ( int n, int id ) { lint left, m, sum = 0; for ( int i = 1; i < 9; i++ ) { left = n / b[i] - (id==0); sum += left * b[i-1]; m = (n % b[i] - n % b[i-1]) / b[i-1]; //求出从第到高的第i位上的具体数字 if ( m > id ) sum += b[i-1]; else if ( m == id ) sum += n % b[i-1] + 1; if ( n < b[i] ) break; } return sum; } int main() { int x, y; while ( scanf("%d%d",&x,&y) && (x||y) ) { if ( x > y ) swap(x,y); for ( int i = 0; i <= 9; i++ ) printf("%I64d ",count(y,i)-count(x-1,i)); printf("\n"); } return 0; }
相关文章推荐
- poj 2282 The Counting Problem & 3286 How many 0's?
- POJ 2282 The Counting Problem & POJ 3286 How many 0's?(按位计算贡献)
- poj 2282 The Counting Problem && poj 3286 How many 0's? (数位dp)
- 统计区间[a,b]各个数字出现的个数:poj 2282 The Counting Problem poj 3286
- POJ 2282 The Counting Problem
- Poj 2282 The Counting Problem[统计区间 0 - 9出现的次数]
- poj 2282 The Counting Problem (数位DP)
- POJ 2282 The Counting Problem,组合数学
- (Realx 数论1.28)POJ 2282 The Counting Problem(区间统计数字:统计a、b之间各个数字(0~9)出现的次数)
- POJ 2282 && HDU 1663 The Counting Problem(数论)
- poj2282 The Counting Problem
- POJ 2282 The Counting Problem
- POJ2282:The Counting Problem(数位DP)
- POJ - 2282 - The Counting Problem - (计数问题)
- POJ 2282-The Counting Problem(组合数学_区间计数)
- [POJ] 2282 -> The Counting Problem
- POJ-2282-The Counting Problem
- POJ题目2282 The Counting Problem(数学)
- POJ 2282 The Counting Problem (数位dp)
- poj 2282 The Counting Problem 按位统计