您的位置:首页 > 其它

Educational Codeforces Round 20 C 数学/贪心/构造

2017-05-02 21:37 399 查看
C. Maximal GCDtime limit per test
1 secondmemory limit per test
256 megabytesinput
standard inputoutput
standard outputYou 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.ExamplesInput
6 3
Output
1 2 3
Input
8 2
Output
2 6
Input
5 3
Output
-1

题意:构造一个递增的长度为k 和为n 的数列,gcd尽可能的大.
题解:gcd 从1~i 所以寻找长度为k 首项为i 等差为i 数列和为n的一个序列
细细考虑 n应当模尽i 那么枚举n的因子 check 一下 取最大的i
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<set>
#include<vector>
#define ll __int64
using namespace std;
ll n,k;
bool fun(ll s){
if(k*(k+1)*s/2>n)
return false;
if((n-k*(k-1)*s/2)%s==0)
return true;
return false;
}
int main()
{
scanf("%I64d %I64d",&n,&k);
if(k>(2*n/(1+k)))
printf("-1\n");
else
{
ll e=1;
for(ll i=1;i*i<=n;i++){
if(n%i==0&&fun(i)) e=max(e,i);
if(n%i==0&&fun(n/i)) e=max(e,n/i);
}
for(int j=1; j<k; j++)
printf("%I64d ",j*e);
printf("%I64d\n",n-k*(k-1)*e/2);
}
return 0;
}

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: