#include <iostream> using namespace std; #define mod 1000000007 #define ll long long int int main() { ios_base::sync_with_stdio(0); cin.tie(0); ll length, differentElems; cin >> length >> differentElems; ll** correctSequences = new ll* [length](); //length, possible endings ll** incorrectSequences = new ll* [length](); //length, possible endings for (int i = 0; i < length; i++) { correctSequences[i] = new ll[length](); incorrectSequences[i] = new ll[length](); } incorrectSequences[0][0] = differentElems; for (ll i = 1; i < length; i++) { for (ll j = 0; j < length - 1; j++) { correctSequences[i][j] += incorrectSequences[i - 1][j] * (j + 1); //* number of possible correct endings correctSequences[i][j] %= mod; correctSequences[i][j] += correctSequences[i - 1][j] * (j + 1); //*number of correct endings correctSequences[i][j] %= mod; incorrectSequences[i][j] += incorrectSequences[i - 1][j] * (differentElems - (j + 1)); //*number of wrong endings incorrectSequences[i][j] %= mod; incorrectSequences[i][j + 1] += correctSequences[i - 1][j] * (differentElems - (j + 1)); //*number of wrong endings incorrectSequences[i][j] %= mod; /*if (correctSequences[i][j] != 0) cout << correctSequences[i][j] << " correct " << j + 1 << '\n'; if (incorrectSequences[i][j] != 0) cout << incorrectSequences[i][j] << " wrong " << j + 1 << '\n';*/ } } ll total = 0; for (ll j = 0; j < length; j++) { total += correctSequences[length - 1][j]; total %= mod; } cout << total << '\n'; }
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 | #include <iostream> using namespace std; #define mod 1000000007 #define ll long long int int main() { ios_base::sync_with_stdio(0); cin.tie(0); ll length, differentElems; cin >> length >> differentElems; ll** correctSequences = new ll* [length](); //length, possible endings ll** incorrectSequences = new ll* [length](); //length, possible endings for (int i = 0; i < length; i++) { correctSequences[i] = new ll[length](); incorrectSequences[i] = new ll[length](); } incorrectSequences[0][0] = differentElems; for (ll i = 1; i < length; i++) { for (ll j = 0; j < length - 1; j++) { correctSequences[i][j] += incorrectSequences[i - 1][j] * (j + 1); //* number of possible correct endings correctSequences[i][j] %= mod; correctSequences[i][j] += correctSequences[i - 1][j] * (j + 1); //*number of correct endings correctSequences[i][j] %= mod; incorrectSequences[i][j] += incorrectSequences[i - 1][j] * (differentElems - (j + 1)); //*number of wrong endings incorrectSequences[i][j] %= mod; incorrectSequences[i][j + 1] += correctSequences[i - 1][j] * (differentElems - (j + 1)); //*number of wrong endings incorrectSequences[i][j] %= mod; /*if (correctSequences[i][j] != 0) cout << correctSequences[i][j] << " correct " << j + 1 << '\n'; if (incorrectSequences[i][j] != 0) cout << incorrectSequences[i][j] << " wrong " << j + 1 << '\n';*/ } } ll total = 0; for (ll j = 0; j < length; j++) { total += correctSequences[length - 1][j]; total %= mod; } cout << total << '\n'; } |