CodeForces - 1373E Sum of Digits(贪心)
题目链接:点击查看
题目大意:设 f( x ) 为 x 的数位之和,给出一个 n 和一个 k ,求 的最小 x ,若不存在,输出 -1
题目分析:因为 n 和 k 比较小,所以可以打表,当 k 为 0 的时候,显然是 r + 99...999 是最优的,其中 r = n%9,当 k 为 1 的时候,打表的时间复杂度是 sqrt( 1e16 ) * 8 ,也就是将近 1e9 ,本地可能绷不住,所以可以打到 1e7 左右,然后自己找找规律补齐剩下的几项就好了,剩下的时间复杂度分别就是 ,之类的了,本地轻轻松松搞定
然后说一下正解,因为 k 最大只有 9 ,比较显然的一点就是,至多会进位一次,因为我们贪心的策略是较低位都放置 9 ,所以个位的进位会涉及到更高位的连续进位,这样我们不妨设我们最后构造出来的数为 r + 99...999 + 8 + 99..999 + x ,我们将需要构造的数分为了五段,因为说过了,为了贪心,所以低位需要尽量放 9 ,我们设 x 左边的第一堆 9 的个数为 num_9,又因为如果涉及进位的话,会导致连续进位,所以我们在 num_9 个 9 之前,放置了一个 8 ,到此停止连续进位,剩下的再按照当 k = 0 时贪心放置就好了
因为最多只有 1e16,换句话说 num_9 最多只有 16 ,而个位的 x 也最多只有 9 种取值,我们可以暴力枚举 x 和 num_9 ,然后计算此时贪心放置得到的最小值,就好了
代码:
#include<iostream>
#include<cstdio>
#include<string>
#include<ctime>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<stack>
#include<climits>
#include<queue>
#include<map>
#include<set>
#include<sstream>
#include<cassert>
using namespace std;typedef long long LL;typedef unsigned long long ull;const LL inf=0x3f3f3f3f3f3f3f3f;const int N=510;int n,k;LL cal(int last,int num_9)
{int num=n;for(int i=0;i<=k;i++){if(last+i<=9)num-=last+i+9*num_9;elsenum-=last+i-9;}if(num<0||num%(k+1)!=0)//如果剩下的数小于0了或者不足以恰好分为k+1份,则此时无解return inf;num/=k+1;LL ans=0;if(num<9)//按照k=0时贪心分配即可ans=num;else{num-=8;//别忘了需要放上一个8用来截断连续的9ans=num%9;for(int i=1;i<=num/9;i++)ans=ans*10+9;ans=ans*10+8;}for(int i=1;i<=num_9;i++)ans=ans*10+9;ans=ans*10+last;return ans;
}LL solve()
{LL ans=inf;for(int i=0;i<10;i++)//枚举个位的xfor(int j=0;j<=16;j++)//枚举x前有多少个9:num_9ans=min(ans,cal(i,j));if(ans==inf)ans=-1;return ans;
}int main()
{
#ifndef ONLINE_JUDGE
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
#endif
// ios::sync_with_stdio(false);int w;cin>>w;while(w--){scanf("%d%d",&n,&k);printf("%lld\n",solve());}return 0;
}
CodeForces - 1373E Sum of Digits(贪心)相关推荐
- codeforces E. Sum of Digits
这题可以暴力打本地表... 等正解出来再搞正解 对于k=0 我们让9尽量多就行 这个可以特判 对于k=1 暴力打表到1e9 对于k>=2 暴力打表到1e6(发现1e6以上就没有变化了) 当然这 ...
- codeforces 1060b Maximum Sum of Digits(思维题)
You are given a positive integer n Let S(x) be sum of digits in base 10 representation of x, for exa ...
- 【 CodeForces - 1060B 】Maximum Sum of Digits(思维,构造)
题干: You are given a positive integer nn. Let S(x)S(x) be sum of digits in base 10 representation of ...
- CF489C Given Length and Sum of Digits
题目: Given Length and Sum of Digits ,哈哈,我们今天来看一道比较简单的贪心题,虽然比较简单,但是如果不仔细的话很容易出错的额,这是选自codeforce 489C上的 ...
- codeforces Gym 100338E Numbers (贪心,实现)
题目:http://codeforces.com/gym/100338/attachments 贪心,每次枚举10的i次幂,除k后取余数r在用k-r补在10的幂上作为候选答案. #include< ...
- codeforces 85D. Sum of Medians
二次联通门 : codeforces 85D. Sum of Medians /*codeforces 85D. Sum of Medians正解线段树或是平衡树结果用vector暴力卡过去了 */ ...
- CodeForces - 1517A Sum of 2050
A. Sum of 2050 time limit per test1 second memory limit per test256 megabytes A number is called 205 ...
- codeforces 85D. Sum of Medians(线段树or分块)
题目链接:codeforces 85D. Sum of Medians 题意: add x 表示向集合中添加x(添加x的时候保证x是第一次被添加入集合) del x 表示从集合中删除x (删除x的时候 ...
- Codeforces 85D Sum of Medians(线段树)
题目链接:Codeforces 85D - Sum of Medians 题目大意:N个操作,add x:向集合中添加x:del x:删除集合中的x:sum:将集合排序后,将集合中所有下标i % 5 ...
最新文章
- BCH优于BCE+LN的5个理由
- 常用面试题(对应的知识点)
- 电子计算机 俗称 它是一种具有,2013年计算机一级B试题模拟1
- eclipse配置PHP自动提示代码
- 神经网络的激活函数、并通过python实现激活函数
- linux命令wget下载jdk(完整解决诸多异常)
- SQLAlchemy 多条件查询
- vim配置Nerdtree
- Kotlin — 适用于 Web 开发
- 实现SharePoint的无限潜力
- 在.net平台下,执行命令行
- AI界的革命!终于可以自动标注了!
- mate30pro鸿蒙系统刷机包下载,华为 Mate 30 Pro 的鸿蒙 2.0 Beta 版刷机包流出(附下载地址)...
- dp交换机命令_交换机常用指令总结
- excel 如何删除有颜色的行
- mysql column specified twice_Column 'box_id' specified twice 错误
- [转]杀毒软件的引擎
- [转]AndroidTolls国内镜像
- 【Unity3D】在Unity中实现UI指向箭头
- java开发速成班培训课程(1)