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. 在Eclipse中用JDBC连接Mysql数据库

    一.配置要求 JDK(下载http://www.oracle.com/technetwork/java/javase/downloads/index.html) Mysql(下载http://www. ...

  2. 影响Scala语言设计的因素列表

    Scala语言设计概述 Scala的设计受许多编程语言和研究思想的影响.事实上,仅很少的Scala的特点是全新的:大多数都已经被以另外的形式用在其他语言中了.Scala的革新主要来源于它是如何构造并放 ...

  3. Ruby迭代器(Iterator)

    简单的讲,一个迭代器就是一个能接受代码块的方法.当初为了进行迭代操作而设置了带块方法,现在很多时候仍然称它为迭带器. 可实际上,早期版本的 Ruby 将使用代码块的方法称为迭代器,因为它们就是被设计来 ...

  4. CentOS 6.5 and Ubuntu 14.04 使用外部邮箱发送邮件

    我们可以使用外部邮箱(163,126,gmail,139等等)为我们发邮件 for CentOS 6.5 yum -y install mailx vi /etc/mail.rc 在文件的末行添加以下 ...

  5. 4、linux-grep awk sed and cuf sort uniq join

    1.grep 1.1 grep [-invc] [--color=auto] '搜寻字符串' filename选项与参数:-i :忽略大小写的不同-n :顺便输出行号-v :显示没有 '搜寻字符串' ...

  6. sklearn常用数据的使用

    from sklearn import datasets from sklearn.linear_model import LinearRegression #加载数据 loaded_data = d ...

  7. \阶段4-独挡一面\项目-基于视频压缩的实时监控系统\Sprint2-采集端图像采集子系统设计

    1.在编写程序前有一个流程,思维导图: 初始化:包括初始化摄像头:注册事件到epoll 然后是开始启动采集:一旦开始采集我们的摄像头就会有数据了,它会触发事件处理函数:我们在这里的处理是保存这个图像: ...

  8. 【Qt官方例程学习笔记】Getting Started Programming with Qt Widgets

    创建一个QApplication对象,用于管理应用程序资源,它对于任何使用了Qt Widgets的程序都必要的.对于没有使用Qt Widgets 的GUI应用,可以使用QGuiApplication代 ...

  9. Oracle 11g 、 Oracle 11g select 、 PLSQL 、 Sql Server迁移助手(SSMA)6.0/7.1 网盘下载地址

    - - - - - - - - 链接: https://pan.baidu.com/s/1q-uwAfeLOPxzBBx6V1pYLg 提取码: hei9

  10. SQL Server列属性修改

    0.创建表 create table Users(Id int,Name nvarchar(32) not null,Phone nvarchar(16),Email nvarchar(128)) 1 ...