Educational Codeforces Round 20 C. Maximal GCD

本文针对CF平台上的MaximalGCD算法题提供了解题思路及代码实现。题目要求构造一个严格递增的正整数序列,使得序列之和等于给定数值n且序列的最大公约数最大。文中详细解释了如何通过枚举因子来确定序列元素的方法。
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 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.

Examples
input
6 3
output
1 2 3
input
8 2
output
2 6
input
5 3
output
-1
暴力枚举,k个数都是n的因子
 n - s > (k - 1)·d.然后从d枚举到n/d就好了,这个思路我是想不到的,听打神讲的,然而我还写不出来,抄了大神的代码,CF这样很好啊。帮助小白进阶,从哪里开始枚举
自己要想清楚
#include <cstdio>
typedef long long ll;
int main()
{
    ll n, k;
    scanf("%lld%lld", &n, &k);
    if (k > (ll)1e8)
    {
        printf("-1\n");
        return 0;
    }
    ll b = n / (k * (k + 1) / 2);
    if (b == 0)
    {
        printf("-1\n");
        return 0;
    }
    ll r = 1;
    for (ll x = 1; x * x <= n; x++)
    {
        if (n % x != 0) continue;
        if (x <= b && x > r) r = x;
        if (n / x <= b && n / x > r) r = n / x;
    }
    for (int i = 1; i < k; i++)
        printf("%lld ", r * i);
    n -= r * k * (k - 1) / 2;
    printf("%lld\n", n);

    return 0;
}
View Code

 

 

 

转载于:https://www.cnblogs.com/BobHuang/p/6799181.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值