Bonetrousle HackerRank 数学 + 思维题
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 数学 + 思维题的更多相关文章
- PJ考试可能会用到的数学思维题选讲-自学教程-自学笔记
PJ考试可能会用到的数学思维题选讲 by Pleiades_Antares 是学弟学妹的讲义--然后一部分题目是我弄的一部分来源于洛谷用户@ 普及组的一些数学思维题,所以可能有点菜咯别怪我 OI中的数 ...
- 51Nod 1003 阶乘后面0的数量(数学,思维题)
1003 阶乘后面0的数量 基准时间限制:1 秒 空间限制:131072 KB 分值: 5 难度:1级算法题 n的阶乘后面有多少个0? 6的阶乘 = 1*2*3*4*5*6 = 720 ...
- Gym 100801D Distribution in Metagonia (数学思维题)
题目:传送门.(需要下载PDF) 题意:t组数据,每组数据给定一个数ni(1 ≤ ni ≤ 10^18),把ni拆成尽可能多的数,要求每个数的素因子只包含2和3,且这些数不能被彼此整除,输出一共能拆成 ...
- BZOJ4377 Kurs szybkiego czytania \ Luogu 3589[POI2015]KUR - 数学思维题
Solution 我又双叒叕去看题解啦$QAQ$, 真的想不到鸭 输入 $a$ 和 $n$ 互质, 所以满足 $a \times i \ mod \ n$ $(0<=i<n)$ 肯定是不重 ...
- 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串,求出小串在大串中出 ...
- EOJ2018.10 月赛(B 数学+思维题)
传送门:Problem B https://www.cnblogs.com/violet-acmer/p/9739115.html 题意: 找到最小的包含子序列a的序列s,并且序列s是 p -莫干山序 ...
- EOJ2018.10 月赛(A 数学+思维题)
传送门:Problem A https://www.cnblogs.com/violet-acmer/p/9739115.html 题意: 能否通过横着排或竖着排将 1x p 的小姐姐填满 n x m ...
- zoj 2818 Root of the Problem(数学思维题)
题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2818 题目描述: Given positive integer ...
- HDU5742 It's All In The Mind 数学思维题
Problem Description Professor Zhang has a number sequence a1,a2,...,an. However, the sequence is not ...
随机推荐
- 如何在开启了log-bin的MySQL Server中创建FUNCTION
在MySQL主从复制机器的master的数据库中创建function,报出如下错误: Error Code: 1418. This function has none of DETERMINISTIC ...
- 广度优先搜索(BFS)
广度优先搜索算法(Breadth-First-Search),又译作宽度优先搜索,或横向优先搜索,简称BFS,是一种图形搜索算法.简单的说,BFS是从根节点开始,沿着树的宽度遍历树的节点.如果所有节点 ...
- mysql命令之一:mysql常用命令之一
一.登录 1.本地登录:MySQL 连接本地数据库,用户名为“root”,密码“123”(注意:“-p”和“123” 之间不能有空格) C:\>mysql -h localhost -u roo ...
- 给JZ2440开发板重新分区
转自:http://mp.weixin.qq.com/s?__biz=MzAxNTAyOTczMw==&mid=2649328035&idx=1&sn=7d3935cc05d3 ...
- jquery插件开发常用总结一
由于使用jquery插件后当form表单提交的时候,若发生错误,同时有验证错误文本时,即使用rules和message后,会自动生成一个label标签里面装有错误文件值. 我们可以替换它: 方式为:v ...
- Redis IRedisTypedClient
IRedisTypedClient IRedisTypedClient类相当于IRedicClient的强类型版,其方法与属性大多数与IRedisClient类似. 它支持在Redis中使用Linq查 ...
- qt数据库sql语句使用c++中的变量
void SerialWidget::on_btnMysql_clicked() { qDebug()<<QSqlDatabase::drivers()<<endl; /*列出 ...
- hive sql 查询 Child Error 错误追究
Diagnostic Messages for this Task: java.lang.Throwable: Child Error at org.apache.hadoop.map ...
- Mysql 增量备份和全量备份
全量备份: # vim /root/DBFullyBak.sh //添加以下内容 #!/bin/bash # Program # use mysqldump to Fully backup mysql ...
- C++11新标准
1. 新类型 long long和unsigned long long: char16_t 和 char32_t: 新增原始字符串: 2. 统一的初始化 C++11扩大了用大括号括起的列表(初始化列表 ...