Maximal GCD CodeForces - 803C

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.

Example
Input
6 3
Output
1 2 3
Input
8 2
Output
2 6
Input
5 3
Output
-1

找出一个递增数列,长度为k,总和为n,使得数列公因数最大,可知数列的形式应该是a,2a,…,ka最优,即要满足a*k(k+1)/2<=n,从大到小找n的因数,判断个数是否满足,可以把循环分为两次1~sqrt节约时间

#include <iostream>
#include <stdio.h>
#include <math.h>
using namespace std;

int main()
{
    long long i,j,n,k,t,q,sk;
    double qq;
    while(cin>>n>>k){
        if(k>1e6||2*n<(k*(k+1))){
            printf("-1\n");
            continue;
        }
        qq=sqrt(n*1.0);
        q=(long long)qq;
        sk=k*(k+1)/2;
        t=0;
        for(i=1;i<=q;i++){
            if(n%i==0){
                j=n/i;
                if(sk<=i){
                    t=j;
                    break;
                }
            }
        }
        if(t==0){
            for(i=q+1;i>=1;i--){
                if(n%i==0){

                    if(i*sk<=n){
                        t=i;
                        break;
                    }
                }
            }
        }
        for(i=1;i<k;i++){
            j=i*t;
            printf("%I64d ",j);
        }
        j=n-t*k*(k-1)/2;
        printf("%I64d\n",j);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值