Maximal GCD CodeForces

来源:互联网 发布:linux x264命令 编辑:程序博客网 时间:2024/05/22 01:51

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;}
原创粉丝点击