#include <iostream> #include <string> #include <vector> #include <utility> #include <limits> std::ostream& operator<<(std::ostream& out, std::vector<std::vector<long long int>> v) { for(int i = 0; i < v.size(); i++) { for(int j = 0; j < v[i].size(); j++) { std::cout << v[i][j] << "\t"; } std::cout << std::endl; } return out; } int main() { std::ios::sync_with_stdio(false); long long int n, m, p; std::cin >> n >> m >> p; std::vector<std::vector<long long int>> comb_index_is_end(n, std::vector<long long int>(m, 0)); std::vector<std::vector<long long int>> comb_index_is_end_multiplied_sum(n, std::vector<long long int>(m, 0)); std::vector<std::vector<long long int>> comb_index_present(n, std::vector<long long int>(m, 0)); for(long long int j = 0; j < m; j++) { comb_index_is_end[0][j] = j+1; comb_index_is_end[0][j] %= p; } comb_index_is_end_multiplied_sum[0][0] = 1; for(long long int j = 1; j < m; j++) { comb_index_is_end_multiplied_sum[0][j] = (j+1) * (j+1) + comb_index_is_end_multiplied_sum[0][j-1]; comb_index_is_end_multiplied_sum[0][j] %= p; } for(long long int j = 0; j < m; j++) { comb_index_present[0][j] = (j+1) * (m-j); comb_index_present[0][j] %= p; } for(long long int i = 1; i < n; i++) { for(long long int j = 0; j < m; j++) { if(j>0) comb_index_is_end[i][j] = comb_index_is_end_multiplied_sum[i-1][j-1]; else comb_index_is_end[i][j] = 0; long long int tmp = comb_index_present[i-1][j]; tmp *= (j+1); tmp %= p; comb_index_is_end[i][j] += tmp; comb_index_is_end[i][j] %= p; tmp = comb_index_present[i-1][j]; tmp *= (j+1); tmp %= p; tmp *= (m-j); tmp %= p; comb_index_present[i][j] = tmp; if(j>0) tmp = comb_index_is_end_multiplied_sum[i-1][j-1]; else tmp = 0; tmp *= (m-j); tmp %= p; comb_index_present[i][j] += tmp; if(j+1<m) tmp = comb_index_is_end_multiplied_sum[i-1][m-j-2]; else tmp = 0; tmp *= (j+1); tmp %= p; comb_index_present[i][j] += tmp; comb_index_present[i][j] %= p; } comb_index_is_end_multiplied_sum[i][0] = comb_index_is_end[i][0]; for(long long int j = 1; j < m; j++) { comb_index_is_end_multiplied_sum[i][j] = (j+1) * comb_index_is_end[i][j] + comb_index_is_end_multiplied_sum[i][j-1]; comb_index_is_end_multiplied_sum[i][j] %= p; } } long long int sum = 0; for(int j = 0; j < m; j++) { sum += comb_index_is_end[n-1][j]; sum %= p; } //std::cout << comb_index_is_end << std::endl; //std::cout << comb_index_is_end_multiplied_sum << std::endl; //std::cout << comb_index_present << std::endl; std::cout << sum << std::endl; return 0; }
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 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 | #include <iostream> #include <string> #include <vector> #include <utility> #include <limits> std::ostream& operator<<(std::ostream& out, std::vector<std::vector<long long int>> v) { for(int i = 0; i < v.size(); i++) { for(int j = 0; j < v[i].size(); j++) { std::cout << v[i][j] << "\t"; } std::cout << std::endl; } return out; } int main() { std::ios::sync_with_stdio(false); long long int n, m, p; std::cin >> n >> m >> p; std::vector<std::vector<long long int>> comb_index_is_end(n, std::vector<long long int>(m, 0)); std::vector<std::vector<long long int>> comb_index_is_end_multiplied_sum(n, std::vector<long long int>(m, 0)); std::vector<std::vector<long long int>> comb_index_present(n, std::vector<long long int>(m, 0)); for(long long int j = 0; j < m; j++) { comb_index_is_end[0][j] = j+1; comb_index_is_end[0][j] %= p; } comb_index_is_end_multiplied_sum[0][0] = 1; for(long long int j = 1; j < m; j++) { comb_index_is_end_multiplied_sum[0][j] = (j+1) * (j+1) + comb_index_is_end_multiplied_sum[0][j-1]; comb_index_is_end_multiplied_sum[0][j] %= p; } for(long long int j = 0; j < m; j++) { comb_index_present[0][j] = (j+1) * (m-j); comb_index_present[0][j] %= p; } for(long long int i = 1; i < n; i++) { for(long long int j = 0; j < m; j++) { if(j>0) comb_index_is_end[i][j] = comb_index_is_end_multiplied_sum[i-1][j-1]; else comb_index_is_end[i][j] = 0; long long int tmp = comb_index_present[i-1][j]; tmp *= (j+1); tmp %= p; comb_index_is_end[i][j] += tmp; comb_index_is_end[i][j] %= p; tmp = comb_index_present[i-1][j]; tmp *= (j+1); tmp %= p; tmp *= (m-j); tmp %= p; comb_index_present[i][j] = tmp; if(j>0) tmp = comb_index_is_end_multiplied_sum[i-1][j-1]; else tmp = 0; tmp *= (m-j); tmp %= p; comb_index_present[i][j] += tmp; if(j+1<m) tmp = comb_index_is_end_multiplied_sum[i-1][m-j-2]; else tmp = 0; tmp *= (j+1); tmp %= p; comb_index_present[i][j] += tmp; comb_index_present[i][j] %= p; } comb_index_is_end_multiplied_sum[i][0] = comb_index_is_end[i][0]; for(long long int j = 1; j < m; j++) { comb_index_is_end_multiplied_sum[i][j] = (j+1) * comb_index_is_end[i][j] + comb_index_is_end_multiplied_sum[i][j-1]; comb_index_is_end_multiplied_sum[i][j] %= p; } } long long int sum = 0; for(int j = 0; j < m; j++) { sum += comb_index_is_end[n-1][j]; sum %= p; } //std::cout << comb_index_is_end << std::endl; //std::cout << comb_index_is_end_multiplied_sum << std::endl; //std::cout << comb_index_present << std::endl; std::cout << sum << std::endl; return 0; } |