zoukankan      html  css  js  c++  java
  • 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;
    }
     
  • 相关阅读:
    防止表单重复提交
    tp5中的配置机制
    PHP remove,empty和detach区别
    jquery data方法
    webstrom使用记录
    input checkbox问题和li里面包含checkbox
    【转】HTML中A标签与click事件的前世今生
    jquery toggle方法
    webstore+nodejs
    web storm使用和配置
  • 原文地址:https://www.cnblogs.com/kls123/p/6804067.html
Copyright © 2011-2022 走看看