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
-1
思路来自:
http://www.cnblogs.com/Twobox/p/6806829.html
若这k个数字的gcd为q,那么n一定可以整除q,所以考虑从N的因子里选出这个q来,先求出k个数字可以表示的最小的数,即1+2+3+...+k,
#include <iostream>
#include <cmath>
using namespace std;
int main()
{//1<=n,k<=1e10
long long n,k,sqr,cadd,q;//q为最大公约数
cin>>n>>k;
if(k>141420)
{
cout<<"-1"<<endl;return 0;
}
cadd=(1+k)*k/2;sqr=sqrt(n);q=0;
for(long long i=1;i<=sqr;i++)
{
if(n%i==0)
{
if(i>=cadd)
{q=n/i;break;}
else if(n/i>=cadd)
q=i;
}
}
//cout<<q<<endl;
if(!q)
{
cout<<"-1"<<endl;return 0;
}
long long sum=0,i;
for(i=1;i<k;i++)
{
cout<<i*q<<" ";sum+=i;
}
cout<<q*(n/q-sum)<<endl;
return 0;
}