Codeforces H. Maximal GCD(贪心)
题目描述:
1 second
256 megabytes
standard input
standard output
You are given positive integer number n. You should create such strictly increasing sequence of k positive numbers a1, a2, ..., ak, that their sum is equal to n and greatest common divisor is maximal.
Greatest common divisor of sequence is maximum of such numbers that every element of sequence is divisible by them.
If there is no possible sequence then output -1.
The first line consists of two numbers n and k (1 ≤ n, k ≤ 1010).
If the answer exists then output k numbers — resulting sequence. Otherwise output -1. If there are multiple answers, print any of them.
6 3
1 2 3
8 2
2 6
5 3
-1
思路:
这个题是要给一个数n,一个数k,看能不能凑出一组含k个元素的严格递增数列,这组数的和为n,还要让这组数的最大公约数最大。
刚开始,a1+a2+...+ak=n>=a1+(a1+1)+(a1+2)+...+(ak+k-1),解得1<=a1<=(n-k*(k-1)/2)/k,既然要求公约数最大,把a的所有因数找出来,从大往小一个个试,
比如,找到一个f是a1的因数,既然要f为这组数的gcd,那f一定也是n的因数,检查一下,不是就跳过,是就n/f,变成找一个严格递增数列和为n/f,就不用考虑公因数了。
刚开始把过多的注意力放到了a1上,就把所有a1可能值一个个遍历,分别找因数,分别检查,结果是错的,而且程序逻辑及其混乱+复杂,还有好多要特判的地方(疯了我已经快)因为每一个a1都有因数1,最后对于较大的a1来说(从大往小检查)总会到1就停止,以为算出了最终结果。
所以后来把所有a的所有因数放到一个set里,也方便找最大值,从大往小遍历所有因数。就好了。。。吗?
结果超时。
仔细想想,怎么办。既然最后都是a1的因数f如果满足题目条件,那么它也是n的因数,那就把n的因数找出来就好了啊。
(٩(๑>◡<๑)۶),注意的是在枚举n的因数是不要忘了f要满足a1的上下界的限制,满足的才加到set中去。
但是但是,还要注意,一开始判断可不可能构造出这么一个数列,a1+a2+...+ak=n>=ka1+k*(k-1)/2>=k+k*(k-1)/2,以为完了?nonono,要注意题目数据范围,如果来个4*10^9这种,long long也存不下啊,那咋办,用double来存k吧...
|
Type |
Size |
数值范围 |
|
无值型void |
0 byte |
无值域 |
|
布尔型bool |
1 byte |
true false |
|
有符号短整型short [int] /signed short [int] |
2 byte |
-32768~32767 |
|
无符号短整型unsigned short [int] |
2 byte |
0~65535 |
|
有符号整型int /signed [int] |
4 byte |
-2147483648~2147483647 |
|
无符号整型unsigned [int] |
4 byte |
0~4294967295 |
|
有符号长整型long [int]/signed long [int] |
4 byte |
-2147483648~2147483647 |
|
无符号长整型unsigned long [int] |
4 byte |
0~4294967295 |
|
long long |
8 byte |
0~18446744073709552000 |
|
有符号字符型char/signed char |
1 byte |
-128~127 |
|
无符号字符型unsigned char |
1 byte |
0~255 |
|
宽字符型wchar_t (unsigned short.) |
2 byte |
0~65535 |
|
单精度浮点型float |
4 byte |
-3.4E-38~3.4E+38 |
|
双精度浮点型double |
8 byte |
1.7E-308~1.7E+308 |
|
long double |
8 byte |
(上表来源见下面博客)
知识点:贪心,数论(一点点)
代码:
#include <iostream>
#include <cmath>
#include <vector>
#include <set>
#include <algorithm>
using namespace std;
long long n;
double k;
set<long long> fac;
vector<long long> num;
int main()
{
cin >> n >> k;
if(k+k*(k-)/<=n)
{
long long uper1 = (n-k*(k-)/)/k;
long long lower1 = ;
//cout << uper1 << " " << lower1 << endl;
long long qz = ;
/*for(long long a = uper1; a>=lower1; a--)
{
for(long long i = 1; i<sqrt(a)+1; i++)
{
if(a%i==0)
{
fac.insert(i);
fac.insert(a/i);
}
}
}*/
for(long long i = ;i<sqrt(n)+;i++)
{
if(n%i==)
{
if(i<=uper1)
{
fac.insert(i);
}
if(n/i<=uper1)
{
fac.insert(n/i);
}
}
}
/*cout << "fac " << endl;
for(auto ite = fac.rbegin();ite!=fac.rend();ite++)
{
cout << *ite << " ";
}
cout << endl;*/
for(auto ite = fac.rbegin(); ite!=fac.rend(); ite++)
{
//cout << "ite " << *ite << endl;
int flag = ;
num.clear();
long long sum = n/(*ite)-;
int shu = ;
num.push_back(shu);
if(sum)
{
shu++;
while(shu<=sum&&num.size()<k-)
{
num.push_back(shu);
sum -= shu;
shu++; }
if(shu<=sum)
{
qz = *ite;
//cout << "qz " << qz << endl;
num.push_back(sum);
break;
}
}
else
{
qz = *ite;
break;
}
}
for(int i = ;i<num.size();i++)
{
cout << qz*num[i] << " ";
}
cout << endl;
}
else
{
cout << - << endl;
}
return ;
}
参考资料:
年少轻狂12138,C++基本数据类型大小及表示范围,https://blog.csdn.net/weixin_40709898/article/details/79368310(间接引用,因为上面的博客也是转载但找不到原博客)
Codeforces H. Maximal GCD(贪心)的更多相关文章
- CodeForce-803C Maximal GCD(贪心数学)
Maximal GCD CodeForces - 803C 现在给定一个正整数 n.你需要找到 k 个严格递增的正整数 a1, a2, ..., ak,满足他们的和等于 n 并且他们的最大公因数尽量大 ...
- codeforces 803C Maximal GCD(GCD数学)
Maximal GCD 题目链接:http://codeforces.com/contest/803/problem/C 题目大意: 给你n,k(1<=n,k<=1e10). 要你输出k个 ...
- Codeforces 803C. Maximal GCD 二分
C. Maximal GCD time limit per test: 1 second memory limit per test: 256 megabytes input: standard in ...
- 【数学】codeforces C. Maximal GCD
http://codeforces.com/contest/803/problem/C [题意] 给定两个数n,k(1 ≤ n, k ≤ 10^10) 要你输出k个数,满足以下条件: ①这k个数之和等 ...
- CodeForces - 803C Maximal GCD 【构造】
You are given positive integer number n. You should create such strictly increasing sequence of k po ...
- Codeforces 803C. Maximal GCD
题目链接:http://codeforces.com/contest/803/problem/C 中了若干trick之后才过... k个数的严格递增序列最小权值和就是${n*(n+1)/2}$,枚举这 ...
- Maximal GCD CodeForces - 803C (数论+思维优化)
C. Maximal GCD time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- AC日记——Maximal GCD codeforces 803c
803C - Maximal GCD 思路: 最大的公约数是n的因数: 然后看范围k<=10^10; 单是答案都会超时: 但是,仔细读题会发现,n必须不小于k*(k+1)/2: 所以,当k不小于 ...
- Educational Codeforces Round 20 C. Maximal GCD
C. Maximal GCD time limit per test 1 second memory limit per test 256 megabytes input standard input ...
随机推荐
- 【Java语言特性学习之一】设计模式
设计模式(Design pattern)是一套被反复使用.多数人知晓的.经过分类编目的.代码设计经验的总结.使用设计模式是为了可重用代码.让代码更容易被他人理解.保证代码可靠性. 毫无疑问,设计模式于 ...
- MySQL之表约束
MySQL表约束 约束是一种限制,它通过对表的行或者列的数据做出限制,来确保表数据的完整性和唯一性. 在mysql当中一般有一下这几种约束: 非空约束. 唯一约束. 主键约束. 自增长. 默认约束. ...
- python3黑帽子渗透笔记第二章--网络基础
1 先来看看不可少的socket模块 (1)tcp_client.py 在渗透测试过程中,创建一个tcp客户端连接服务,发送垃圾数据,进行模糊测试等. (2)udp_client.py 2 nc工具的 ...
- java学习笔记(5)-排序(1)
标签(空格分隔): 学习笔记 1. 冒泡 public class MaoPao{ public static void sort(int[] arr){ for(int i=arr.length-1 ...
- 「中山纪中集训省选组D4T1」折射伤害 高斯消元
题目描述 在一个游戏中有n个英雄,初始时每个英雄受到数值为ai的伤害,每个英雄都有一个技能"折射",即减少自己受到的伤害,并将这部分伤害分摊给其他人.对于每个折射关系,我们用数对\ ...
- Cookie 允许第三方cookie
这样本地调线上的接口,就可以使用线上接口生成的cookie了. 或者允许,或者增加白名单.
- springcloud学习的坑
一:启动Euerka作为提供者或者消费者时,启动失败报:Process finished with exit code 0 Unregistering application EUREKA-SERVI ...
- 【转帖】MIPS构架:曾经是英特尔的“眼中钉”
MIPS构架:曾经是英特尔的“眼中钉” https://www.eefocus.com/mcu-dsp/363953 <处理器史话>之十一 2016-06-17 08:02 作者:付丽华预 ...
- 【RocketMQ源码学习】- 4. Client 事务消息源码解析
介绍 > 基于4.5.2版本的源码 1. RocketMQ是从4.3.0版本开始支持事务消息的. 2. RocketMQ的消息队列能够保证生产端,执行数据和发送MQ消息事务一致性,而消费端的事务 ...
- Scrapy框架——介绍、安装、命令行创建,启动、项目目录结构介绍、Spiders文件夹详解(包括去重规则)、Selectors解析页面、Items、pipelines(自定义pipeline)、下载中间件(Downloader Middleware)、爬虫中间件、信号
一 介绍 Scrapy一个开源和协作的框架,其最初是为了页面抓取 (更确切来说, 网络抓取 )所设计的,使用它可以以快速.简单.可扩展的方式从网站中提取所需的数据.但目前Scrapy的用途十分广泛,可 ...