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 of xx, for example, S(123)=1+2+3=6, S(0)=0.
Your task is to find two integers a,ba,b, such that 0≤a,b≤n, a+b=n and 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).
Output
Print largest S(a)+S(b) among all pairs of integers a,ba,b, such that 0≤a,b≤n and a+b=n.
Sample Input
35
17
10000000000
91
Hint
In the first example, you can choose, for example, a=17 and b=18, so that S(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=5000000001 and b=4999999999, with S(5000000001)+S(4999999999)=91. It can be shown that it is impossible to get a larger answer.
相信不少人wa是这个代码
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<stack>
#include<vector>
#include<queue>
#include<set>
#include<algorithm>
#define max(a,b) (a>b?a:b)
#define min(a,b) (a<b?a:b)
#define swap(a,b) (a=a+b,b=a-b,a=a-b)
#define maxn 320007
#define N 100000000
#define INF 0x3f3f3f3f
#define mod 1000000009
#define e 2.718281828459045
#define eps 1.0e18
#define PI acos(-1)
#define lowbit(x) (x&(-x))
#define read(x) scanf("%d",&x)
#define put(x) printf("%d\n",x)
#define memset(x,y) memset(x,y,sizeof(x))
#define Debug(x) cout<<x<<" "<<endl
#define lson i << 1,l,m
#define rson i << 1 | 1,m + 1,r
#define ll long long
//std::ios::sync_with_stdio(false);
//cin.tie(NULL);
using namespace std; int main()
{
ll n,a,b;
cin>>n;
ll m=n,k=;
while(m>=k)
{
k=k*+;
}
k/=;
//cout<<k<<endl;
a=k;
b=n-k;
ll sum=;
while(a)
{
sum+=a%;
a/=;
}
while(b)
{
sum+=b%;
b/=;
}
cout<<sum<<endl;
return ;
}
你们可以尝试一下123这个样例,输出为15,但正确答案为24(99+24)。
AC代码如下:
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<stack>
#include<vector>
#include<queue>
#include<set>
#include<algorithm>
#define max(a,b) (a>b?a:b)
#define min(a,b) (a<b?a:b)
#define swap(a,b) (a=a+b,b=a-b,a=a-b)
#define maxn 320007
#define N 100000000
#define INF 0x3f3f3f3f
#define mod 1000000009
#define e 2.718281828459045
#define eps 1.0e18
#define PI acos(-1)
#define lowbit(x) (x&(-x))
#define read(x) scanf("%d",&x)
#define put(x) printf("%d\n",x)
#define memset(x,y) memset(x,y,sizeof(x))
#define Debug(x) cout<<x<<" "<<endl
#define lson i << 1,l,m
#define rson i << 1 | 1,m + 1,r
#define ll long long
//std::ios::sync_with_stdio(false);
//cin.tie(NULL);
using namespace std; int main()
{
ll n,a,b;
cin>>n;
ll m=n,k=;
while(m>=k)
{
k=k*+;
}
k/=;
//cout<<k<<endl;
a=k;
b=n-k;
ll sum=;
while(a)
{
sum+=a%;
a/=;
}
while(b)
{
sum+=b%;
b/=;
}
cout<<sum<<endl;
return ;
}
Maximum Sum of Digits(CodeForces 1060B)的更多相关文章
- 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 r ...
- (CodeForces - 5C)Longest Regular Bracket Sequence(dp+栈)(最长连续括号模板)
(CodeForces - 5C)Longest Regular Bracket Sequence time limit per test:2 seconds memory limit per tes ...
- 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 ...
- Sorted Adjacent Differences(CodeForces - 1339B)【思维+贪心】
B - Sorted Adjacent Differences(CodeForces - 1339B) 题目链接 算法 思维+贪心 时间复杂度O(nlogn) 1.这道题的题意主要就是让你对一个数组进 ...
- 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 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 ...
- [题解]Yet Another Subarray Problem-DP 、思维(codeforces 1197D)
题目链接:https://codeforces.com/problemset/problem/1197/D 题意: 给你一个序列,求一个子序列 a[l]~a[r] 使得该子序列的 sum(l,r)-k ...
- (CodeForces 558C) CodeForces 558C
题目链接:http://codeforces.com/problemset/problem/558/C 题意:给出n个数,让你通过下面两种操作,把它们转换为同一个数.求最少的操作数. 1.ai = a ...
- HDU 3415 Max Sum of Max-K-sub-sequence(单调队列)
转载请注明出处:http://blog.csdn.net/u012860063 Max Sum of Max-K-sub-sequence Time Limit: 2000/1000 MS (Java ...
随机推荐
- Oracle SQL语句之常见优化方法总结--不定更新
1.SQL语句尽量用大写的: 因为oracle总是先解析SQL语句,把小写的字母转换成大写的再执行. 2.WHERE子句中的连接顺序: ORACLE采用自下而上的顺序解析WHERE子句,根据这个原理, ...
- 误删除(重命名)libc.so.6的处理方法
LD_PRELOAD=/lib64/libc-2.12.so ln -s /lib64/libc-2.12.so /lib64/libc.so.6 ssh尚未断开的话可以直接这样操作,断开之后可以用l ...
- Guitar Por如何演奏刮弦
每当我们听到吉他现场演出的时候,看到吉他手在激烈的刮弦时,都觉得很酷,非常有感染力.刮弦在我们弹吉他或编曲时,会经常用到,虽然时间很短,但会为你加分不少. 那么我们应该如何演奏刮弦呢,我们先用E5和弦 ...
- Python脚本备份
#!/usr/bin/python3 # -*- coding:utf- -*- # 保证源程序可以输入汉字 print bool([]) # 任何为零的数字或空集(空列表.空元组和空字典等)均为Fa ...
- (转)Extracting knowledge from knowledge graphs using Facebook Pytorch BigGraph.
Extracting knowledge from knowledge graphs using Facebook Pytorch BigGraph 2019-04-27 09:33:58 This ...
- mongodb 数据自动备份
创建Mongodb数据库备份目录 mkdir -p ~/backup/mongod_bak/mongod_bak_now mkdir -p ~/backup/mongod_bak/mongod_bak ...
- SAP 成套销售&按项目销售
http://blog.sina.com.cn/s/blog_95ac31e30102x5we.html 分类: SAP_SD SAP 成套销售&按项目销售 一.业务简介 成套销售(KIT ...
- [mysql]You must reset your password using ALTER USER statement before executing this statement.
原因分析: MySQL版本5.6.6版本起,添加了password_expired功能,它允许设置用户的过期时间.这个特性已经添加到mysql.user数据表,但是它的默认值是”N”,可以使用ALTE ...
- 2019 To do List
做好测试不是靠编程,而是靠的是严禁的作风,慎密的逻辑思维,适合的测试流程. 内心有些迷茫的时候,迷茫的是作为测试既然要学那么多编程,为什么不直接去干开发呢?学了编程,用不上,到底有什么用呢? 看了这句 ...
- CentOS7.X中使用yum安装nginx的方法
nginx官方文档说明:http://nginx.org/en/linux_packages.html#RHEL-CentOS 一.安装前准备: yum install yum-utils 二.添加源 ...