• 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 k positive numbersa1, 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
     
    这比赛很骚,打完ab就直接睡了,然后发现c好tm gay。
    思路大致就是求出满足要求的最大公约数( n/i>=k*(k+1)/2 ,因为输出的值是增长且有最大公约数的,要想有值必须满足该等式),然后依次输出该公约数的倍数,
    最后一个直接输出前面剩下的就行。
    #include <iostream>
    #include <cstdio>
    using namespace std;
    
    long long n,k;
    
    bool check(long long a)  
    {
        long long c=n/a;   
    
        if(k>=1e8)
         return false;
    
        return c>=k*(k+1LL)/2LL;
    
    }
    
    int main()
    {
        long long i;
        long long ans=-1;
        long long leftover;
    
        cin>>n>>k;
    
        for(i=1;i*i<=n;i++)
        {
            if(n%i==0)
            {
             if(check(i))
                ans=max(ans,i);
             if(check(n/i))
                ans=max(ans,n/i);
            }
        }
    
        if(ans==-1)
        {
          printf("%I64d
    ",ans);
          return 0;
        }
    
        leftover=n/ans;
        for(i=1;i<k;i++)
        {
           printf("%I64d ",i*ans);
           leftover-=i;
        }
    
        printf("%I64d
    ",leftover*ans);
    
        return 0;
    }
     
  • 相关阅读:
    说说与线程相关的方法
    sleep()和yield()有什么区别?
    同步和异步有何异同,分别在什么情况下使用?
    如何保证多个线程同时启动?
    volatile关键字能否保证线程安全?
    使用对象的wait()方法需要注意什么?
    乐观锁与悲观锁是什么?
    Condition实现等待、唤醒
    LongAdder与AtomicLong有什么区别?
    介绍一下ForkJoinPool的使用
  • 原文地址:https://www.cnblogs.com/kls123/p/6804067.html
Copyright © 2020-2023  润新知