Codeforces 803C. 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
题意:输出递增的k个数,使得他们的和等于n并且它们的GCD最大。 不存在输出-1。
思路:二分n的约数。(1+k)*k<=n是才存在k个符合要求的数,否者输出-1。
代码:
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
const int maxn=2e5+,inf=0x3f3f3f3f,mod=1e9+;
const ll MAXN=1e13+;
ll n,k,sum;
ll sign[maxn];
int check(ll q)
{
if(sum*q<=n) return ;
else return ;
}
int main()
{
scanf("%lld%lld",&n,&k);
if(k>=1e6)
{
cout<<-<<endl;
return ;
}
sum=(+k)*k/;
if(sum>n)
{
cout<<-<<endl;
return ;
}
ll l=,r=,q=;
for(ll i=; i*i<=n; i++)
if(n%i==) sign[++r]=i,sign[++r]=n/i;
sort(sign+,sign+r+);
while(l<=r)
{
ll mid=(l+r)/;
if(check(sign[mid])) l=mid+,q=sign[mid];
else r=mid-;
}
ll cou=;
for(int i=; i<k; i++)
{
cout<<q*i<<" ";
cou+=q*i;
}
cout<<n-cou<<endl;
return ;
}
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 【构造】
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}$,枚举这 ...
- 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 ...
随机推荐
- is 和 as 运算符
is和as运算符:is是判断是否是某个类型,返回true或falseo as Ren: 如果转换成功了,没问题:as 是用来转换如果没转换成功,不会报出错误,而是返回一个null值 例 实例化一个集合 ...
- yii配置访问路由权限配置
'verbs' => [ 'class' => VerbFilter::className(), 'actions' => [ 'logout' => ['post', 'ge ...
- GPS卫星定位防盗系统机芯
GPS卫星定位防盗系统机芯,一般安装在驾驶室的操作台内,包括GPS模块,安装时需要先把车门打开,并把两边的车门拆卸下来,才能拆卸驾驶仪表台,再按说明书安装GPS定位防盗机芯,同时置于4A/12V免维护 ...
- mui-当使用addeleventlisener()方法绑定事件时选择器无法绑定事件
在mui中绑定事件不能用jQuery或mui(“#XX”)的形式选取某个元素,而是document.getelementbyid()的形式 mui(“#XX”)可以使用on方法绑定事件
- 关于U3D图片的压缩格式
http://blog.sina.com.cn/s/blog_930ffa0b0102vass.html
- js中函数的 this、arguments 、caller,call(),apply(),bind()
在函数内部有两个特殊的对象,arguments 和 this,还有一个函数对象的属性caller. arguments对象 arguments是一个类似数组的对象,包含着传入函数的所有参数. func ...
- SpringCloud报错: "Field discoveryClient in com.controller.DcController required a bean of type 'com.netflix.discovery.DiscoveryClient' that could not be found."
SpringCloud报错: "Field discoveryClient in com.controller.DcController required a bean of type 'c ...
- 第十一章 串 (c1)KMP算法:从记忆力到预知力
- NumPy 矩阵库(Matrix)
NumPy 矩阵库(Matrix) NumPy 中包含了一个矩阵库 numpy.matlib,该模块中的函数返回的是一个矩阵,而不是 ndarray 对象. 一个 的矩阵是一个由行(row)列(col ...
- 江西财经大学第一届程序设计竞赛 H题 求大数的阶乘
链接:https://www.nowcoder.com/acm/contest/115/H 来源:牛客网 晚上,小P喜欢在寝室里一个个静静的学习或者思考,享受自由自在的单身生活. 他总是能从所学的知识 ...