bzoj1833 digit

这道题其实挺水,只是写的时候需要想清楚。我的方法是:

1.将[a,b]转化为[0,b+1)-[0,a)

2.预处理出非0的v在区间[0,10^p)出现次数以及0在区间[0,10^p)出现数

3.将一个区间再拆分为几段,如:

12345拆分为[0,10000),[10000,12000),[12000,12300),[12300,12340),[12340,12346)

下面是代码:

 1 #include<cstdio>
 2 using namespace std ;
 3
 4 static class digit {
 5     long long pow10 [ 20 ] ;
 6     long long cnt [ 20 ] ;
 7     long long cnt0 [ 20 ] ;
 8 public :
 9     digit () {
10         long long k = 1 ;
11         for ( int i = 0 ; i <= 18 ; ++ i ) {
12             pow10 [ i ] = k ;
13             k *= 10 ;
14         }
15         cnt0 [ 0 ] = 1 ;
16         for ( int i = 1 ; i <= 18 ; ++ i ) {
17             cnt [ i ] = pow10 [ i - 1 ] + cnt [ i - 1 ] * 10 ;
18             //printf ( "%lld\n" , cnt [ i ] ) ;
19             cnt0 [ i ] = cnt [ i - 1 ] * 9 + cnt0 [ i - 1 ] ;
20             //printf ( "%lld\n" , cnt0 [ i ] ) ;
21         }
22     }
23     long long operator () ( long long x , const int v ) {
24         x += 1 ;
25         int w = -1 ; while ( pow10 [ ++ w ] < x ) ;
26         int c = 0 ; long long ans = 0 ;
27         if ( v == 0 ) {
28             ans += cnt [ w ] * ( x / pow10 [ w ] - 1 ) + cnt0 [ w ] ;
29             x %= pow10 [ w -- ] ;
30         }
31         while ( x ) {
32             ans += c * ( x / pow10 [ w ] * pow10 [ w ] ) + cnt [ w ] * ( x / pow10 [ w ] ) + ( x / pow10 [ w ] > v ? pow10 [ w ] : 0 ) ;
33             c += ( x / pow10 [ w ] == v ) ;
34             x %= pow10 [ w -- ] ;
35         }
36         return ans ;
37     }
38 } DIGIT ;
39
40 long long a , b ;
41 int main () {
42     scanf ( "%lld%lld" , & a , & b ) ;
43     printf ( "%lld" , DIGIT ( b , 0 ) - DIGIT ( a - 1 , 0 ) ) ;
44     for ( int i = 1 ; i < 10 ; ++ i ) printf ( " %lld" , DIGIT ( b , i ) - DIGIT ( a - 1 , i ) ) ;
45     putchar ( ‘\n‘ ) ;
46     return 0 ;
47 }
时间: 2024-10-31 04:17:04

bzoj1833 digit的相关文章

LeetCode 233. Number of Digit One

Given an integer n, count the total number of digit 1 appearing in all non-negative integers less than or equal to n. For example:Given n = 13,Return 6, because digit 1 occurred in the following numbers: 1, 10, 11, 12, 13. 本来想凑一个关于阶乘的题目,做个笔记,不枉我昨天A八个

TOJ 2703: Cow Digit Game

2703: Cow Digit Game Time Limit(Common/Java):1000MS/10000MS     Memory Limit:65536KByte Total Submit: 1            Accepted:1 Description Bessie is playing a number game against Farmer John, and she wants you to help her achieve victory. Game i start

紫书第三章练习题:UVA 1225 Digit Counting by 15邱盼威

来源:http://m.blog.csdn.net/article/details?id=70861055 Trung is bored with his mathematicshomeworks. He takes a piece of chalk and starts writing a sequence ofconsecutive integers starting with 1 to N (1 < N < 10000). After that, hecounts the number

【bzoj1833】 ZJOI2010—count 数字计数

http://www.lydsy.com/JudgeOnline/problem.php?id=1833 (题目链接) 题意 求在${[a,b]}$范围内整数中,每个数码出现的次数. Solution 数位dp. ${t}$数组取到最大数时表示每一位是多少. ${f[i][j][k]}$表示第${i}$位,这一位上的数为${j}$,数字${k}$的出现次数.转移:$${f[i][j][k]=\sum_{l=0}^9f[i-1][l][k]+10^(i-1)}$$ ${g[i][k]}$表示第${

BZOJ1833 [ZJOI2010] count

[问题描述] 给定两个正整数a和b,求在[a,b]中的所有整数中,每个数码(digit)各出现了多少次. [输入格式] 输入文件中仅包含一行两个整数a.b,含义如上所述. [输出格式] 输出文件中包含一行10个整数,分别表示0-9在[a,b]中出现了多少次. [输入样例] 1 99 [输出样例] 9 20 20 20 20 20 20 20 20 20 [数据范围] 30%的数据中,a<=b<=10^6: 100%的数据中,a<=b<=10^12. 正解:数位DP 解题报告: 只要

&lt;hdu - 1600 - 1601&gt; Leftmost Digit &amp;&amp; Rightmost Digit 数学方法求取大位数单位数字

1060 - Leftmost Digit 1601 - Rightmost Digit 1060题意很简单,求n的n次方的值的最高位数,我们首先设一个数为a,则可以建立一个等式为n^n = a * 10^x;其中x也是未知的: 两边取log10有:lg(n^n) = lg(a * 10^x); 即:n * lg(n)  - x = lg(a); 现在就剩x一个变量了,我们知道x是值n^n的位数-1,a向下取整就是我们要求的数: 所以 按着上面的推导式翻译成代码就可以了(注意:数值的范围和之间的

HDU 1061 Rightmost Digit

Description Given a positive integer N, you should output the most right digit of N^N. Input The input contains several test cases. The first line of the input is a single integer T which is the number of test cases. T test cases follow. Each test ca

Arrange an Array to Form a Smallest Digit

/** * Input an array of positive integers, arrange the integers to form new digits, * and output the smallest digit among all the new ones. * Input Example 1: * {2, 1} * Output Example 1: * 12 * * Input Example 2: * {32, 321} * Output Example 2: * 32

Project Euler:Problem 90 Cube digit pairs

Each of the six faces on a cube has a different digit (0 to 9) written on it; the same is done to a second cube. By placing the two cubes side-by-side in different positions we can form a variety of 2-digit numbers. For example, the square number 64