time limit per test : 1 second
memory limit per test : 256 megabytes
You are given positive integer number
. You should create such strictly increasing sequence of
positive numbers
, that their sum is equal to
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 .
Input
The first line consists of two numbers
and
.
Output
If the answer exists then output numbers — resulting sequence. Otherwise output . If there are multiple answers, print any of them.
题意:
给定
,
,构造出一个长度为
的递增序列,使得这个递增序列的和是
并且序列的最大公因数最大。
题解:
首先我们先考虑,如果k大于20w,那么肯定不可能存在了,毕竟最简单的上升序列都是1,2,3…
然后考虑枚举最大公因数。设最大公因数为c,则我们只需要找到任意一个序列和为n/c的上升序列即可,设这个序列的和为s。显然s和c都是n的因数,那么只需要枚举s或者枚举c就行了,时间复杂度是
的,当你已知s的时候,只要先做一个上升序列1,2,3…,k,然后再给每一位加上s/k,最后的剩余的数字从后往前每个加一即可。
#include<bits/stdc++.h>
#define LiangJiaJun main
#define ll long long
using namespace std;
ll n,k,a[200004];
ll poa[200004],cnt,delta,rest,sau;
void output(int g,int v){
for(int i=1;i<=k;i++)a[i]=i+delta;
for(int i=k;i>=1&&v>0;i--){
a[i]++;
v--;
}
ll gk=n/sau;
for(int i=1;i<=k;i++){
printf("%lld ",a[i]*gk);
}
puts("");
}
int LiangJiaJun(){
cnt=0;
scanf("%lld%lld",&n,&k);
if(k>2e5||k*(k+1)/2>n)return puts("-1"),0;
sau=k*(k+1)/2;
for(ll i=1;i<=sqrt(n);i++){
if(n%i==0){
poa[++cnt]=i;
if(n!=i*i){
poa[++cnt]=n/i;
}
}
}
sort(poa+1,poa+cnt+1);
int cip=1;
while(cip<=cnt&&poa[cip]<sau)cip++;
if(cip>cnt)return puts("-1"),0;
delta=0;
rest=0;
if(poa[cip]==sau){
output(delta,rest);
}
else{
rest+=poa[cip]-sau;
sau=poa[cip];
delta+=rest/k;
rest%=k;
output(delta,rest);
}
return 0;
}