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\n",ans);
      return 0;
    }

    leftover=n/ans;
    for(i=1;i<k;i++)
    {
       printf("%I64d ",i*ans);
       leftover-=i;
    }

    printf("%I64d\n",leftover*ans);

    return 0;
}

 

 
posted @ 2017-05-03 21:47  冥想选手  阅读(143)  评论(0编辑  收藏  举报