[codeforces803C]Maximal GCD

Copyright: hard code word, when you reprint remember to tell me 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.

Meaning of the questions:
given n n , k ( n , k < = 1 0 10 ) k(n,k<=10^{10}) , a length construct k k increment sequence, and such that the sequence is incremented n n and the greatest common divisor of the maximum sequence.

Solution:
First, let's consider, if k is greater than 20w, then it is surely impossible, after all, is the easiest-up sequencing, 2, 3 ...
and then consider enumerate the greatest common divisor. Provided the greatest common factor is c, then we just need to find a sequence of arbitrary n / c sequence to rise, and provided the sequence is s. S and c are obviously factor of n, then only Enumeration enum s or c on the line, the time complexity is n \sqrt{n} And when you s known, as long as do a rising sequence 1,2,3 ..., k, and give each a plus s / k, and finally the remaining number plus one each from the back It can be.

#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;
}

Guess you like

Origin blog.csdn.net/dxyinme/article/details/90905090
gcd