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
思路:很明显应该我们应该先从n的因子找起,看一眼数据范围是1e10,但用根号的话只有1e5,所以时间上能过,但注意根号后的因子也要记录,然后我们在一遍一遍的遍历他的因子,遍历的时候只要判断最后一个值是否符合条件就行了;
下面附上我的代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n,k;
ll a[200005],b[200005];
int main()
{
while(cin>>n>>k)
{
int l=0;
if((k+1)*k/2>n||k>5e5)
{
puts("-1");
continue;
}
for(int i=1;i<=sqrt(n);i++)
{
if(n%i==0)
{
a[l++]=i;
a[l++]=n/i;
}
}
sort(a,a+l);
int flag=0;
for(int i=0;i<l;i++)
{
ll p=n-k*(k-1)*a[i]/2;
if(p>(k-1)*a[i]&&(p%a[i]==0))
{
flag=1;
for(ll j=1;j<k;j++)
b[j]=a[i]*j;
b[k]=p;
}
else if(p<=(k-1)*a[i])
break;
}
if(flag)
for(ll i=1;i<=k;i++)
printf("%lld%c",b[i],i==k?'\n':' ');
}
return 0;
}