B. Vanya and Books (Codeforces Round #308 (Div. 2 )),

Source: Internet
Author: User

B. Vanya and Books (Codeforces Round #308 (Div. 2 )),
B. Vanya and Bookstime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard output

Vanya got an important task-he shocould enumerate books in the library and label each book with its number. Each ofNBooks shoshould be assigned with a number from 1N. Naturally, distinct books shoshould be assigned distinct numbers.

Vanya wants to know how many digits he will have to write down as he labels the books.

Input

The first line contains integerN(1 digit ≤ DigitNBandwidth ≤ limit 109)-the number of books in the library.

Output

Print the number of digits needed to number all the books.

Sample test (s) input
13
Output
17
Input
4
Output
4
Note

Note to the first test. the books get numbers 1, clerk 2, clerk 3, clerk 4, clerk 5, clerk 6, clerk 7, clerk 8, clerk 9, clerk 10, clerk 11, clerk 12, listen 13, which totals to 17 digits.

Note to the second sample. The books get numbers 1, numbers 2, numbers 3, numbers 4, which totals to 4 digits.


Question: Give you an integer n and ask how many digits are there from 1 to n. For example, n = 13, 1, listen 2, listen 3, listen 4, Listen 5, listen 6, listen 7, listen 8, then 9, then 10, then 11, then 12, listen 13 has 17 digits in total.

F [I] indicates from 1 10i The total number of digits. For example, from 753 to 100 is three digits, so the answer is f [2] + 753*3.

Reprinted, please specify the Source: search for & STAR kids

Link: http://codeforces.com/contest/552/problem/ B


#include <stdio.h>#include <string.h>#include <algorithm>#include<string>#include<map>#define LL __int64using namespace std;const int MAXN = 1005;LL n;LL ans = 0;LL a[12]= {0,9,99,999,9999,99999,999999,9999999,99999999,999999999,9999999999};int main(){    while(~scanf("%I64d",&n))    {        ans = 0;        LL i,j,k = 1;        if(n<10)        {            printf("%I64d\n",n);            return 0;        }        for(i = 1; i<=10; i++)        {            k *= 10;            if(n<k)            {                ans+=(n-k/10+1)*i;              //   printf("%I64d %I64d %I64d\n",i,ans,(n-k/10+1)*i);                break;            }           /* else if(n==k)            {                ans+=(i+1);                break;            }*/            else            {                ans+=(a[i]-a[i-1])*i;            }            // printf("%d %I64d\n",i,ans);        }        printf("%I64d\n",ans);    }    return 0;}
Or

#include <bits/stdc++.h>using namespace std;int n;long long f[12];long long t[12];int main() {    cin >> n;    t[1] = 10;    for (int i = 2; i <= 10; i++)        t[i] = t[i - 1] * 10;    f[1] = 11;    for (int i = 2; i <= 10; i++)        f[i] = f[i - 1] + (t[i] - t[i - 1]) * i + 1;    long long ans;    long long base;    for (int i = 0; i <= n; i++) {        if (t[i] > n) {            base = i - 1;            break;        }    }    ans = f[base] + (n - t[base]) * (base + 1);    cout << ans << endl;    return 0;}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.