https://www.hackerrank.com/contests/world-codesprint-6/challenges/bonetrousle

给定一个数n,和k个数,1--k这k个,要求选择b个数,使得这b个数的和等于n。

首先考虑最小值,在1--k中选择前b个数,是最小的,记为mi。最大值,后b个数相加,记为mx

注意到一个东西:如果mi <= n <= mx。那么是绝对可行的。因为mi总能增加1(同时保证满足要求),所以在这个区间里面的,都是可行解。

所以首先从mi开始枚举,每次把第B个数变成最大值k(第b - 1个数改成k - 1....依次类推,因为不能重复用数字)

则总能枚举到mi >= n。然后输出方案即可。

bug点:后b个数太大了,会爆LL。所以,当后面的数相加到 >= n的时候,够了,证明n是 <= mx的了,有可行解。

然后对着模拟即可。复杂度O(b)

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
const int maxn = 1e5 + ;
LL a[maxn];
void work () {
// ios::sync_with_stdio(false);
LL n, k, b;
// scanf ("%lld%lld%lld", &n, &k, &b);
cin >> n >> k >> b;
//cout << n << " " << k << " " << b << endl;
LL mi = , mx = ;
for (int i = ; i <= b; ++i) {
mi += i;
}
for (LL i = k; i >= k - b + ; --i) {
mx += i;
//if (mx < 0) while (1);
if (mx >= n) break; // ok
}
//cout << mi << " " << mx << endl;
if (mx < n) {
printf ("-1\n");
return ;
}
if (mi > n) {
printf ("-1\n");
return ;
}
LL now = mi;
int to = b;
LL get = k;
for (int i = ; i <= b; ++i) a[i] = i;
while (true) {
now = now - a[to] + get;
a[to] = get;
if (now >= n) {
LL cut = now - n;
a[to] -= cut;
for (int i = ; i <= b - ; ++i) {
printf ("%lld ", a[i]);
}
printf ("%lld\n", a[b]);
break;
}
to--;
get--;
}
} int main ()
{
#ifdef LOCAL
freopen("data.txt","r",stdin);
#endif
int t;
cin >> t;
// cout << t << endl;
while (t--) work ();
return ;
}

Bonetrousle HackerRank 数学 + 思维题的更多相关文章

  1. PJ考试可能会用到的数学思维题选讲-自学教程-自学笔记

    PJ考试可能会用到的数学思维题选讲 by Pleiades_Antares 是学弟学妹的讲义--然后一部分题目是我弄的一部分来源于洛谷用户@ 普及组的一些数学思维题,所以可能有点菜咯别怪我 OI中的数 ...

  2. 51Nod 1003 阶乘后面0的数量(数学,思维题)

    1003 阶乘后面0的数量 基准时间限制:1 秒 空间限制:131072 KB 分值: 5         难度:1级算法题 n的阶乘后面有多少个0? 6的阶乘 = 1*2*3*4*5*6 = 720 ...

  3. Gym 100801D Distribution in Metagonia (数学思维题)

    题目:传送门.(需要下载PDF) 题意:t组数据,每组数据给定一个数ni(1 ≤ ni ≤ 10^18),把ni拆成尽可能多的数,要求每个数的素因子只包含2和3,且这些数不能被彼此整除,输出一共能拆成 ...

  4. BZOJ4377 Kurs szybkiego czytania \ Luogu 3589[POI2015]KUR - 数学思维题

    Solution 我又双叒叕去看题解啦$QAQ$, 真的想不到鸭 输入 $a$ 和 $n$ 互质, 所以满足 $a \times i \ mod \ n$ $(0<=i<n)$ 肯定是不重 ...

  5. BZOJ4377[POI2015]Kurs szybkiego czytania——数学思维题

    题目描述 给定n,a,b,p,其中n,a互质.定义一个长度为n的01串c[0..n-1],其中c[i]==0当且仅当(ai+b) mod n < p.给定一个长为m的小01串,求出小串在大串中出 ...

  6. EOJ2018.10 月赛(B 数学+思维题)

    传送门:Problem B https://www.cnblogs.com/violet-acmer/p/9739115.html 题意: 找到最小的包含子序列a的序列s,并且序列s是 p -莫干山序 ...

  7. EOJ2018.10 月赛(A 数学+思维题)

    传送门:Problem A https://www.cnblogs.com/violet-acmer/p/9739115.html 题意: 能否通过横着排或竖着排将 1x p 的小姐姐填满 n x m ...

  8. zoj 2818 Root of the Problem(数学思维题)

    题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2818 题目描述: Given positive integer ...

  9. HDU5742 It's All In The Mind 数学思维题

    Problem Description Professor Zhang has a number sequence a1,a2,...,an. However, the sequence is not ...

随机推荐

  1. bzoj 2044 三维导弹拦截——DAG最小路径覆盖(二分图)

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2044 还以为是CDQ.发现自己不会三维以上的…… 第一问可以n^2.然后是求最长不下降子序列 ...

  2. Linux CentOS安装Azure Cli工具

    vim /etc/yum.repos.d/epel.repo [epel] name=epel baseurl=http://mirrors.sohu.com/fedora-epel/6/$basea ...

  3. Azure PIP (Instance Level Public IP)

    微软的Azure平台已经支持Instance Level Public IP功能.当有复杂协议的情况下,需要开启多个端口的情况下,可以考虑开启PIP功能. 先介绍几个概念: VIP – virtual ...

  4. 关于导入excel报错的处理(xls,xlsx)

    关于导入excel报错的处理(xls,xlsx) 最近在做一个将excel导入到dataGriview中的小功能在做的过程中遇到以下问题: 链接excel的链接串是这样写的 string strCon ...

  5. 请问两个div之间的上下距离怎么设置

    转自:https://zhidao.baidu.com/question/344630087.html 楼上说的是一种方法,yanzilisan183 <div style="marg ...

  6. R: which(查询位置)、%in% (是否存在)、ifelse(判断是否):

    ################################################### 问题:ifelse.which.%in%    18.4.27 解决方案: > x < ...

  7. C++ STL 的各结构实现

    C++ STL 的实现: 1.vector  底层数据结构为数组 ,支持快速随机访问 2.list    底层数据结构为双向链表,支持快速增删 3.deque   底层数据结构为一个中央控制器和多个缓 ...

  8. 自签名配置HTTPS

    基于AFN3.0 1.将后台提供的.cer文件文件保存至本地 2.在封装的网络请求工具类中为AFN的AFSecurityPolicy属性赋值 -(AFSecurityPolicy *)customSe ...

  9. 为什么Java的hash表的长度一直是2的指数次幂?为什么这个(hash&(h-1)=hash%h)位运算公式等价于取余运算?

    1.什么是hash表? 答:简单回答散列表,在hash结构散列(分散)存放的一种数据集结构. 2.如何散列排布,如何均匀排布? 答:取余运算 3.Java中如何实现? 答:hash&(h-1) ...

  10. Loadrunner监控服务器资源

    LoadRunner 加载监听服务器的步骤如下: 1.在 LoadRunner Controller 下,将工作面板切换到 Run状态,Available Graphs 栏 ,System Resou ...