Maximal GCD

CodeForces - 803C

现在给定一个正整数 n。你需要找到 k 个严格递增的正整数 a1, a2, ..., ak,满足他们的和等于 n 并且他们的最大公因数尽量大。

如果不可能请输出 -1。

这k个数的gcd的必定是n的因数,于是变成枚举n的因数,可以知道只需要枚举 1~sqrt(n) 范围内满足 n%i==0 的因数就行,复杂度就变成10的5次方了。然后贪心,要使得递增序列的公因子最大,先从大到小枚举因数 n/i ,没找到满足的因数再从小到大枚举因数 i。若当前枚举的gcd为x,若1x+2x+3x+....+kx<=n,那么必能把n分成k个递增的x的倍数。

#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
long long n,k;
bool check(long long x)
{
//cout<<x<<endl;
if(x>n*2/k/(k+1))
return false;
for(int i = 1;i<=k-1;i++)
{
printf("%lld ",x*i);
n -= x*i;
}
printf("%lld",n);
return true;
}
int main()
{
cin>>n>>k;
for(int i = 1;i<=sqrt(n)+1;i++)
{
if(n%i)
continue;
if(check(n/i))
return 0;
}
for(int i = sqrt(n)+1;i>=1;i--)
{
if(n%i)
continue;
if(check(i))
return 0;
}
printf("-1");
return 0;
}
 

CodeForce-803C Maximal GCD(贪心数学)的更多相关文章

  1. codeforces 803C Maximal GCD(GCD数学)

    Maximal GCD 题目链接:http://codeforces.com/contest/803/problem/C 题目大意: 给你n,k(1<=n,k<=1e10). 要你输出k个 ...

  2. Codeforces 803C. Maximal GCD 二分

    C. Maximal GCD time limit per test: 1 second memory limit per test: 256 megabytes input: standard in ...

  3. CodeForces - 803C Maximal GCD 【构造】

    You are given positive integer number n. You should create such strictly increasing sequence of k po ...

  4. CodeFoorces 803C Maximal GCD

    枚举. 枚举$gcd$,然后计算剩下的那个数能不能分成$k$个递增的数. #include <iostream> #include <cstdio> #include < ...

  5. Codeforces 803C. Maximal GCD

    题目链接:http://codeforces.com/contest/803/problem/C 中了若干trick之后才过... k个数的严格递增序列最小权值和就是${n*(n+1)/2}$,枚举这 ...

  6. AC日记——Maximal GCD codeforces 803c

    803C - Maximal GCD 思路: 最大的公约数是n的因数: 然后看范围k<=10^10; 单是答案都会超时: 但是,仔细读题会发现,n必须不小于k*(k+1)/2: 所以,当k不小于 ...

  7. Maximal GCD CodeForces - 803C (数论+思维优化)

    C. Maximal GCD time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  8. Codeforces H. Maximal GCD(贪心)

    题目描述: H. Maximal GCD time limit per test 1 second memory limit per test 256 megabytes input standard ...

  9. 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 ...

随机推荐

  1. Java中解决多线程数据安全问题

    同步代码块 基本语句 synchronized (任意对象) { 操作共享代码 } 代码示例 public class SellTicket implements Runnable { private ...

  2. vue源码解析之响应式原理

    关于defineReactive等使用细节需要自行了解 一些关键知识点 $mount时 会 new Watcher 把组件的 updateComponent 方法传给watcher 作为getter ...

  3. S3C2440—5.UART的使用

    文章目录 一.S3C2440中的UART介绍 1.1 电平匹配 1.2 UART数据帧与波特率 1.3UART框图 二.UART的配置 2.1 UART引脚的配置 2.2 波特率的配置 2.3 数据帧 ...

  4. SpringBoot监听redis过期key

    开启过期监听 vim /etc/redis.conf 取消notify-keyspace-events Elg的注释 pom.xml 添加: <dependency> <groupI ...

  5. docker 安装部署 jenkins

    cd /data/docker-data/jenkins mkdir jenkins_home chmod 777 jenkins_home docker run -d -p 10240:8080 - ...

  6. QT 资源文件的添加

  7. 六:使用Cookie进行会话管理

    1.存储客户端的状态 因为Http协议是无状态的,也就是说每个客户访问服务器端资源时,服务器并不知道该客户端是谁,所以需要会话技术识别客户端的状态.会话技术是帮助服务器 记住客户端状态 2.会话技术 ...

  8. web整合Spring和Hibernate

    上一篇是简单整合web和Spring, 这一篇是整合hibernate: 连接池c3p0: spring5.0, hibernate5.0 jars: ------------------------ ...

  9. Scan error on column index 1, name “created_at“: unsupported Scan, storing driver.Value type []uint8

    使用gorm,出现以下报错 在连接数据库时加上: parseTime=True db, err = gorm.Open(utils.Db, fmt.Sprintf("%s:%s@(%s:%s ...

  10. 在Raspberry Pi 3B+上安装Windows 10 IoT

    下载 进入树莓派下载页面,当前网址https://www.raspberrypi.org/downloads/ 选择Windows 10 IoT Core,当前网址https://docs.micro ...