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 ...
随机推荐
- C# 请求被中止: 未能创建 SSL/TLS 安全通道。 设置SecurityProtocol无效
今天为了获取一张图片,用了一段代码: ServicePointManager.ServerCertificateValidationCallback += new RemoteCertificateV ...
- JVM虚拟机基础
JVM 全称Java Virtual Machine,也就是我们耳熟能详的Java 虚拟机.它能识别.class 后缀的文件,并且能够解析它的指令,最终调用操作系统上的函数,完成我们想要的操作. Ja ...
- 解决Python内CvCapture视频文件格式不支持问题
解决Python内CvCapture视频文件格式不支持问题 在读取视频文件调用默认的摄像头cv.VideoCapture(0)会出现下面的视频格式问题 CvCapture_MSMF::initStre ...
- 两种方式,花五分钟就能构建一个 Spring Boot 应用
前言 Spring Boot 的好处自然不必多说,对于想要从事 Java 工作的朋友们来说,可谓是必学的技能. 在我看来,它的优势就是多快好省. 功能多,很多常用的能力都有集成: 接入快,简单的几行代 ...
- CSRF Cross-site request forgery 跨站请求伪造
跨站请求伪造目标站---无知用户---恶意站 http://fallensnow-jack.blogspot.com/2011/08/webgoat-csrf.html https://wiki.ca ...
- 网页开发(HTML 基础)
网页的标准是W3C,目前模式是HTML.CSS和JavaScript. HTML,全称"Hyper Text Markup Language(超文本标记语言)",简单来说,网页就是 ...
- 云服务器镜像问题("Couldn't resolve host 'mirrors.tencentyun.com')
云服务器镜像问题("Couldn't resolve host 'mirrors.tencentyun.com') 原因: 腾讯云服务器内网yum源的域名 mirrors.tencentyu ...
- 我们到底为什么要用 IoC 和 AOP
作为一名 Java 开发,对 Spring 框架是再熟悉不过的了.Spring 支持的控制反转(Inversion of Control,缩写为IoC)和面向切面编程(Aspect-oriented ...
- 【机制】js的闭包、执行上下文、作用域链
1.从闭包说起 什么是闭包 一个函数和对其周围状态(词法环境)的引用捆绑在一起,这样的组合就是闭包. 也就是说,闭包让你可以在一个内层函数中访问到其外层函数的作用域. 在 JavaScript 中,每 ...
- IDLE怎么修改背景?
摘要:IDLE默认为白色,可能有的人喜欢其他颜色,那么怎么修改呢? 颜色喜好,因人而异.不想千篇一律使用默认的白色,可以通过以下操作修改IDLE的背景颜色以及其他设置. 打开Python官方自带的ID ...