Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- #define endl '\n'
- using namespace std;
- int n, m, k, x;
- int dp[(1 << 17) + 10][17];
- signed main() {
- ios_base::sync_with_stdio(0);
- cin.tie(0);
- cin >> n >> m >> k;
- x = (2 << m);
- for(int msk=0;msk<x;msk++) dp[msk][0] = 1;
- for(int i=0;i<n;i++) {
- for(int j=0;j<m-1;j++) {
- for(int msk=0;msk<x;msk++) {
- int a = ((msk & (1 << j)) > 0);
- int b = ((msk & (2 << j)) > 0);
- int c = ((msk & (4 << j)) > 0);
- if(a + b + c + 1 >= 2) {
- dp[msk | (2 << j)][j+1] += dp[msk][j];
- if(dp[msk | (2 << j)][j+1] >= k) dp[msk | (2 << j)][j+1] -= k;
- }
- if(a + b + c >= 2) {
- dp[(msk | (2 << j)) - (2 << j)][j+1] += dp[msk][j];
- if(dp[(msk | (2 << j)) - (2 << j)][j+1] >= k) dp[(msk | (2 << j)) - (2 << j)][j+1] -= k;
- }
- }
- }
- if(i == n-1) break;
- for(int msk=0;msk<x;msk++) dp[msk][0] = 0;
- for(int msk=0;msk<x;msk++) {
- int newmsk = ((msk << 1) | (2 << m)) - (2 << m);
- dp[newmsk][0] += dp[msk][m-1];
- if(dp[newmsk][0] >= k) dp[newmsk][0] -= k;
- dp[newmsk|1][0] += dp[msk][m-1];
- if(dp[newmsk|1][0] >= k) dp[newmsk|1][0] -= k;
- }
- for(int j=1;j<m;j++) for(int msk=0;msk<x;msk++) dp[msk][j] = 0;
- }
- int ans = 0;
- for(int msk=0;msk<x;msk+=2) {
- ans += dp[msk][0];
- if(ans >= k) ans -= k;
- }
- cout << ans << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement