C. Maximal GCD
time limit per test
1 secondmemory limit per test
256 megabytesinput
standard inputoutput
standard outputYou are given positive integer number n. You should create such strictly increasing sequence of k positive 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.
Input
The first line consists of two numbers n and k (1 ≤ n, k ≤ 1010).
Output
If the answer exists then output k numbers — resulting sequence. Otherwise output -1. If there are multiple answers, print any of them.
Examples
Input
6 3
Output
1 2 3
Input
8 2
Output
2 6
Input
5 3
Output
-1
题意:构造一个递增的长度为k 和为n 的数列,gcd尽可能的大.
题解:gcd 从1~i 所以寻找长度为k 首项为i 等差为i 数列和为n的一个序列
细细考虑 n应当模尽i 那么枚举n的因子 check 一下 取最大的i
1 #include<iostream> 2 #include<cstdio> 3 #include<cmath> 4 #include<cstring> 5 #include<algorithm> 6 #include<set> 7 #include<vector> 8 #define ll __int64 9 using namespace std; 10 ll n,k; 11 bool fun(ll s){ 12 if(k*(k+1)*s/2>n) 13 return false; 14 if((n-k*(k-1)*s/2)%s==0) 15 return true; 16 return false; 17 } 18 int main() 19 { 20 scanf("%I64d %I64d",&n,&k); 21 if(k>(2*n/(1+k))) 22 printf("-1 "); 23 else 24 { 25 ll e=1; 26 for(ll i=1;i*i<=n;i++){ 27 if(n%i==0&&fun(i)) e=max(e,i); 28 if(n%i==0&&fun(n/i)) e=max(e,n/i); 29 } 30 for(int j=1; j<k; j++) 31 printf("%I64d ",j*e); 32 printf("%I64d ",n-k*(k-1)*e/2); 33 } 34 return 0; 35 }