#include <iostream> #include <vector> #include <unordered_set> using namespace std; void addTree(vector<int>& currentTreeSequence, int numberOfTrees, int numberOfTreeSpecies, int currentLength) { if (currentLength == numberOfTrees) { //if (currentTreeSequence[0] != currentTreeSequence[currentTreeSequence.size() - 1]) { for (int i = 0; i < currentTreeSequence.size(); i++) { cout << currentTreeSequence[i] << ""; } cout << endl; //} return; } for (int i = 0; i < numberOfTreeSpecies; i++) { currentTreeSequence[currentLength] = i; addTree(currentTreeSequence, numberOfTrees, numberOfTreeSpecies, currentLength + 1); } } void generateAllTreeSequences(int numberOfTrees, int numberOfTreeSpecies) { vector<int> currentTreeSequence(numberOfTrees); addTree(currentTreeSequence, numberOfTrees, numberOfTreeSpecies, 0); } bool isValidSequence(vector<int> currentTreeSequence) { return false; } int main() { std::ios::sync_with_stdio(false); int numberOfTrees, numberOfTreeSpecies; cin >> numberOfTrees >> numberOfTreeSpecies; //generateAllTreeSequences(numberOfTrees, numberOfTreeSpecies); if (numberOfTrees < 2) { cout << 0; return 0; } if (numberOfTrees < 4) { cout << numberOfTreeSpecies; return 0; } vector<long long int> numberOfPossibilites(numberOfTrees + 1, 0); if (numberOfTreeSpecies == 2) { numberOfPossibilites[2] = 2; numberOfPossibilites[3] = 4; for (int i = 4; i <= numberOfTrees; i++) { numberOfPossibilites[i] = (long long)(numberOfPossibilites[i - 1] * 2 + 2 * (i - 3)) % 1000000007; } } else if (numberOfTreeSpecies == 3) { numberOfPossibilites[2] = 3; numberOfPossibilites[3] = 9; for (int i = 4; i <= numberOfTrees; i++) { numberOfPossibilites[i] = (long long)(numberOfPossibilites[i - 1] * 3 + 9) % 1000000007; } } else { numberOfPossibilites[numberOfTrees] = -1; } cout << numberOfPossibilites[numberOfTrees]; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | #include <iostream> #include <vector> #include <unordered_set> using namespace std; void addTree(vector<int>& currentTreeSequence, int numberOfTrees, int numberOfTreeSpecies, int currentLength) { if (currentLength == numberOfTrees) { //if (currentTreeSequence[0] != currentTreeSequence[currentTreeSequence.size() - 1]) { for (int i = 0; i < currentTreeSequence.size(); i++) { cout << currentTreeSequence[i] << ""; } cout << endl; //} return; } for (int i = 0; i < numberOfTreeSpecies; i++) { currentTreeSequence[currentLength] = i; addTree(currentTreeSequence, numberOfTrees, numberOfTreeSpecies, currentLength + 1); } } void generateAllTreeSequences(int numberOfTrees, int numberOfTreeSpecies) { vector<int> currentTreeSequence(numberOfTrees); addTree(currentTreeSequence, numberOfTrees, numberOfTreeSpecies, 0); } bool isValidSequence(vector<int> currentTreeSequence) { return false; } int main() { std::ios::sync_with_stdio(false); int numberOfTrees, numberOfTreeSpecies; cin >> numberOfTrees >> numberOfTreeSpecies; //generateAllTreeSequences(numberOfTrees, numberOfTreeSpecies); if (numberOfTrees < 2) { cout << 0; return 0; } if (numberOfTrees < 4) { cout << numberOfTreeSpecies; return 0; } vector<long long int> numberOfPossibilites(numberOfTrees + 1, 0); if (numberOfTreeSpecies == 2) { numberOfPossibilites[2] = 2; numberOfPossibilites[3] = 4; for (int i = 4; i <= numberOfTrees; i++) { numberOfPossibilites[i] = (long long)(numberOfPossibilites[i - 1] * 2 + 2 * (i - 3)) % 1000000007; } } else if (numberOfTreeSpecies == 3) { numberOfPossibilites[2] = 3; numberOfPossibilites[3] = 9; for (int i = 4; i <= numberOfTrees; i++) { numberOfPossibilites[i] = (long long)(numberOfPossibilites[i - 1] * 3 + 9) % 1000000007; } } else { numberOfPossibilites[numberOfTrees] = -1; } cout << numberOfPossibilites[numberOfTrees]; } |