题目链接


题目大意:就是给你nnn和kkk然后再定义一个函数f(x)是十进制数x各个位数之和f(x)是十进制数x各个位数之和f(x)是十进制数x各个位数之和
叫你求出最小的x使得f(x)+f(x+1)+...+f(x+k)=n∣k∈[0,9],n∈[1,150]叫你求出最小的x使得f(x)+f(x+1)+...+f(x+k)=n|k\in[0,9],n\in[1,150]叫你求出最小的x使得f(x)+f(x+1)+...+f(x+k)=n∣k∈[0,9],n∈[1,150]


解题思路:首先我们发现两个规律:
1.就是x+kx+kx+k我们最多进位一次
2.你要是想进位的话你要取决俩个东西
1):就是你个位数的大小
2):就是你个位数之后9的个数

3.当上面两个确定之后高位就贪心选
4.还有一个细节就是这(k+1)(k+1)(k+1)个数的高位应该是一样的
5.那么我们就可以枚举个位数以及个位之后会9的个数


主函数

 int T;read(T);while(T --){cin >> n >> k;ll ans = 1e18;_for(i,0,17)//枚举9的个数_for(j,0,10)//枚举个位ans = min(ans,num(n,k,i,j));if(ans == 1e18) ans = -1;cout << ans << endl;}

首先我们先讲已经确定的各位数字之和先加上

ll num(int n, int k, int c9, int r) //函数头
{int res = 0;_for(i,0,k+1) {if (i + r < 10) {res += i + r + 9 * c9;} else {res += i + r - 9;}}

判断一下还剩下数的多少,因为后面的k+1个数的高位都是一样的

int need = n - res;if (need % (k + 1) || need < 0){return 1e18;}

贪心选取每一位

need /= k + 1;ll ans = 0;if (need < 9){ans = need;}else {need -= 8;int t = need % 9;ans = t;for (int i = 1; i <= need / 9; i++) {ans = ans * 10 + 9;}ans = ans * 10 + 8;}// 这里就是将高位和低位拼接起来for (int i = 1; i <= c9; i++) {ans = ans * 10 + 9;}ans = ans * 10 + r;return ans;
}

完整代码


#include <iostream>
#include <cstdio>
#include <stack>
#include <sstream>
#include <vector>
#include <map>
#include <cstring>
#include <deque>
#include <cmath>
#include <iomanip>
#include <queue>
#include <algorithm>
#include <set>
#define mid ((l + r) >> 1)
#define Lson rt << 1, l , mid
#define Rson rt << 1|1, mid + 1, r
#define ms(a,al) memset(a,al,sizeof(a))
#define log2(a) log(a)/log(2)
#define _for(i,a,b) for( int i = (a); i < (b); ++i)
#define _rep(i,a,b) for( int i = (a); i <= (b); ++i)
#define for_(i,a,b) for( int i = (a); i >= (b); -- i)
#define rep_(i,a,b) for( int i = (a); i > (b); -- i)
#define lowbit(x) ((-x) & x)
#define IOS std::ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define INF 0x3f3f3f3f
#define hash Hash
#define next Next
#define count Count
#define pb push_back
#define f first
#define s second
using namespace std;
const int N = 1e6+10, mod = 1e9 + 7;
const double eps = 1e-10;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PII;
template<typename T> void read(T &x)
{x = 0;char ch = getchar();ll f = 1;while(!isdigit(ch)){if(ch == '-')f*=-1;ch=getchar();}while(isdigit(ch)){x = x*10+ch-48;ch=getchar();}x*=f;
}
template<typename T, typename... Args> void read(T &first, Args& ... args)
{read(first);read(args...);
}
int n, k;
ll num(int n, int k, int c9, int r)
{int res = 0;_for(i,0,k+1) {if (i + r < 10) {res += i + r + 9 * c9;} else {res += i + r - 9;}}int need = n - res;if (need % (k + 1) || need < 0){return 1e18;}need /= k + 1;ll ans = 0;if (need < 9){ans = need;}else {need -= 8;int t = need % 9;ans = t;for (int i = 1; i <= need / 9; i++) {ans = ans * 10 + 9;}ans = ans * 10 + 8;}for (int i = 1; i <= c9; i++) {ans = ans * 10 + 9;}ans = ans * 10 + r;return ans;
}int main()
{int T;read(T);while(T --){cin >> n >> k;ll ans = 1e18;_for(i,0,17)_for(j,0,10)ans = min(ans,num(n,k,i,j));if(ans == 1e18) ans = -1;cout << ans << endl;}return 0;
}

贪心 ---- Educational Codeforces Round 90 (Rated for Div. 2)E. Sum of Digits[数位贡献+思维题+贪心]相关推荐

  1. 贪心 ---- Educational Codeforces Round 90 (Rated for Div. 2)D Maximum Sum on Even Positions[偶数子段最大和]

    题目链接 题目大意:给你一个序列你可以选择一个连续的子段将其反转,反转后使得偶数位置上的数字和最大 1.很明显我们可以看出反转的字符串的长度一定是偶数的,因为是奇数的话偶数位还是在偶数位不变所以没有用 ...

  2. Educational Codeforces Round 90 (Rated for Div. 2)(A, B, C, D, E)

    Educational Codeforces Round 90 (Rated for Div. 2) Donut Shops 思路 分三种情况: a==c/ba == c / ba==c/b这个时候两 ...

  3. Educational Codeforces Round 90 (Rated for Div. 2)(D 思维 E 打表)

    题目链接 D. Maximum Sum on Even Positions 题意:给你n长度的数组a  要求翻转 子区间  使得  偶数上的数之和尽量最大. 做法:用偶数位置减去奇数位置的值  就相当 ...

  4. C. Numbers on Whiteboard(模拟+贪心) Educational Codeforces Round 96 (Rated for Div. 2)

    原题链接: https://codeforces.com/contest/1430/problem/C 测试样例 input 1 4 output 2 2 4 3 3 3 1 题意: 给定一个 1 1 ...

  5. Educational Codeforces Round 90 (Rated for Div. 2)部分题解

    A - Donut Shops 题解: 1.我们首先特判一下包装盒里面只有一个物品的情况,如果只装了一个物品,那我们就直接比较这两个物品的单价即可. 2.如果盒子里面装的不止是一键物品,那么我们就需要 ...

  6. Educational Codeforces Round 90 (Rated for Div. 2) A. Donut Shops (1000)

    1373A - Donut Shops 题意&&思路: if(a<c)cout<<1; else cout<<-1;if(a*b>c)cout< ...

  7. Educational Codeforces Round 50 (Rated for Div. 2)的A、B、C三题AC代码

    A题链接:https://codeforces.com/contest/1036/problem/A A题AC代码: 1 #include <stdio.h> 2 #include < ...

  8. Educational Codeforces Round 75 (Rated for Div. 2) E2. Voting (Hard Version) 贪心

    传送门 文章目录 题意: 思路: 题意: n≤2e5,m≤n,p≤1e9n\le2e5,m\le n,p\le 1e9n≤2e5,m≤n,p≤1e9 思路: 首先需要发现一些性质,假设preipre_ ...

  9. Educational Codeforces Round 106 (Rated for Div. 2)(A ~ E)题解(每日训练 Day.16 )

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 目录 Educational Codeforces Round 106 (Rated for Div. ...

最新文章

  1. 怎么用php制作会员注册表单,帝国CMS前台会员登陆表单的制作教程
  2. matplotlib画图中文显示
  3. Python 通过all()判断列表(list)中所有元素是否都包含某个字符串(string)
  4. python的imread、newaxis
  5. Fedora换源:换成aliyun镜像源
  6. 餐饮水单打印软件_开发一款餐饮手机app系统软件什么价格?有哪些方面需要考虑?...
  7. cmd模式下如何从c盘转换到其他盘
  8. t–sql pl–sql_SQL Server处理器性能指标–第3部分–指示硬件组件问题的指标
  9. 决策树人工智能预测模型_部署和服务AI模型进行预测的10种方法
  10. c++ idea 插件_推荐 33 个 IDEA 最牛配置,写代码太爽了
  11. Weblogic的下载、安装、使用
  12. android开发骰子动画,Android实现微信摇骰子游戏
  13. 打印101-150之间的质数
  14. 自知、自胜、知足、强行,不失其所
  15. 算法:数组中寻找两个数字的和等于固定值
  16. .Net/C#: 一个将在线简体中文网页转为繁体中文页简单方法
  17. 基于C的α-β剪枝算法实现的AI五子棋游戏
  18. 语义分析——TEST编译器(3)
  19. python处理行列分明的txt文件
  20. 【面经】陌陌-2017年8月28日,散招实习生

热门文章

  1. matlab中clc,close,close all,clear,clear all作用区别
  2. 在 Google Colab 中使用 OpenCV 进行图像处理简介
  3. 实战:动手搭建一个开源动作相机
  4. 【从零学习OpenCV 4】Mat类介绍
  5. 黄聪:原生js的音频播放器,兼容pc端和移动端(原创)
  6. 优化:梯度下降法、牛顿法、共轭梯度法
  7. Linux Tomcat安装,Linux配置Tomcat,Linux Tomcat修改内存,Linux tomcat修改端口
  8. Luke 5—— 可视化 Lucene 索引查看工具,可以查看ES的索引
  9. 第十五章 动态规划——最优二叉搜索树
  10. Silverlight网站服务器端的配置