Skip to content

Instantly share code, notes, and snippets.

@swvitaliy
Created November 11, 2019 16:48
Show Gist options
  • Save swvitaliy/c2fdef8350f085d9851d14a7844380ba to your computer and use it in GitHub Desktop.
Save swvitaliy/c2fdef8350f085d9851d14a7844380ba to your computer and use it in GitHub Desktop.
#include<iostream>
#include<vector>
using namespace std;
typedef vector<int> VI;
int ordered_partitons(int n, VI& ni) {
int res = 0;
for (int i : ni) {
int t = n- i;
if (t == 0)
++res;
else if (t > 0)
res += ordered_partitons(t, ni);
}
return res;
}
int unordered_partitions(int n, VI& ni, int j) { // expected ordered ni passed
if (j < 0)
return 0;
int t = n - ni[j];
if (t == 0)
return 1;
if (t < 0)
return 0;
return unordered_partitions(n, ni, j-1) +
unordered_partitions(t, ni, j);
}
int main(int argc, char ** argv) {
VI ni({1,2,3});
cout << "F(7; 1,2,3): " << ordered_partitons(7, ni) << endl;
cout << "P(7; 1,2,3): " << unordered_partitions(7, ni, (int)ni.size() - 1) << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment