Educational Codeforces Round 20 C

Description

You are given positive integer number n. You should create such strictly increasing sequence of k positive numbersa1, 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
output
-1

 题意:求n分解成k个数的形式,要求他们的最大公约数最大

 解法:首先根据范围知道1e8后是无解的,然后根据等差数列和求是不是符合要求

再求出d的最大范围,接下来就是求最大的公约数了

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn=654321;
ll n,num;
int main()
{
    std::ios::sync_with_stdio(false);
    cin>>n>>num;
    ll sum=num*(num+1)/2;
    if(sum>n||num>(ll)1e8)
    {
        cout<<"-1"<<endl;
        return 0;
    }
    ll d=n/sum;
    ll r=1;
   // cout<<d<<endl;
    for(ll i=1;i*i<=n;i++)
    {
        if(n%i) continue;
        if(i>=r&&i<=d) r=i;
        if(n/i>=r&&n/i<=d) r=n/i;
    }
    for(ll i=1;i<num;i++)
    {
        n-=i*r;
        cout<<i*r<<" ";
    }
    cout<<n<<endl;
    return 0;
}
原文地址:https://www.cnblogs.com/yinghualuowu/p/6792651.html