CodeForce-803C Maximal GCD(贪心数学)
Maximal GCD
现在给定一个正整数 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(贪心数学)的更多相关文章
- 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 【构造】
You are given positive integer number n. You should create such strictly increasing sequence of k po ...
- CodeFoorces 803C Maximal GCD
枚举. 枚举$gcd$,然后计算剩下的那个数能不能分成$k$个递增的数. #include <iostream> #include <cstdio> #include < ...
- Codeforces 803C. Maximal GCD
题目链接:http://codeforces.com/contest/803/problem/C 中了若干trick之后才过... k个数的严格递增序列最小权值和就是${n*(n+1)/2}$,枚举这 ...
- 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 ...
- Codeforces H. Maximal GCD(贪心)
题目描述: H. Maximal GCD time limit per test 1 second memory limit per test 256 megabytes input standard ...
- 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 ...
随机推荐
- Linux - curl -w 参数详解
-w 的作用 完成请求传输后,使 curl 在 stdout 上显示自定义信息 格式是一个字符串,可以包含纯文本和任意数量的变量 输出格式 输出格式中的变量会被 curl 用对应的值替换掉 所有变量的 ...
- dubbo学习实践(4)之Springboot整合Dubbo及Hystrix服务熔断降级
1. springboot整合dubbo 在provider端,添加maven引入,修改pom.xml文件 引入springboot,版本:2.3.2.RELEASE,dubbo(org.apache ...
- 关于协议栈XDATA,内存溢出的小结
[第二部分的内容仅供参考,自己不是十分确定] ************************************************************** ************** ...
- repeatedly function in Clojure
user=> (doc repeatedly) clojure.core/repeatedly ([f] [n f]) Takes a function of no args, presumab ...
- springboot项目出现Whitelabel Error Page的问题
springboot项目出现Whitelabel Error Page的问题 大概就是这种情况,然而昨天还是没问题的,通过对比就发现,是自己手欠了 简单来说解决办法就是将springboot的启动项目 ...
- 【笔记】scikit-learn中的PCA(真实数据集)
sklearn中的PCA(真实的数据集) (在notebook中) 加载好需要的内容,手写数字数据集 import numpy as np import matplotlib.pyplot as pl ...
- XV6学习笔记(2) :内存管理
XV6学习笔记(2) :内存管理 在学习笔记1中,完成了对于pc启动和加载的过程.目前已经可以开始在c语言代码中运行了,而当前已经开启了分页模式,不过是两个4mb的大的内存页,而没有开启小的内存页.接 ...
- 剑指 Offer 61. 扑克牌中的顺子
剑指 Offer 61. 扑克牌中的顺子 从扑克牌中随机抽5张牌,判断是不是一个顺子,即这5张牌是不是连续的.2-10为数字本身,A为1,J为11,Q为12,K为13,而大.小王为 0 ,可以看成任意 ...
- NOIP 模拟 $17\; \rm weight$
题解 \(by\;zj\varphi\) 一道树剖的题 先对于原图求出一棵最小生成树,求出来的这棵树中的边定为树边,其它边叫非树边 那么对于一条非树边,它要成为最小生成树上的边,权值只能为连接它两个端 ...
- Mybatis的分页工具
配置拦截器插件 特别注意,新版拦截器是 com.github.pagehelper.PageInterceptor. com.github.pagehelper.PageHelper 现在是一个特殊的 ...