#include <iostream> #include <algorithm> #include <cmath> #include <vector> #include <string> #include <map> #include <unordered_map> #include <set> #include <unordered_set> #include <queue> #include <deque> #include <climits> using namespace std; void input(int &n, int &m, int &p) { cin >> n >> m >> p; } int solve (int &n, int &m, int &p) { vector<long long> numbers, tmp; for (long long k = 1; k < pow(2, m); k *= 2) tmp.push_back(k); for (int i = 0; i < tmp.size(); i++) { long long sum = 0; for (int j = i; j < tmp.size(); j++) { sum += tmp[j]; numbers.push_back(sum); } } int M = numbers.size(); vector<vector<int>> G(M); for (int i = 0; i < M; i++) { for (int j = i; j < M; j++) { if (numbers[i] & numbers[j]) { G[i].push_back(j); if (i != j) G[j].push_back(i); } } } long long dp[n + 1][M]; for (int i = 0; i < n + 1; i++) for (int j = 0; j < M; j++) dp[i][j] = 0; for (int i = 0; i < M; i++) dp[1][i] = 1; for (int i = 1; i < n; i++) { for (int j = 0; j < M; j++) { if (dp[i][j] == 0) continue; for (int q : G[j]) { dp[i + 1][q] += dp[i][j]; dp[i + 1][q] %= p; } } } long long result = 0; for (int i = 0; i < M; i++) result = (result + dp[n][i]) % p; return result; } int main() { ios_base::sync_with_stdio(0); int n, m, p; input(n, m, p); cout << solve(n, m, p) << "\n"; 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 | #include <iostream> #include <algorithm> #include <cmath> #include <vector> #include <string> #include <map> #include <unordered_map> #include <set> #include <unordered_set> #include <queue> #include <deque> #include <climits> using namespace std; void input(int &n, int &m, int &p) { cin >> n >> m >> p; } int solve (int &n, int &m, int &p) { vector<long long> numbers, tmp; for (long long k = 1; k < pow(2, m); k *= 2) tmp.push_back(k); for (int i = 0; i < tmp.size(); i++) { long long sum = 0; for (int j = i; j < tmp.size(); j++) { sum += tmp[j]; numbers.push_back(sum); } } int M = numbers.size(); vector<vector<int>> G(M); for (int i = 0; i < M; i++) { for (int j = i; j < M; j++) { if (numbers[i] & numbers[j]) { G[i].push_back(j); if (i != j) G[j].push_back(i); } } } long long dp[n + 1][M]; for (int i = 0; i < n + 1; i++) for (int j = 0; j < M; j++) dp[i][j] = 0; for (int i = 0; i < M; i++) dp[1][i] = 1; for (int i = 1; i < n; i++) { for (int j = 0; j < M; j++) { if (dp[i][j] == 0) continue; for (int q : G[j]) { dp[i + 1][q] += dp[i][j]; dp[i + 1][q] %= p; } } } long long result = 0; for (int i = 0; i < M; i++) result = (result + dp[n][i]) % p; return result; } int main() { ios_base::sync_with_stdio(0); int n, m, p; input(n, m, p); cout << solve(n, m, p) << "\n"; return 0; } |