[codeforces803C]Maximal GCD

版权声明:辛辛苦苦码字,你们转载的时候记得告诉我 https://blog.csdn.net/dxyinme/article/details/90905090

time limit per test : 1 second
memory limit per test : 256 megabytes

You are given positive integer number n n . You should create such strictly increasing sequence of k k positive numbers a 1 , a 2 , . . . , a k a_1, a_2, ..., a_k , that their sum is equal to n 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 -1 .

Input
The first line consists of two numbers n n and k ( 1 n , k 1 0 10 ) k (1 ≤ n, k ≤ 10^{10}) .
Output

If the answer exists then output k k numbers — resulting sequence. Otherwise output 1 -1 . If there are multiple answers, print any of them.

题意:
给定 n n , k ( n , k < = 1 0 10 ) k(n,k<=10^{10}) ,构造出一个长度为 k k 的递增序列,使得这个递增序列的和是 n n 并且序列的最大公因数最大。

题解:
首先我们先考虑,如果k大于20w,那么肯定不可能存在了,毕竟最简单的上升序列都是1,2,3…
然后考虑枚举最大公因数。设最大公因数为c,则我们只需要找到任意一个序列和为n/c的上升序列即可,设这个序列的和为s。显然s和c都是n的因数,那么只需要枚举s或者枚举c就行了,时间复杂度是 n \sqrt{n} 的,当你已知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;
}

猜你喜欢

转载自blog.csdn.net/dxyinme/article/details/90905090
gcd