Educational Codeforces Round 20 C 数学

题目:

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值