CodeForces - 803C Maximal GCD 【构造】
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.
Input
The first line consists of two numbers n and k (1 ≤ n, k ≤ 1010).
Output
If the answer exists then output k numbers — resulting sequence. Otherwise output -1. If there are multiple answers, print any of them.
Examples
6 3
1 2 3
8 2
2 6
5 3
-1
这题刚看一直以为数据有问题,1e10光输出就超时了啊
但是经过思考可以发现,当k * (k + 1) / 2 > n 时,直接输出-1. 所以其实k的范围不到1e5
题意是构造一个长度为k的严格递增的数组数组,要求这k个数组的最大公约数尽可能的大,且这k个数的和为n
解题思路:先解决数据范围的问题,我们设最大公约数为gcd,由严格递增可得,gcd取最小值为1,递增数组两数间的差取最小值1,则1 + 2 + 3 + …… + k <= n 否则就不存在,这样就缩小了数据范围
接下来是求出这个数组,既然k个数都是gcd的倍数,那么n也是gcd的倍数,所以最终答案的gcd一定是n的因子,那么我们可以通过循环1到sqrt(n)暴力找出最大的因子即可。(如果不知道为什么1到sqrt(n)就能找出所有因子,可以去学学筛选素数法。
附ac代码:
1 #include<iostream>
2 #include <cstdio>
3 #include <cmath>
4 #include <cstring>
5 #include <algorithm>
6 #include <string>
7 typedef long long ll;
8 using namespace std;
9 const int maxn = 1e6;
10 const int inf = 0x3f3f3f3f;
11 int nu[maxn];
12 int dis[maxn];
13 using namespace std;
14 int main()
15 {
16 ll n, k;
17 scanf("%lld %lld", &n, &k);
18
19 ll sum = k * (k + 1) / 2;
20 if(k + 1 > n * 2/ k) {
21 printf("-1");
22 return 0;
23 } else if(k == 1) {
24 printf("%lld", n);
25 return 0;
26 }
27 ll i = 0;
28 ll u = 0;
29 ll sqt = sqrt(n) + 1;
30 ll gcd = 0;
31 for(i = 1; i <= sqt; ++i) {
32 if(n % i == 0) {
33 if(i >= sum) {
34 gcd = n / i;
35 break;
36 } else if(n / i >= sum) {
37 gcd = i;
38 }
39 }
40 }
41 // printf("%lld i\n", i);
42 if(gcd == 0) printf("-1");
43 else {
44 for(ll j = 1; j <= k - 1; ++j) {
45 printf("%lld ", gcd * j);
46 n -= gcd * j;
47 }
48 printf("%lld", n);
49 }
50 return 0;
51 }
CodeForces - 803C Maximal GCD 【构造】的更多相关文章
- 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 803C. Maximal GCD
题目链接:http://codeforces.com/contest/803/problem/C 中了若干trick之后才过... k个数的严格递增序列最小权值和就是${n*(n+1)/2}$,枚举这 ...
- Codeforces H. Maximal GCD(贪心)
题目描述: H. Maximal GCD time limit per test 1 second memory limit per test 256 megabytes input standard ...
- CodeFoorces 803C Maximal GCD
枚举. 枚举$gcd$,然后计算剩下的那个数能不能分成$k$个递增的数. #include <iostream> #include <cstdio> #include < ...
- 【数学】codeforces C. Maximal GCD
http://codeforces.com/contest/803/problem/C [题意] 给定两个数n,k(1 ≤ n, k ≤ 10^10) 要你输出k个数,满足以下条件: ①这k个数之和等 ...
- AC日记——Maximal GCD codeforces 803c
803C - Maximal GCD 思路: 最大的公约数是n的因数: 然后看范围k<=10^10; 单是答案都会超时: 但是,仔细读题会发现,n必须不小于k*(k+1)/2: 所以,当k不小于 ...
- Maximal GCD CodeForces - 803C (数论+思维优化)
C. Maximal GCD time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- 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 ...
随机推荐
- pytest学习笔记(pytest框架结构)
一.pytest框架中使用setup.teardown.更灵活按照用例级别可以分为以下几类: 1.模块级:(setup_module.teardown_module)在模块始末调用 2.函数级:(se ...
- (013)每日SQL学习:日期的各种计算
1.确定两个日期之间的工作日天数 --确定两个日期之间的工作日天数with x0 as (select to_date('2018-01-01','yyyy-mm-dd') as 日期 from du ...
- .Net 5 C# 反射(Reflection)
这里有个目录 什么是反射 有什么用?怎么用? 获取类型的类型信息. 获取泛型信息 获取程序集的信息 从已加载的程序集获取 Type 对象 查看类的信息 首尾呼应.重复强调.重要事情说三遍 后记 什么是 ...
- php中两个函数可能导致的sql注入
sprintf https://www.php.net/manual/zh/function.sprintf.php 漏洞demo: <?php $name = addslashes($_GET ...
- Docker监控平台prometheus和grafana,监控redis,mysql,docker,服务器信息
Docker监控平台prometheus和grafana,监控redis,mysql,docker,服务器信息 一.通过redis_exporter监控redis 1.1 下载镜像 1.2 运行服务 ...
- Django(多表查询操作)
首先了解一下 mysql中的表之间的关系,一对一,一对多,多对一,多对多. 一对多关系.多对一关系.一对一关系 至少都有一侧是单个实体,所以记录之间的联系通过外键实现,让外键指向这个实体. 实现这种关 ...
- 飞塔5.4和5.6版本IPSec互备冗余测试
主电信.备联通:测试方法:修改诚盈的IPSec,将阶段一电信的对端地址改为错误的. 方法一: 通过静态路由的管理距离:电信设置为10:联通为15.经测试,可以实现自动切换,且电信恢复后 可以切换回电信 ...
- CCF-命令行选项(模拟)
命令行选项 问题描述 请你写一个命令行分析程序,用以分析给定的命令行里包含哪些选项.每个命令行由若干个字符串组成,它们之间恰好由一个空格分隔.这些字符串中的第一个为该命令行工具的名字,由小写字母组 ...
- Linux 配置永久辅助IP
1.什么是辅助IP 辅助IP来源于Linux之中,Linux的系统网卡可以支持多IP的绑定,而辅助IP多用于解耦解决服务之间的兼容性问题,常见的应用场景有: 虚拟IP,高可用飘逸: 永久临时IP解耦使 ...
- docker学习一
由于本人学习精力有限,不能像大学时那样,每个笔记都认真的手敲记录,一些地方会截图展示,并会添加自己的理解和备注,建议大家也做自己的笔记,看别人的可以,但是自己动手并思考记忆更深刻. 1.什么是虚拟化 ...