[codeforces803C]Maximal GCD

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

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

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

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

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

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值