CF803 C. Maximal GCD

题目传送门:https://codeforces.com/problemset/problem/803/C

题目大意:
给定两个数\(n,k\),求\(k\)个数\(a_1<a_2<...<a_k\),满足\(\sum\limits_{i=1}^ka_i=n\),且使得\(\gcd\limits_{i=1}^k\{a_i\}\)最大


假定\(\alpha=\gcd\limits_{i=1}^k\{a_i\}\),则有\(a_i=\alpha\times r_i\),因为\(\sum\limits_{i=1}^kr_i\geqslant \frac{k(k+1)}{2}\),故\(\alpha\leqslant \frac{2n}{k(k+1)}\)

显然\(n=\alpha\sum\limits_{i=1}^kr_i\),故\(\alpha\)应为\(n\)的约数,且最大不超过\(\frac{2n}{k(k+1)}\)

故我们可以\(O(\sqrt n)\)找到最大的\(\alpha\),然后按照\(r_i=i(1\leqslant i\leqslant k-1)\)\(r_k=\frac{n}{\alpha}-\sum\limits_{i=1}^{k-1}r_i\)构造\(a_i\)即可

/*program from Wolfycz*/
#include<map>
#include<cmath>
#include<cstdio>
#include<vector>
#include<cstring>
#include<iostream>
#include<algorithm>
#define Fi first
#define Se second
#define ll_inf 1e18
#define MK make_pair
#define sqr(x) ((x)*(x))
#define pii pair<int,int>
#define int_inf 0x7f7f7f7f
using namespace std;
typedef long long ll;
typedef unsigned int ui;
typedef unsigned long long ull;
inline char gc(){
	static char buf[1000000],*p1=buf,*p2=buf;
	return p1==p2&&(p2=(p1=buf)+fread(buf,1,1000000,stdin),p1==p2)?EOF:*p1++;
}
template<typename T>inline T frd(T x){
	int f=1; char ch=gc();
	for (;ch<'0'||ch>'9';ch=gc())	if (ch=='-')    f=-1;
	for (;ch>='0'&&ch<='9';ch=gc())	x=(x<<1)+(x<<3)+ch-'0';
	return x*f;
}
template<typename T>inline T read(T x){
	int f=1; char ch=getchar();
	for (;ch<'0'||ch>'9';ch=getchar())	if (ch=='-')	f=-1;
	for (;ch>='0'&&ch<='9';ch=getchar())	x=(x<<1)+(x<<3)+ch-'0';
	return x*f;
}
inline void print(int x){
	if (x<0)	putchar('-'),x=-x;
	if (x>9)	print(x/10);
	putchar(x%10+'0');
}
vector<ll>vec;
int main(){
//	freopen(".in","r",stdin);
//	freopen(".out","w",stdout);
	ll n=read(0ll),k=read(0ll);
	if (k>sqrt(n<<1)){
		printf("-1\n");
		return 0;
	}
	ll m=(k+1)*k/2,limit=n/m,All=0;
	if (!limit){
		printf("-1\n");
		return 0;
	}
	for (ll i=1;i<=sqrt(n);i++){
		if (n%i)	continue;
		vec.push_back(i);
		if (n/i!=i)	vec.push_back(n/i);
	}
	sort(vec.begin(),vec.end());
	vector<ll>::iterator it=upper_bound(vec.begin(),vec.end(),limit);
	it--;
	for (int i=1;i<k;i++)	printf("%lld ",*it*i),All+=*it*i;
	printf("%lld\n",n-All);
	return 0;
}
posted @ 2021-07-08 11:20  Wolfycz  阅读(47)  评论(0编辑  收藏  举报