Created
July 25, 2016 02:51
-
-
Save aadimator/791b5c97420215873c77f90de0628856 to your computer and use it in GitHub Desktop.
Pairwise Distinct Summands
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <vector> | |
using std::vector; | |
vector<int> optimal_summands(int n) { | |
// using the algorithm described in the pdf | |
vector<int> summands; | |
for (int k = n, l = 1; k > 0; l++) { | |
if (k <= 2 * l) { | |
summands.push_back(k); | |
k -= k; | |
} else { | |
summands.push_back(l); | |
k -= l; | |
} | |
} | |
return summands; | |
} | |
int main() { | |
int n; | |
std::cin >> n; | |
vector<int> summands = optimal_summands(n); | |
std::cout << summands.size() << '\n'; | |
for (size_t i = 0; i < summands.size(); ++i) { | |
std::cout << summands[i] << ' '; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment