Skip to content

Instantly share code, notes, and snippets.

@aadimator
Created July 25, 2016 02:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aadimator/791b5c97420215873c77f90de0628856 to your computer and use it in GitHub Desktop.
Save aadimator/791b5c97420215873c77f90de0628856 to your computer and use it in GitHub Desktop.
Pairwise Distinct Summands
#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