BZOJ1833: [ZJOI2010]count 数字计数

来源:互联网 发布:工控机编程用什么语言 编辑:程序博客网 时间:2024/04/28 17:45

Description

给定两个正整数a和b,求在[a,b]中的所有整数中,每个数码(digit)各出现了多少次。

Input

输入文件中仅包含一行两个整数a、b,含义如上所述。

Output

输出文件中包含一行10个整数,分别表示0-9在[a,b]中出现了多少次。

Sample Input

1 99

Sample Output

9 20 20 20 20 20 20 20 20 20

HINT

30%的数据中,a<=b<=10^6;
100%的数据中,a<=b<=10^12。

Source

大概只有我这种蒟蒻才要做这题想很久了
数位dp
dp[i]表示考虑前导0每种数字的出现次数
然后直接统计答案即可
代码里有注释
注意考虑最高位无前导0
#include <bits/stdc++.h>using namespace std;long long table[15], ans[10], dp[15], l, r;int num[15], m;inline void getdp(long long n, int flag){if( !n ) return ;long long ret = n;m = 0;while( n ) num[ ++m ] = n % 10, n /= 10;//位数<m for( int i = 1 ; i < m ; i++ )for( int j = 0 ; j <= 9 ; j++ )ans[ j ] += flag * ( 9 * dp[ i - 1 ] + ( ( !j ) ? 0 : table[ i - 1 ] ) );//位数=m for( int i = m ; i ; i -- ){ret -= num[ i ] * table[ i - 1 ];//最高位不顶上界 最高位 for( int j = ( i == m ) ; j < num[ i ] ; j++ ) ans[ j ] += table[ i - 1 ] * flag;//最高位不顶上界 其他位 for( int j = 0 ; j <= 9 ; j++ ) ans[ j ] += dp[ i - 1 ] * ( num[ i ] - ( i == m ) ) * flag;//最高位顶上界 最高位ans[ num[ i ] ] += ( ret + 1 ) * flag; }}int main(){table[ 0 ] = 1;for( int i = 1 ; i < 15 ; i++ )dp[ i ] = dp[ i - 1 ] * 10 + table[ i - 1 ], table[ i ] = table[ i - 1 ] * 10;cin >> l >> r;getdp( r, 1 );getdp( l - 1, -1 );for( int i = 0 ; i < 9 ; i++ ) printf( "%lld ", ans[ i ] );return printf( "%lld\n", ans[ 9 ] ), 0;}


0 0
原创粉丝点击