C - Maximal GCD CodeForces - 803C

13 篇文章 0 订阅
3 篇文章 0 订阅

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

题意:给你个整数n,和个数k,让你求k个数加起来等于n,且这k个数严格递增,并且有最大的公因数
题解:既然有最大公约数g,那么a1+a2+a3+a4+... +ak==g*(1+2+3+4+5+...+k),正好符合题意递增,
例如  16 3
16==2*(1+3+4) =2*(1+2+5)换成1+2+3+...前面不是的话吧各部分多余的累到最后一个数上1+3+4---》1+2+5
所以,最多有k个也就是有下限 n/g< (1+k)*k/2
找到n的所有因子,用二分找 根号n 个因子,但是要比较一下,因为一次出现两个因子,取最大的那个因子

由于n也是小于1010,下面如果k>231−1=(2.14∗109)那么k*(k+1)/2是肯定超过n了,g>=1,所以不可能的。以此来防止超过long long的表示范围。(263−1=9.23∗1018 ,而k最大也可以取到1010,那么need的计算会超过表示范围)

#include<stdio.h>
#include<math.h> 

long long MAX = (1<<31)-1;

int main(){
    long long n,k;
    scanf("%lld %lld",&n,&k);
    long long need = k * (k + 1) / 2;
    if(n < need || k > MAX)                //防止超过long long的表示范围
        puts("-1");
    else{
        long long g = 1;
        long long i = sqrt(n);
        for(i; i >=1 ; i--){
            if(n % i == 0){
                long long now = n / i;
                if(now >= need)            //now是个数和,i是因子 
                    g = g>i?g:i;//取较大的那个因子
                if(i >= need)              //考虑i是个数和,need是因子的情况 
                    g = g>now?g:now;
             }

        }
        long long now = n / g;
        for(long long i = 1; i < k; i++){
           printf("%lld ",i * g);
           now -= i;
        } 
        printf("%lld\n",g * now);
    }
    return 0;
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值