#include <bits/stdc++.h>
using namespace std;
vector<int> pasujace[1<<20];
vector<int> dp[1<<20];
int m;
bool f(int x) {
bool bylo = false;
bool jest = false;
for(int i=0;i<m;i++) {
if(x & (1<<i)) {
if(jest == false and bylo == false) {
jest = true;
bylo = true;
} else if(jest == false and bylo == true) {
return false;
}
} else {
if(bylo == true) {
jest = false;
}
}
}
return true;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, p;
cin >> n >> m >> p;
for(int i=0;i<(1<<m);i++) {
for(int j=0;j<(1<<m);j++) {
if((i & j) and f(i) and f(j)) {
pasujace[i].push_back(j);
}
}
}
dp[0].push_back(0);
for(int i=1;i<(1<<m);i++) {
if(f(i))
dp[i].push_back(1);
else
dp[i].push_back(0);
}
for(int i=1;i<n;i++) {
for(int j=0;j<(1<<m);j++) {
dp[j].push_back(0);
for(int maska:pasujace[j]) {
dp[j][i] += dp[maska][i-1];
dp[j][i] %= p;
}
}
}
int result = 0;
for(int i=0;i<(1<<m);i++) {
result+=dp[i][n-1];
result%=p;
}
cout << result << "\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 58 59 60 61 62 63 64 | #include <bits/stdc++.h> using namespace std; vector<int> pasujace[1<<20]; vector<int> dp[1<<20]; int m; bool f(int x) { bool bylo = false; bool jest = false; for(int i=0;i<m;i++) { if(x & (1<<i)) { if(jest == false and bylo == false) { jest = true; bylo = true; } else if(jest == false and bylo == true) { return false; } } else { if(bylo == true) { jest = false; } } } return true; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, p; cin >> n >> m >> p; for(int i=0;i<(1<<m);i++) { for(int j=0;j<(1<<m);j++) { if((i & j) and f(i) and f(j)) { pasujace[i].push_back(j); } } } dp[0].push_back(0); for(int i=1;i<(1<<m);i++) { if(f(i)) dp[i].push_back(1); else dp[i].push_back(0); } for(int i=1;i<n;i++) { for(int j=0;j<(1<<m);j++) { dp[j].push_back(0); for(int maska:pasujace[j]) { dp[j][i] += dp[maska][i-1]; dp[j][i] %= p; } } } int result = 0; for(int i=0;i<(1<<m);i++) { result+=dp[i][n-1]; result%=p; } cout << result << "\n"; } |
English