【33.33%】【codeforces 552B】Vanya and Books
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
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.
Examples
input
13
output
17
input
4
output
4
Note
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.
【题目链接】:http://codeforces.com/contest/552/problem/B
【题解】
1..9都是1个数字+9
10..99都是2个数字+(99-10+1)*2
100..999都是3个数字+….
字符串表示的数字为x,可以在字符串后面不断加数字9;组成的数字为y;然后记录当前加了几个9->len
则答案递增len*(y-x);
重复上述过程直到x<=数字n,且y>n;
然后剩下的数字都是和n的位数一样的.
则再递增(n-x)*len;
【完整代码】
#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define rei(x) scanf("%d",&x)
#define rel(x) scanf("%I64d",&x)
typedef pair<int,int> pii;
typedef pair<LL,LL> pll;
//const int MAXN = x;
const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
int n;
LL ans = 0;
string now;
LL cl(string x)
{
int len = x.size();
LL now = 0;
rep1(i,0,len-1)
now = now*10+x[i]-'0';
return now;
}
int main()
{
//freopen("F:\\rush.txt","r",stdin);
rei(n);
now = "0";
int len = 1;
LL pre = 0;
while (1)
{
LL key = cl(now+"9");
if (key <= n)
{
ans+=(key-pre)*len;
len++;
now+="9";
}
else
break;
pre = key;
}
if (pre==n)
cout << ans <<endl;
else
{
// cout << ans <<endl;
// cout << len<<endl;
// cout << pre<<endl;
ans+=len*(n-pre);
cout << ans<<endl;
}
return 0;
}
【33.33%】【codeforces 552B】Vanya and Books的更多相关文章
- 【 BowWow and the Timetable CodeForces - 1204A 】【思维】
题目链接 可以发现 十进制4 对应 二进制100 十进制16 对应 二进制10000 十进制64 对应 二进制1000000 可以发现每多两个零,4的次幂就增加1. 用string读入题目给定的二进制 ...
- 【23.33%】【codeforces 557B】Pasha and Tea
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【33.10%】【codeforces 604C】Alternative Thinking
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【25.33%】【codeforces 552D】Vanya and Triangles
time limit per test4 seconds memory limit per test512 megabytes inputstandard input outputstandard o ...
- 【33.33%】【codeforces 586D】Phillip and Trains
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【33.33%】【codeforces 608C】Chain Reaction
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【58.33%】【codeforces 747B】Mammoth's Genome Decoding
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【UOJ#33】【UR #2】树上GCD(长链剖分,分块)
[UOJ#33][UR #2]树上GCD(长链剖分,分块) 题面 UOJ 题解 首先不求恰好,改为求\(i\)的倍数的个数,最后容斥一下就可以解决了. 那么我们考虑枚举一个\(LCA\)位置,在其两棵 ...
- JAVA 基础编程练习题33 【程序 33 杨辉三角】
33 [程序 33 杨辉三角] 题目:打印出杨辉三角形(要求打印出 10 行如下图) 程序分析: 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 package ...
随机推荐
- C# winform利用反射和自定义特性加载功能模块(插件式开发)
由于在实际的工作中, 碰见这样的一个问题: 一个软件, 销售给A客户 他需要所有功能, 但是销售给B客户, 他只需要其中的一部分, 1.如果我们在实际的开发过程中, 没有把一些功能模块区分开来的话, ...
- 洛谷 P2026 求一次函数解析式
P2026 求一次函数解析式 题目背景 做数学寒假作业的怨念…… 题目描述 给定两个整点的坐标,求它们所在直线的函数解析式(一次函数). 输入输出格式 输入格式: 输入共两行. 第一行有两个整数x1, ...
- 在 Swift 專案中使用 Javascript:編寫一個將 Markdown 轉為 HTML 的編輯器
原文:Using JavaScript in Swift Projects: Building a Markdown to HTML Editor 作者:GABRIEL THEODOROPOULOS ...
- [Vue + TS] Create your own Decorators in Vue with TypeScript
We’ve used @Watch, @Inject and more decorators from vue-property-decorator. In this lesson however w ...
- 1.12 Python基础知识 - 序列:字符串
字符串是一个有序的字符集合,即字符序列.Pythpn内置数据类型str,用于字符串处理,使用单引号或双引号括起来的字符,就是字符常量,Python解释器会自动创建str型对象实例. 字符串的定义: 1 ...
- socket TCP简单通讯
socket 服务器 // // main.m // socket_server // // Created by lujunjie on 2016/11/23. // Copyright © 201 ...
- java hadoop file system API
org.apache.hadoop.fs Class FileSystem java.lang.Object org.apache.hadoop.fs.FileSystem All Implement ...
- Lintcode 将整数A转换为B
例子 如把31转换为14,须要改变2个bit位. ()10=()2 ()10=()2 贴代码 class Solution { public: /** *@param a, b: Two intege ...
- 关于Newtonsoft.json JsonConvert.DeserializeObject反序列化的使用
object obj = JsonConvert.DeserializeObject("{\"Sta\":3}", paramClass); //paramCl ...
- <meta name="viewport" content="width=device-width,initial-scale=1.0">
meta name="viewport" content="width=device-width,initial-scale=1.0" 解释 <meta ...