Created
November 11, 2019 16:48
-
-
Save swvitaliy/c2fdef8350f085d9851d14a7844380ba to your computer and use it in GitHub Desktop.
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 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