C. Maximal GCD
time limit per test:
1 second
memory limit per test:
256 megabytes
input:
standard input
output:
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.
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
input
6 3
output
1 2 3
input
8 2
output
2 6
input
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 ;
}