CodeForces 1060 B Maximum Sum of Digits
Maximum Sum of Digits
You are given a positive integer n.
Let S(x)S(x) be sum of digits in base 10 representation of xx , for example, S(123)=1+2+3=6S(123)=1+2+3=6 , S(0)=0S(0)=0 .
Your task is to find two integers a,ba,b , such that 0≤a,b≤n0≤a,b≤n , a+b=na+b=n and S(a)+S(b)S(a)+S(b) is the largest possible among all such pairs.
Input
The only line of input contains an integer nn (1≤n≤1012)(1≤n≤1012) .
Output
Print largest S(a)+S(b)S(a)+S(b) among all pairs of integers a,ba,b , such that 0≤a,b≤n0≤a,b≤n and a+b=na+b=n .
Examples
35
17
10000000000
91
Note
In the first example, you can choose, for example, a=17a=17 and b=18b=18 , so that S(17)+S(18)=1+7+1+8=17S(17)+S(18)=1+7+1+8=17 . It can be shown that it is impossible to get a larger answer.
In the second test example, you can choose, for example, a=5000000001a=5000000001 and b=4999999999b=4999999999 , with S(5000000001)+S(4999999999)=91S(5000000001)+S(4999999999)=91 . It can be shown that it is impossible to get a larger answer.
解题思路:
给出一个数字n,将他拆分为2个数字,使拆分的两个数字每一位相加的和最大,输出相加后的和。
个人感觉不用管下面提示。我们本着贪心的思想,希望获得尽可能多的9,就是将35,拆分为9与26,将10000000000,拆分为9999999999与1,既将原始数字拆分为比其第一位的全由9组成的数字与另一个补偿数字,补偿数字为原始数字减去拆分的全9数字。之后将拆分的两个数字所有位都相加便可以得到答案。
#include<iostream>
#include<sstream>
#include<string>
#include<cstdio>
//CodeForces不支持万能头文件bits/stdc++.h
using namespace std;
typedef long long ll;
string n;
ll power(int a, int b){ //快速幂
ll ans = ;
while(b){
if(b & ){
ans = ans * a;
}
a = a * a;
b >>= ;
}
return ans;
}
int main()
{
ll a, b;
while(cin >> n)
{
int suma=;
int sumb=;
a = power(, n.size() - );
//若想拆分出小于且9最多的数字,只需要找到n的位数n.size() - 1
//之后便可以用10的n.size() - 1次幂找到与n位数相等数字中最小数字
//减一便可以得到我们所要拆分的数字
istringstream cinn(n);
cinn >> b;
//先用istringstream读取n中的值输入到整形b中
//b - a就是补偿的数字。
a--;
b = b - a;
while(a) //将a的每一位加起来
{
suma += a % ;
a/=;
}
while(b)
{
sumb += b % ; //将b的每一位加起来
b/=;
}
cout << suma + sumb << endl; //所有位数加和
}
return ;
}
CodeForces 1060 B Maximum Sum of Digits的更多相关文章
- cf#513 B. Maximum Sum of Digits
B. Maximum Sum of Digits time limit per test 2 seconds memory limit per test 512 megabytes input sta ...
- Maximum Sum of Digits(CodeForces 1060B)
Description You are given a positive integer nn. Let S(x) be sum of digits in base 10 representation ...
- CF1060B:Maximum Sum of Digits
我对贪心的理解:https://www.cnblogs.com/AKMer/p/9776293.html 题目传送门:http://codeforces.com/problemset/problem/ ...
- Codeforces_B.Maximum Sum of Digits
http://codeforces.com/contest/1060/problem/B 题意:将n拆为a和b,让a+b=n且S(a)+S(b)最大,求最大的S(a)+S(b). 思路:考虑任意一个数 ...
- Codeforces Round #277.5 (Div. 2)-C. Given Length and Sum of Digits...
http://codeforces.com/problemset/problem/489/C C. Given Length and Sum of Digits... time limit per t ...
- CodeForces 489C Given Length and Sum of Digits... (贪心)
Given Length and Sum of Digits... 题目链接: http://acm.hust.edu.cn/vjudge/contest/121332#problem/F Descr ...
- Codeforces Round #277.5 (Div. 2)C——Given Length and Sum of Digits...
C. Given Length and Sum of Digits... time limit per test 1 second memory limit per test 256 megabyte ...
- codeforces#277.5 C. Given Length and Sum of Digits
C. Given Length and Sum of Digits... time limit per test 1 second memory limit per test 256 megabyte ...
- CodeForces 489C Given Length and Sum of Digits... (dfs)
C. Given Length and Sum of Digits... time limit per test 1 second memory limit per test 256 megabyte ...
随机推荐
- asp.net 类头部描述
这里教大家怎么在新建类的时候默认有头部描述,先看效果: 像这样的内容我们要怎么进行添加呢? 前方高能...... 找到VS的安装目录-->比如我自己的安装目录D:\VS2013\Common7\ ...
- C# Session操作
Session.Abandon();//清除全部Session//清除某个SessionSession["UserName"] = null;Session.Remove(&quo ...
- if、else if 、else及switch...case使用小记(C#)
有时候编程编的久了,如果不停下来认真思考一下,即便是一些最基础的知识点,也可能让自己懵圈.其实,说到底还是打基础的时候没打牢,或者说自以为是地认为自己懂了,然后在打基础的时候就懒得思考懒得看了,结果就 ...
- .Net Mvc5Filter与权限认证扩展
WebForm 在做WebForm的时候,如果我们要实现某页面登陆后才能访问,这个非常容易实现 public partial class IndexForm : Page { protected vo ...
- MvvmLight框架使用入门(三)
本篇是MvvmLight框架使用入门的第三篇.从本篇开始,所有代码将通过Windows 10的Universal App来演示.我们将创建一个Universal App并应用MvvmLight框架. ...
- 【题解】 洛谷P2340 奶牛会展
传送门 重新开始打代码Day1 第一眼看感觉不对啊,这道题目好像空间开不下,是不是不能dp... 后来想到了一个思路,他要求的是\(dp_{i,j,k}=j+k\),然后这样子不是很奇怪吗? 直接一维 ...
- 446. Arithmetic Slices II - Subsequence
A sequence of numbers is called arithmetic if it consists of at least three elements and if the diff ...
- STM32-RS485通信软硬件实现
OS:Windows 64 Development kit:MDK5.14 IDE:UV4 MCU:STM32F103C8T6/VET6 AD:Altium Designer 18.0.12 1.RS ...
- vue环境搭建简介
简单整理下vue的安装的新建项目 安装node.js和npm 参考其他教程 安装vue npm install vue 安装脚手架 vue-cli npm install --global vue-c ...
- [Alpha]Scrum Meeting#5
github 本次会议项目由PM召开,时间为4月7日晚上10点30分 时长10分钟 任务表格 人员 昨日工作 下一步工作 木鬼 撰写博客目录 整理清明开会记录 SiMrua 模型再训练(issue#1 ...