您的位置:首页 > 其它

Educational Codeforces Round 20 C 数学

2017-04-29 11:24 316 查看
题目:

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 kpositive 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


分析:

求k个数(严格递增),让他们加起来等于n,且它们的最大公约数最大。  我假设最大公约数是maxDiv,因为这几个数是严格递增的,所以它们的和最小sum=sigm(1...k);所以最大公约数不超过MAX=n/sum.    k1*g+k2*g+...kk*g=n,然后分解n,g一定是n的因数。k1+k2+..+kk=n/g。右分析可知n/g>=sum,我为了方便,我让k1=1*g,k2=2*g,k(k-1)=(k-1)*g,然后kk=n-sigma(1...k-1)。为什么这样保证一定是递增的?因为n>=g*sigma(1...k)所以kk=n-g*sigma(1...k-1)>g*k的,故,kk大于(k1,k2,..k(k-1))。

code:

#include<cstdio>

#define max(a,b) (a>b?a:b)

typedef long long LL;

int main(void){

    LL n,k;scanf("%I64d%I64d",&n,&k);

    if(k>1000000){printf("-1\n");return 0;}

    LL sum=(1+k)*k/2;

    LL MAX=n/sum;

    int maxDiv=-1;

    for(LL i=1;i*i<=n;++i){

        if(n%i==0){

            if(i<=MAX)maxDiv=max(maxDiv,i);

            if(n/i<=MAX)maxDiv=max(maxDiv,n/i);

        }

    }

    if(maxDiv==-1){printf("-1\n");return 0;}

    for(LL i=1;i<=k-1;++i)

        printf("%I64d ",maxDiv*i);

    printf("%I64d\n",n-k*(k-1)/2*maxDiv);

}

另外就是我犯低级(要命)错误的两个地方:





图1会产生奇怪的答案,因为maxDiv是int型

tu




图1会导致死循环,i*i会变成负数

结论:涉及到溢出就用LL吧,0rzzzz
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  codeforces gcd 数学