codeforces 803C Maximal GCD

51 篇文章 0 订阅
7 篇文章 0 订阅
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
output
-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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值