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.
InputThe 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.
Input
6 3
Output
1 2 3
Input
8 2
Output
2 6
Input
5 3
Output
-1
题意:给你个整数n,和个数k,让你求k个数加起来等于n,且这k个数严格递增,并且有最大的公因数
题解:既然有最大公约数g,那么a1+a2+a3+a4+... +ak==g*(1+2+3+4+5+...+k),正好符合题意递增,
例如 16 3
16==2*(1+3+4)
=2*(1+2+5)换成1+2+3+...前面不是的话吧各部分多余的累到最后一个数上1+3+4---》1+2+5
所以,最多有k个也就是有下限 n/g<
(1+k)*k/2
找到n的所有因子,用二分找 根号n 个因子,但是要比较一下,因为一次出现两个因子,取最大的那个因子
由于n也是小于1010,下面如果k>231−1=(2.14∗109)那么k*(k+1)/2是肯定超过n了,g>=1,所以不可能的。以此来防止超过long long的表示范围。(263−1=9.23∗1018 ,而k最大也可以取到1010,那么need的计算会超过表示范围)
#include<stdio.h>
#include<math.h>
long long MAX = (1<<31)-1;
int main(){
long long n,k;
scanf("%lld %lld",&n,&k);
long long need = k * (k + 1) / 2;
if(n < need || k > MAX) //防止超过long long的表示范围
puts("-1");
else{
long long g = 1;
long long i = sqrt(n);
for(i; i >=1 ; i--){
if(n % i == 0){
long long now = n / i;
if(now >= need) //now是个数和,i是因子
g = g>i?g:i;//取较大的那个因子
if(i >= need) //考虑i是个数和,need是因子的情况
g = g>now?g:now;
}
}
long long now = n / g;
for(long long i = 1; i < k; i++){
printf("%lld ",i * g);
now -= i;
}
printf("%lld\n",g * now);
}
return 0;
}