Subject description:
You 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.
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
Ideas:
The problem is to give a number n, a number k, see if I can Couchu with a set of k elements of strictly increasing number of columns, and this set of numbers is n, but also let the greatest common divisor largest number of this group.
At first, a1 + a2 + ... + ak = n> = a1 + (a1 + 1) + (a1 + 2) + ... + (ak + k-1), solve for 1 <= a1 <= (nk * (k-1) / 2) / k, since the maximum divisor required, to find out a factor of all, from a large to a small specimens,
For example, to find a f a1 is a factor, if it wants to f gcd this set of numbers, it must also be f n factor, check, is not skipped, with regard to n / f, becomes to find a strictly increasing and the number of columns is n / f, would not have considered a common factor.
Beginning to put too much attention on a1, a1 put all possible values to go along, to find factors were, respectively, the result is wrong, and the program logic and chaos + complex, there are many special sentenced to place (I'm almost crazy) because each has a factor of 1 a1, a1 is the last for larger (from big to small checks) will always stop to 1, that to calculate the final result.
So then all of a factor all put in a set, but also easy to find the maximum, through all the factors from small to large. Enough. . . ?
The results timeout.
Think about it, how to do. Since last factor f if a1 are subject conditions are met, then it is also a factor n, then n factor to find out just fine ah. (9 (1> ◡ <1) 6), n is noted that the enumeration is not forget factor f to satisfy the constraints of the bounds of a1, it satisfies added to this set.
But, however, also noted that a start determination may be possible to construct such a series, a1 + a2 + ... + ak = n> = ka1 + k * (k-1) / 2> = k + k * (k- 1) / 2, that is over? NONONO, to be noted that the scope of the subject data, if to a 4 * 10 ^ 9 such, long long also save any ah, supposed that, with the double bar to keep k ...
Type |
Size |
Value range |
No value type void |
0 byte |
No range |
Boolean bool |
1 byte |
true false |
Signed short short [int] / signed short [ int] |
2 byte |
-32768~32767 |
Unsigned short unsigned short [int] |
2 byte |
0~65535 |
Signed integer int / signed [int] |
4 byte |
-2147483648~2147483647 |
Unsigned int unsigned [int] |
4 byte |
0~4294967295 |
Signed long integer long [int] / signed long [ int] |
4 byte |
-2147483648~2147483647 |
Unsigned long unsigned long [int] |
4 byte |
0~4294967295 |
long long |
8 byte |
0~18446744073709552000 |
Signed char char / signed char |
1 byte |
-128~127 |
Unsigned char unsigned char |
1 byte |
0~255 |
Wide char wchar_t (unsigned short.) |
2 byte |
0~65535 |
Single-precision floating point type float |
4 byte |
-3.4E-38 3.4E + 38 ~ |
Double-precision floating-point double |
8 byte |
1.7E-308 ~ 1.7E + 308 |
long double |
8 byte |
|
(See table below blog on the source)
Code:
1 #include <iostream> 2 #include <cmath> 3 #include <vector> 4 #include <set> 5 #include <algorithm> 6 using namespace std; 7 long long n; 8 double k; 9 set<long long> fac; 10 vector<long long> num; 11 int main() 12 { 13 cin >> n >> k; 14 if(k+k*(k-1)/2<=n) 15 { 16 long long uper1 = (n-k*(k-1)/2)/k; 17 long long lower1 = 1; 18 //cout << uper1 << " " << lower1 << endl; 19 long long qz = 1; 20 /*for(long long a = uper1; a>=lower1; a--) 21 { 22 for(long long i = 1; i<sqrt(a)+1; i++) 23 { 24 if(a%i==0) 25 { 26 fac.insert(i); 27 fac.insert(a/i); 28 } 29 } 30 }*/ 31 for(long long i = 1;i<sqrt(n)+1;i++) 32 { 33 if(n%i==0) 34 { 35 if(i<=uper1) 36 { 37 fac.insert(i); 38 } 39 if(n/i<=uper1) 40 { 41 fac.insert(n/i); 42 } 43 } 44 } 45 /*cout << "fac " << endl; 46 for(auto ite = fac.rbegin();ite!=fac.rend();ite++) 47 { 48 cout << *ite << " "; 49 } 50 cout << endl;*/ 51 for(auto ite = fac.rbegin(); ite!=fac.rend(); ite++) 52 { 53 //cout << "ite " << *ite << endl; 54 int flag = 1; 55 num.clear(); 56 long long sum = n/(*ite)-1; 57 int shu = 1; 58 num.push_back(shu); 59 if(sum) 60 { 61 shu++; 62 while(shu<=sum&&num.size()<k-1) 63 { 64 num.push_back(shu); 65 sum -= shu; 66 shu++; 67 68 } 69 if(shu<=sum) 70 { 71 qz = *ite; 72 //cout << "qz " << qz << endl; 73 num.push_back(sum); 74 break; 75 } 76 } 77 else 78 { 79 qz = *ite; 80 break; 81 } 82 } 83 for(int i = 0;i<num.size();i++) 84 { 85 cout << qz*num[i] << " "; 86 } 87 cout << endl; 88 } 89 else 90 { 91 cout << -1 << endl; 92 } 93 return 0; 94 }
References:
Dazed and Confused 12138, C ++ basic data type size and scope of representation, https: //blog.csdn.net/weixin_40709898/article/details/79368310 (indirect reference, because the above blog is reprinted but could not find the original blog)