ural 1150. Page Numbers
1150. Page Numbers
Time limit: 1.0 second
Memory limit: 64 MB
Memory limit: 64 MB
John Smith has decided to number the pages in his notebook from 1 to N. Please, figure out the number of zeros, ones, twos, …, nines he might need.
Input
One number N (1 ≤ N < 109).
Output
Output 10 lines. The first line should contain the number of zeros needed, the second line should contain the number of ones needed, …, the tenth line should contain the number of nines needed.
Sample
input | output |
---|---|
12 |
1 5 2 1 1 1 1 1 1 1 |
Problem Author: Eugene Bryzgalov
Problem Source: Ural Collegiate Programming Contest, April 2001, Perm, English Round
Problem Source: Ural Collegiate Programming Contest, April 2001, Perm, English Round
Tags: none
Difficulty: 236
题意:1-n中各个数字 0-9各出现多少次
分析:简单的逐位计算+数学统计
1 #include <cstdio> 2 #include <cstring> 3 #include <cstdlib> 4 #include <cmath> 5 #include <deque> 6 #include <vector> 7 #include <queue> 8 #include <iostream> 9 #include <algorithm> 10 #include <map> 11 #include <set> 12 #include <ctime> 13 using namespace std; 14 typedef long long LL; 15 typedef double DB; 16 #define For(i, s, t) for(int i = (s); i <= (t); i++) 17 #define Ford(i, s, t) for(int i = (s); i >= (t); i--) 18 #define Rep(i, t) for(int i = (0); i < (t); i++) 19 #define Repn(i, t) for(int i = ((t)-1); i >= (0); i--) 20 #define rep(i, x, t) for(int i = (x); i < (t); i++) 21 #define MIT (2147483647) 22 #define INF (1000000001) 23 #define MLL (1000000000000000001LL) 24 #define sz(x) ((int) (x).size()) 25 #define clr(x, y) memset(x, y, sizeof(x)) 26 #define puf push_front 27 #define pub push_back 28 #define pof pop_front 29 #define pob pop_back 30 #define ft first 31 #define sd second 32 #define mk make_pair 33 inline void SetIO(string Name) { 34 string Input = Name+".in", 35 Output = Name+".out"; 36 freopen(Input.c_str(), "r", stdin), 37 freopen(Output.c_str(), "w", stdout); 38 } 39 40 inline int Getint() { 41 int Ret = 0; 42 char Ch = ' '; 43 while(!(Ch >= '0' && Ch <= '9')) Ch = getchar(); 44 while(Ch >= '0' && Ch <= '9') { 45 Ret = Ret*10+Ch-'0'; 46 Ch = getchar(); 47 } 48 return Ret; 49 } 50 51 const int N = 10; 52 int n; 53 LL Ans[N]; 54 55 inline void Input() { 56 scanf("%d", &n); 57 } 58 59 inline void Solve() { 60 LL x, y; 61 int Now = 1; 62 while(n/Now) { 63 x = n/(Now*10), y = n%Now; 64 int p = (n/Now)%10; 65 if(!p) Ans[0] += (x-1)*Now+y+1; 66 else Ans[0] += x*Now; 67 For(i, 1, p-1) Ans[i] += (x+1)*Now; 68 if(p) Ans[p] += x*Now+y+1; 69 For(i, p+1, 9) Ans[i] += x*Now; 70 Now *= 10; 71 //cout<<Ans[0]<<endl; 72 } 73 74 Rep(i, N) cout<<Ans[i]<<endl; 75 } 76 77 int main() { 78 #ifndef ONLINE_JUDGE 79 SetIO("A"); 80 #endif 81 Input(); 82 Solve(); 83 return 0; 84 }