题目:
You are given positive integer number n. You should create such strictly increasing sequence of kpositive numbers a1, a2, ..., ak, that their sum is equal to 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.
The first line consists of two numbers n and k (1 ≤ n, k ≤ 1010).
If the answer exists then output k numbers — resulting sequence. Otherwise output -1. If there are multiple answers, print any of them.
6 3
1 2 3
8 2
2 6
5 3
-1
求k个数(严格递增),让他们加起来等于n,且它们的最大公约数最大。 我假设最大公约数是maxDiv,因为这几个数是严格递增的,所以它们的和最小sum=sigm(1...k);所以最大公约数不超过MAX=n/sum. k1*g+k2*g+...kk*g=n,然后分解n,g一定是n的因数。k1+k2+..+kk=n/g。右分析可知n/g>=sum,我为了方便,我让k1=1*g,k2=2*g,k(k-1)=(k-1)*g,然后kk=n-sigma(1...k-1)。为什么这样保证一定是递增的?因为n>=g*sigma(1...k)所以kk=n-g*sigma(1...k-1)>g*k的,故,kk大于(k1,k2,..k(k-1))。
code:
#include<cstdio>
#define max(a,b) (a>b?a:b)
typedef long long LL;
int main(void){
LL n,k;scanf("%I64d%I64d",&n,&k);
if(k>1000000){printf("-1\n");return 0;}
LL sum=(1+k)*k/2;
LL MAX=n/sum;
int maxDiv=-1;
for(LL i=1;i*i<=n;++i){
if(n%i==0){
if(i<=MAX)maxDiv=max(maxDiv,i);
if(n/i<=MAX)maxDiv=max(maxDiv,n/i);
}
}
if(maxDiv==-1){printf("-1\n");return 0;}
for(LL i=1;i<=k-1;++i)
printf("%I64d ",maxDiv*i);
printf("%I64d\n",n-k*(k-1)/2*maxDiv);
}
另外就是我犯低级(要命)错误的两个地方:
图1会产生奇怪的答案,因为maxDiv是int型
tu
图1会导致死循环,i*i会变成负数
结论:涉及到溢出就用LL吧,0rzzzz