CodeForces - 552B Vanya and Books (数学阶乘)水

CodeForces - 552B
Vanya and Books
Time Limit: 1000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64u

Submit Status

Description

Vanya got an important task — he should enumerate books in the library and label each book with its number. Each of the n books should be assigned with a number from 1 to n. Naturally, distinct books should 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 integer n (1 ≤ n ≤ 109) — the number of books in the library.

Output

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

Sample Input

Input
13
Output
17
Input
4
Output
4

Hint

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

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

Source

Codeforces Round #308 (Div. 2)
//题意:
输入一个数字n,让你计算组成1--n这n个数总共需要几位数字
//Hait:这里不能用math头文件里的pow(n,m)这个函数,会出错,(这这块WA了5次)所以自己定义一个就行了
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
#include<math.h>
#define ll long long
using namespace std;
ll ppow(int n,int k)
{
	ll s=1;
	for(int i=1;i<=k;i++)
		s*=n;
	return s;
}
int main()
{
	ll n;
	while(scanf("%lld",&n)!=EOF)
	{
		ll sum=0;
		int nn=log10(n);
		sum=(nn+1)*(n-ppow(10,nn)+1);
		while(nn)
		{
			nn--;
			sum+=9*ppow(10,nn)*(nn+1);
		}
		printf("%lld\n",sum);
	}
	return 0;
}

你可能感兴趣的:(CodeForces - 552B Vanya and Books (数学阶乘)水)