Problem:
F: グリッドの番号
問題
えびちゃんは、横
n
列、縦
2
行のグリッドに
1
から
2 \times n
までの整数をちょうど
1
つずつ書き込もうとしています。
グリッドの各マスにはちょうど
1
つの整数しか書き込めません。
普通に書き込むだけでは面白くないので、以下のようなルールを定めました。
隣り合う
2
つのマスに書き込まれた整数の、差の絶対値は
k
以下である。
あるマスの右にマスが存在する時、あるマスに書き込まれた整数は、右のマスに書き込まれた整数より真に小さい。
あるマスの下にマスが存在する時、あるマスに書き込まれた整数は、下のマスに書き込まれた整数より真に小さい。
ここで、隣り合う
2
つのマスとは、あるマスと上下左右の辺を共有するマスのことを表します。
このような書き込み方は何通りあるでしょうか?
答えが非常に大きくなる可能性があるので、素数
m
で割った余りを出力してください。
補足
上記の
2
番目のルールと
3
番目のルールは、以下の図のように、不等号に違反しないように整数を書き込むことを要求しています。
入力形式
入力として整数が 3 つ与えられます。
n
k
m
制約
1 \leq n \leq 100
1 \leq k \leq 10
2 \leq m \leq 10^9+7
m
は素数
出力形式
数字の書き込み方の通り数を一行に出力してください。
また、末尾の改行を忘れないように注意してください。
入力例 1
3 2 7
出力例 1
1
上記の書き込み方が条件を満たし、これ以外に条件を満たす書き込み方は存在しません。
入力例 2
5 10 11
出力例 2
9
m
で割った余りを出力してください。

Submitted solutions:

code_snippet: // AOJ #2965 Grid Number
// 2025.5.28

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

int main(){
    int n, k;
    ll m;
    cin >> n >> k >> m;
    int N = 2 * n;
    ll B  = k + 2;
    ll B2 = k + 2;

    unordered_map<ll, ll> cur, nxt;
    cur.reserve(100000);
    nxt.reserve(100000);
    ll init_key = ((0 * B2 + 0) * B2 + 0);
    cur[init_key] = 1;

    for(int p = 0; p < N; p++){
        nxt.clear();
        for(auto &kv : cur){
            ll key = kv.first, cnt = kv.second;
            ll tmp = key;
            int b = (tmp % B2) - 1; tmp /= B2;
            int t = (tmp % B2) - 1; tmp /= B2;
            ll code_v = tmp;
            vector<int> v;
            ll tt = code_v;
            while(tt){
                v.push_back((tt % B) - 1);
                tt /= B;
            }
            int sz = v.size();

            if(sz < 2*n - p && (t < 0 || t < k) && (sz == 0 || v[0] < k)) {
                vector<int> u(sz+1);
                for(int i = 0; i < sz; i++) u[i] = v[i] + 1;
                u[sz] = 0;
                ll nc = 0;
                for(int i = sz; i >= 0; i--) nc = nc * B + (u[i] + 1);
                int t2 = 0;
                int b2 = (b < 0 ? -1 : min(b + 1, k));
                ll k2 = ((nc * B2 + (t2 + 1)) * B2 + (b2 + 1));
                nxt[k2] = (nxt[k2] + cnt) % m;
            }
            if(sz > 0 && v[0] < k && (b < 0 || b < k)) {
                vector<int> u(sz-1);
                for(int i = 1; i < sz; i++) u[i-1] = v[i] + 1;
                ll nc = 0;
                for(int i = (int)u.size()-1; i >= 0; i--) nc = nc * B + (u[i] + 1);
                int t2 = (t < 0 ? -1 : min(t + 1, k));
                int b2 = 0;
                ll k2 = ((nc * B2 + (t2 + 1)) * B2 + (b2 + 1));
                nxt[k2] = (nxt[k2] + cnt) % m;
            }
        }
        cur.swap(nxt);
    }
    ll ans = 0;
    for(auto &kv : cur){
        ll key = kv.first, cnt = kv.second;
        if(key / (B2*B2) == 0) ans = (ans + cnt) % m;
    }
    cout << ans << endl;
    return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 4836
final_rank: 1

code_snippet: #define _GLIBCXX_SHARED_MUTEX 1
#include<bits/stdc++.h>

using namespace std;

#define int long long

constexpr long long INF = 1e18;
constexpr long long MOD = 1000000007; 

struct fast_io {
  fast_io(){
    // std::cin.tie(nullptr);
    // std::ios::sync_with_stdio(false);
    cout<<fixed<<setprecision(14);
  };
} fio;

int solve(int n, int k, int m){
  vector<vector<vector<int>>> dp;
  vector<vector<vector<int>>> bit;
  int ans = 0, Z = k;
  
  dp.resize(k+1, vector<vector<int>>(k*2+1, vector<int>(1<<Z)));
  bit.resize(1<<Z, vector<vector<int>>(2));
  
  for(int i = 0; i < 1<<Z; i++){
    for(int j = 0; j < Z; j++){
      bit[i][(i>>j)&1].push_back(j);
    }
  }
  
  dp[0][0][1] = 1;
  
  for(int i = 1; i <= n; i++){
    
    vector<vector<vector<int>>> ndp(k+1, vector<vector<int>>(k*2+1, vector<int>(1<<Z)));
    
    int Y = max(0ll, i-k);
    int nY = max(0ll, i+1-k);
    for(int j = Y; j <= i; j++) {
      int X = max(0ll, i+j-k*2);
      int nX = max(0ll, i+j+1-k*2);
      for(int o = X; o <= i+j; o++) {
        for(int l = 0; l < (1<<Z); l++){
          
          do{
            if(j + 1 > i || !l) break;
            if((bit[l][1].back() + 1) > k) break; 
            int v = (l^(1<<bit[l][1].back()))<<1, w = i+j-bit[l][1].front();
            if(j && !bit[l][1].front()){ 
              if(i+j+1 - o > k) break;
            }
            
            if(v >= (1<<Z) || w-nX < 0) break;
            dp[j+1-Y][w-nX][v] = (dp[j-Y][o-X][l] + dp[j+1-Y][w-nX][v]) % m;
          } while(false);
          
          do{
            if(i + 1 > n) break;
            
            int v = ((l<<1)|1), w = o;
            
            if(l == 0) {
              if(i+j+1 - o > k) break;
              w = i+j;
            } else {
              if((bit[l][1].front() + 1) > k) break;
              if(bit[l][1].front()){
                w = i+j;
              }
            }
            
            if(v >= (1<<Z) || j-nY < 0 || w-nX < 0) break;
            ndp[j-nY][w-nX][v] = (dp[j-Y][o-X][l] + ndp[j-nY][w-nX][v]) % m;
          } while(false);
        }
      }
    }
    
    if(n != i) swap(dp, ndp);
  }
  
  for(int i = 0; i < 1<<Z; i++) {
    for(int j = 0; j <= k*2; j++){
      ans += dp[n-max(0ll, n-k)][j][i];
      ans %= m;
    }
  }
  
  return ans;
}

signed main(){
  int n, k, m;
  
  cin>>n>>k>>m;
  
  cout<<solve(n, k, m)<<endl;
  
  return 0;
}
accuracy: 1
time_ms: 270
memory_kb: 7268
final_rank: 3

code_snippet: #define _GLIBCXX_SHARED_MUTEX 1
#include<bits/stdc++.h>

using namespace std;

#define int long long

constexpr long long INF = 1e18;
constexpr long long MOD = 1000000007; 

using ll = long long;
#define var auto

struct fast_io {
  fast_io(){
    // std::cin.tie(nullptr);
    // std::ios::sync_with_stdio(false);
    cout<<fixed<<setprecision(14);
  };
} fio;

int solve(int n, int k, int m){
  vector<vector<vector<int>>> dp;
  vector<vector<vector<int>>> bit;
  int ans = 0, X = k+4;
  
  dp.resize(n+1, vector<vector<int>>(n+1, vector<int>(1<<X)));
  bit.resize(1<<X, vector<vector<int>>(2));
  
  for(int i = 0; i < 1<<X; i++){
    for(int j = 0; j < X; j++){
      bit[i][(i>>j)&1].push_back(j);
    }
  }
  
  dp[1][0][1] = 1;
  
  for(int i = 1; i <= n; i++){
    for(int j = max(0ll, i-k); j <= i; j++){
      for(int l = 1; l+1 < (1<<X); l++){
        
        do{
          if(j + 1 > i) break;
          int v = l;
          if(j == 0) {
            if((bit[l][1].back() + 1) > k) break;
          } else {
            if((bit[l][0].front() + 1) > k) break;
            if((bit[l][1][bit[l][1].size()-2] + 1) > k) break;
            v ^= 1<<bit[l][1].back(); 
          }
          
          v <<= 1;
          if(v >= (1<<X)) break;
          dp[i][j+1][v] = (dp[i][j][l] + dp[i][j+1][v]) % m;
        } while(false);
        
        do{
          if(i + 1 > n) break;
          if(bit[l][1].size() && (bit[l][1].front() + 1) > k) break;
          int v = l; v <<= 1; v |= 1;
          if(v >= (1<<X)) break;
          dp[i+1][j][v] = (dp[i][j][l] + dp[i+1][j][v]) % m;
        } while(false);
        
      }
    }
  }
  for(int i = 0; i < 1<<X; i++){
    ans += dp[n][n][i];
    ans %= m;
  }
  return ans;
}

signed main(){
  int n, k, m;
  
  cin>>n>>k>>m;
  
  cout<<solve(n, k, m)<<endl;
  
  return 0;
}
accuracy: 0.07692307692307693
time_ms: 10
memory_kb: 12056
final_rank: 20

code_snippet: #include<bits/stdc++.h>
using ll = long long;
#define var auto
const char newl = '\n';

using namespace std;

template<typename T1, typename T2> inline void chmin(T1& a, T2 b) { if (a > b) a = b; }
template<typename T1, typename T2> inline void chmax(T1& a, T2 b) { if (a < b) a = b; }

template<typename T>
struct Rint {
  static T mod;
  static void set_mod(T nmod) { mod = nmod; }

  T v;
  Rint() :v(0) {}
  Rint(signed v) :v(v) {}
  Rint(long long t) { v = t % mod; if (v < 0) v += mod; }

  Rint pow(long long k) {
    Rint res(1), tmp(v);
    while (k) {
      if (k & 1) res *= tmp;
      tmp *= tmp;
      k >>= 1;
    }
    return res;
  }

  static Rint add_identity() { return Rint(0); }
  static Rint mul_identity() { return Rint(1); }

  Rint inv() { return pow(mod - 2); }

  Rint& operator+=(Rint a) { v += a.v; if (v >= mod)v -= mod; return *this; }
  Rint& operator-=(Rint a) { v += mod - a.v; if (v >= mod)v -= mod; return *this; }
  Rint& operator*=(Rint a) { v = 1LL * v * a.v % mod; return *this; }
  Rint& operator/=(Rint a) { return (*this) *= a.inv(); }

  Rint operator+(Rint a) const { return Rint(v) += a; }
  Rint operator-(Rint a) const { return Rint(v) -= a; }
  Rint operator*(Rint a) const { return Rint(v) *= a; }
  Rint operator/(Rint a) const { return Rint(v) /= a; }

  Rint operator-() const { return v ? Rint(mod - v) : Rint(v); }

  bool operator==(const Rint a)const { return v == a.v; }
  bool operator!=(const Rint a)const { return v != a.v; }
  bool operator <(const Rint a)const { return v < a.v; }
};
template<typename T> T Rint<T>::mod;
template<typename T>
ostream& operator<<(ostream& os, Rint<T> m) { os << m.v; return os; }

using M = Rint<int>;

ll naive(int n, int k, int m) {
  vector<int> group(n * 2);
  for (int i = n; i < n * 2; i++) {
    group[i] = 1;
  }
  ll res = 0;
  vector<vector<int>> rows(2, vector<int>(n));
  int inds[2];
  do
  {
    inds[0] = 0;
    inds[1] = 0;
    for (int i = 0; i < n * 2; i++) {
      var id = group[i];
      rows[id][inds[id]] = i;
      if (inds[id] != 0) {
        var d = rows[id][inds[id]] - rows[id][inds[id] - 1];
        if (k < d) goto invalid;
      }
      inds[id]++;
    }
    for (int i = 0; i < n; i++) {
      if (rows[0][i] >= rows[1][i]) goto invalid;
      if (k < rows[1][i] - rows[0][i]) goto invalid;
    }
    res++;
  invalid:;
  } while (next_permutation(group.begin(), group.end()));
  return res % m;
}

ll solve(int n, int k, int m) {
  M::mod = m;

  vector<int> msb_table(1 << (k + 1));
  vector<int> nxtmsb_table(1 << (k + 1));
  for (int i = 0; i <= k; i++) {
    for (int b = (1 << i); b < (1 << (i + 1)); b++) {
      for (int j = i + 1; j <= k; j++) {
        nxtmsb_table[b | 1 << j] = 1 << i;
      }
      msb_table[b] = 1 << i;
    }
  }

  vector<vector<vector<M>>> dp(n + 1, vector<vector<M>>(n + 1, vector<M>(1 << k)));
  dp[1][0][1] = 1;
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j <= i; j++) {
      if (k < i - j) continue;
      if (j != 0 && k == i - j) continue;
      for (int b = 0; b < (1 << k); b++) {
        if (i > j) {
          if (not(i == j + 1 && i != n && (b >> (k - 1) & 1))) {
            dp[i][j + 1][(b - msb_table[b]) << 1] += dp[i][j][b];
          }
        }
        if (i != n && not (b >> (k - 1) & 1)) {
          dp[i + 1][j][b << 1 | 1] += dp[i][j][b];
        }
      }
    }
  }

  return dp[n][n][0].v;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  //for (int i = 1; i < 5; i++) {
  //  for (int j = 1; j < 10; j++) {
  //    var res = solve(i, j, 1e9 + 7);
  //    var ans = naive(i, j, 1e9 + 7);
  //    if (res != ans) {
  //      printf("%d %d\nexpected: %d\n  actual: %d\n", i, j, ans, res);
  //    }
  //  }
  //}

  int n, k, m;
  cin >> n >> k >> m;
  cout << solve(n, k, m) << endl;
}
accuracy: 1
time_ms: 10
memory_kb: 44516
final_rank: 4

code_snippet: #include<iostream>
#include<algorithm>
#include<vector>

using namespace std;

int N, k;
//int dp[200 + 10][100 + 10][1 << (10 + 1)];
//集合は、直近k+1個の数字を上(=1)or下(=0)においたときのbit集合

int m = 1000000007;

int cnt[30];

unsigned long long popcount(unsigned long long x) {
	x = ((x & 0xaaaaaaaaaaaaaaaaUL) >> 1) +
		(x & 0x5555555555555555UL);
	x = ((x & 0xccccccccccccccccUL) >> 2) +
		(x & 0x3333333333333333UL);
	x = ((x & 0xf0f0f0f0f0f0f0f0UL) >> 4) +
		(x & 0x0f0f0f0f0f0f0f0fUL);
	x = ((x & 0xff00ff00ff00ff00UL) >> 8) +
		(x & 0x00ff00ff00ff00ffUL);
	x = ((x & 0xffff0000ffff0000UL) >> 16) +
		(x & 0x0000ffff0000ffffUL);
	x = ((x & 0xffffffff00000000UL) >> 32) +
		(x & 0x00000000ffffffffUL);
	return x;
}

int main() {
	cin >> N >> k >> m;
	
	vector<vector<vector<int>>> dp(2 * N + 1, vector<vector<int>>(N + 1, vector<int>((1 << (k + 1)), 0)));
	dp[0][0][0] = 1;
	for (int i = 0; i < 2 * N; i++) {
		for (int j = (i + 1) / 2; j <= N; j++) {
			for (int bit = 0; bit < (1 << (k + 1)); bit++) {
				if (dp[i][j][bit] == 0)continue;

				//cout << i << " " << j << " " << bit << " " << dp[i][j][bit] << endl;	

				//今のメモした桁数。
				int keta = min(k + 1, i);
				if (keta == 0) {
					//i == 0, j == 0, bit == 0の時
					dp[i + 1][j + 1][1] = dp[i][j][bit];
					continue;
				}

				vector<int> ue;
				int shita = 0;
				for (int l = i; l >= i - keta + 1; l--) {
					if ((bit >> (i - l)) & 1) {
						//数字lを上に置いたとき
						ue.push_back(l);
					}
					else if (shita == 0) {
						shita = l;
					}
				}
				//下の制約がないとき
				if (shita == 0) {
					shita = i + 1;
				}

				int cpy = bit;
				//下に置く
				//j - (i - j) - 1がout of rangeする！
				if (i != 2 * j &&
					(abs(shita - (i + 1)) <= k
						&& abs(ue.at(j - (i - j) - 1) - (i + 1)) <= k)) {
					cpy = ((cpy << 1) & ((1 << (k + 1)) - 1));
					dp[i + 1][j][cpy] += dp[i][j][bit];
					dp[i + 1][j][cpy] %= m;
				}

				cpy = bit;
				//cout << i << " " << j << " " << bit << "A" << " " << ue.size() << endl;

				//cout << "ue == " << endl;
				//for (int i = 0; i < ue.size(); i++)cout << ue[i] << " ";
				//cout << endl;

				//条件が少しおかしいと思う
				//上に置く
				if ((j * 2 == i && abs(ue.front() - (i + 1)) <= k)|| 
					((j * 2 != i) && (i + 2 - shita <= k) && (i + 2 - ue.at(j - (i - j) - 1) <= k) && 
					abs(ue.front() - (i + 1)) <= k && j != N)) {
					cpy = ((cpy << 1) & ((1 << (k + 1)) - 1));
					cpy += (1 << 0);
					dp[i + 1][j + 1][cpy] += dp[i][j][bit];
					dp[i + 1][j + 1][cpy] %= m;
				}
			}
		}
	}
	int ans = 0;
	for (int i = 0; i < (1 << (k + 1)); i++) {
		ans += dp[2 * N][N][i];
		ans %= m;
	}
	cout << ans << endl;
	return 0;
}
accuracy: 1
time_ms: 150
memory_kb: 166712
final_rank: 9

code_snippet: #include <bits/stdc++.h>
using namespace std;

long long n, k, m;
vector<vector<vector<long long>>> dp;

long long solve();

int main() {
  cin >> n >> k >> m;
  cout << solve() << endl;
  return 0;
}

long long solve() {
  dp.assign(2 * n + 1,
            vector<vector<long long>>(
                n + 1, vector<long long>(1LL << k, 0)));
  dp[1][1][1] = 1;
  for(int i = 1; i < 2 * n; ++i) {
    for(int j = 1; j <= n; ++j) {
      for(int s = 0; s < (1LL << k); ++s)
        if(j >= i - j && j - (i - j) <= k) {
          int up = -100, upright = -1, down = 1, id = j;
          for(int l = 0; l < min(k, (long long)i); ++l) {
            if(!(s >> l & 1) && down == 1) down = i - l;
            if(s >> l & 1) {
              if(upright == -1) upright = i - l;
              if(id == i - j + 1) up = i - l;
              --id;
            }
          }
          if(i + 1 - upright <= k && j < n)
            (dp[i + 1][j + 1][(s << 1) % (1LL << k) + 1] +=
             dp[i][j][s]) %= m;
          if(i + 1 - min(up, down) <= k)
            (dp[i + 1][j][(s << 1) % (1LL << k)] +=
             dp[i][j][s]) %= m;
        }
    }
  }
  long long res = 0;
  for(int i = 0; i < (1LL << k); ++i)
    (res += dp[2 * n][n][i]) %= m;
  return res;
}
accuracy: 1
time_ms: 120
memory_kb: 166780
final_rank: 8

code_snippet: #include <iostream>
using namespace std;
typedef long long int ll;
ll mod;
ll dp[2][201][11][(1<<11)];
int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int N,K; cin >> N >> K >> mod;
    dp[0][0][1][1]=1;
    int kk=(1<<(1+K));
    for(int _=0;_<N;_++){
        for(int i=0;i<=2*N;i++){
            for(int j=0;j<=K;j++){
                for(int k=0;k<kk;k++){
                    dp[1][i][j][k]=0;
                }
            }
        }
        for(int i=0;i<2*N;i++){
            for(int j=1;j<=K;j++){
                for(int k=0;k<kk;k++){
                    if(dp[0][i][j][k]==0)continue;
                    int t=0;
                    while((k>>t)&1)t++;
                    if(t>K)continue;
                    int ii=i+t;
                    int kkk=(k>>t)+1;
                    for(int jj=1;jj<=K;jj++){
                        if((kkk>>jj)&1)continue;
                        if(ii+jj-i-j>K)continue;
                        if(i+j<ii+jj&&ii+jj<=2*N)(dp[1][ii][jj][kkk|(1<<jj)]+=dp[0][i][j][k])%=mod;
                    }
                }
            }
        }
        for(int i=0;i<=2*N;i++){
            for(int j=1;j<=K;j++){
                for(int k=0;k<kk;k++){
                    dp[0][i][j][k]=dp[1][i][j][k];
                }
            }
        }
    }
    ll ans=0;
    for(int i=0;i<2*N;i++){
        for(int j=1;j<=K;j++){
            for(int k=0;k<kk;k++){
                (ans+=dp[0][i][j][k])%=mod;
            }
        }
    }
    cout << ans << endl;
}
accuracy: 1
time_ms: 1820
memory_kb: 71472
final_rank: 13

code_snippet: #include <iostream>
using namespace std;
typedef long long int ll;
ll mod;
ll dp[2][201][11][(1<<11)];
int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int N,K; cin >> N >> K >> mod;
    dp[0][0][1][1]=1;
    int kk=(1<<(1+K));
    for(int _=0;_<N;_++){
        for(int i=0;i<201;i++){
            for(int j=0;j<11;j++){
                for(int k=0;k<kk;k++){
                    dp[1][i][j][k]=0;
                }
            }
        }
        for(int i=0;i<2*N;i++){
            for(int j=1;j<=K;j++){
                for(int k=0;k<kk;k++){
                    if(dp[0][i][j][k]==0)continue;
                    //cout << i << " " << j << " " << k << endl;
                    int t=0;
                    while((k>>t)&1)t++;
                    if(t>K)continue;
                    int ii=i+t;
                    int kkk=(k>>t)+1;
                    for(int jj=1;jj<=K;jj++){
                        if((kkk>>jj)&1)continue;
                        if(ii+jj-i-j>K)continue;
                        if(i+j<ii+jj&&ii+jj<=2*N)(dp[1][ii][jj][kkk|(1<<jj)]+=dp[0][i][j][k])%=mod;
                    }
                }
            }
        }
        for(int i=0;i<201;i++){
            for(int j=1;j<11;j++){
                for(int k=0;k<kk;k++){
                    //f(dp[1][i][j][k])cout << dp[1][i][j][k] << endl;
                    dp[0][i][j][k]=dp[1][i][j][k];
                }
            }
        }
    }
    ll ans=0;
    for(int i=0;i<2*N;i++){
        for(int j=1;j<=K;j++){
            for(int k=0;k<kk;k++){
                (ans+=dp[0][i][j][k])%=mod;
                //cout << dp[0][i][j][k] << endl;
            }
        }
    }
    cout << ans << endl;
}
accuracy: 1
time_ms: 1810
memory_kb: 71500
final_rank: 12

code_snippet: #include <bits/stdc++.h>
using namespace std;

unsigned long long popcount(unsigned long long x) {
  x = ((x & 0xaaaaaaaaaaaaaaaaUL) >> 1) +
      (x & 0x5555555555555555UL);
  x = ((x & 0xccccccccccccccccUL) >> 2) +
      (x & 0x3333333333333333UL);
  x = ((x & 0xf0f0f0f0f0f0f0f0UL) >> 4) +
      (x & 0x0f0f0f0f0f0f0f0fUL);
  x = ((x & 0xff00ff00ff00ff00UL) >> 8) +
      (x & 0x00ff00ff00ff00ffUL);
  x = ((x & 0xffff0000ffff0000UL) >> 16) +
      (x & 0x0000ffff0000ffffUL);
  x = ((x & 0xffffffff00000000UL) >> 32) +
      (x & 0x00000000ffffffffUL);
  return x;
}

long long n, k, m;
vector<vector<vector<long long>>> dp;

long long solve();

int main() {
  cin >> n >> k >> m;
  cout << solve() << endl;
  return 0;
}

long long solve() {
  dp.assign(2 * n + 1,
            vector<vector<long long>>(
                n + 1, vector<long long>(1LL << k, 0)));
  dp[1][1][1] = 1;
  for(int i = 1; i < 2 * n; ++i) {
    for(int j = 1; j <= n; ++j) {
      for(int s = 0; s < (1LL << k); ++s)
        if(j >= i - j && j - (i - j) <= k) {
          int up = -100, upright = -1, down = 1, id = j;
          for(int l = 0; l < min(k, (long long)i); ++l) {
            if(!(s >> l & 1) && down == 1) down = i - l;
            if(s >> l & 1) {
              if(upright == -1) upright = i - l;
              if(id == i - j + 1) up = i - l;
              --id;
            }
          }
          if(i + 1 - upright <= k && j < n)
            (dp[i + 1][j + 1][(s << 1) % (1LL << k) + 1] +=
             dp[i][j][s]) %= m;
          if(i + 1 - min(up, down) <= k)
            (dp[i + 1][j][(s << 1) % (1LL << k)] +=
             dp[i][j][s]) %= m;
        }
    }
  }
  long long res = 0;
  for(int i = 0; i < (1LL << k); ++i)
    (res += dp[2 * n][n][i]) %= m;
  return res;
}
accuracy: 1
time_ms: 110
memory_kb: 166732
final_rank: 7

code_snippet: #include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>

#include <iostream>
#include <complex>
#include <string>
#include <algorithm>
#include <numeric>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <bitset>

#include <functional>
#include <cassert>

typedef long long ll;
using namespace std;

#ifndef LOCAL
#define debug(x) ;
#else
#define debug(x) cerr << __LINE__ << " : " << #x << " = " << (x) << endl;

template <typename T1, typename T2>
ostream &operator<<(ostream &out, const pair<T1, T2> &p) {
  out << "{" << p.first << ", " << p.second << "}";
  return out;
}

template <typename T>
ostream &operator<<(ostream &out, const vector<T> &v) {
  out << '{';
  for (const T &item : v) out << item << ", ";
  out << "\b\b}";
  return out;
}
#endif

#define mod 1000000007 //1e9+7(prime number)
#define INF 1000000000 //1e9
#define LLINF 2000000000000000000LL //2e18
#define SIZE 210

ll dp[SIZE][SIZE][1<<10]; //現, 上, 履歴

int main(){
  int N, K, M;
  bool t[2][SIZE];

  cin >> N >> K >> M;

  dp[0][0][0] = 1;

  int mask = (1 << K) - 1;

  for (int i=0; i<=N*2; i++) {
    for (int u=0; u<=i && u<=N; u++) {
      for (int r=0; r<(1<<K); r++) {
        if (!dp[i][u][r]) continue;

        int d = i - u;

        t[0][d] = false;
        t[1][u] = false;
        if (u > 0) t[0][u-1] = false;
        if (d > 0) t[1][d-1] = false;

        int tmpU = u, tmpD = d;

        for (int j=0; j<K && j<i; j++) {
          if (r & (1<<j)) {
            tmpU--;
            t[0][tmpU] = true;
          } else {
            tmpD--;
            t[1][tmpD] = true;
          }
        }

        //上
        if (u < N && (u == 0 || t[0][u-1])) {
          dp[i+1][u+1][((r << 1)&mask) + 1] += dp[i][u][r];
          dp[i+1][u+1][((r << 1)&mask) + 1] %= M;
        }

        //下
        if (d < N && d < u && t[0][d] && (d == 0 || t[1][d-1])) {
          dp[i+1][u][((r << 1)&mask) + 0] += dp[i][u][r];
          dp[i+1][u][((r << 1)&mask) + 0] %= M;
        }
      }
    }
  }

  ll ans = 0;

  for (int i=0; i<(1<<K); i++) {
    ans += dp[N*2][N][i];
  }

  cout << ans % M << endl;

  return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 30892
final_rank: 2

code_snippet: #include<bits/stdc++.h>
using namespace std;
using Int = long long;
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}

int MOD = 1000000007;
template<typename T>
struct Mint{
  T v;
  Mint():v(0){}
  Mint(signed v):v(v){}
  Mint(long long t){v=t%MOD;if(v<0) v+=MOD;}

  Mint pow(long long k){
    Mint res(1),tmp(v);
    while(k){
      if(k&1) res*=tmp;
      tmp*=tmp;
      k>>=1;
    }
    return res;
  }

  static Mint add_identity(){return Mint(0);}
  static Mint mul_identity(){return Mint(1);}

  Mint inv(){return pow(MOD-2);}

  Mint& operator+=(Mint a){v+=a.v;if(v>=MOD)v-=MOD;return *this;}
  Mint& operator-=(Mint a){v+=MOD-a.v;if(v>=MOD)v-=MOD;return *this;}
  Mint& operator*=(Mint a){v=1LL*v*a.v%MOD;return *this;}
  Mint& operator/=(Mint a){return (*this)*=a.inv();}

  Mint operator+(Mint a) const{return Mint(v)+=a;};
  Mint operator-(Mint a) const{return Mint(v)-=a;};
  Mint operator*(Mint a) const{return Mint(v)*=a;};
  Mint operator/(Mint a) const{return Mint(v)/=a;};

  Mint operator-() const{return v?Mint(MOD-v):Mint(v);}

  bool operator==(const Mint a)const{return v==a.v;}
  bool operator!=(const Mint a)const{return v!=a.v;}
  bool operator <(const Mint a)const{return v <a.v;}
};
template<typename T>
ostream& operator<<(ostream &os,Mint<T> m){os<<m.v;return os;}

//INSERT ABOVE HERE

using M = Mint<int>;
M dp[202][11][1<<10]={};
M nx[202][11][1<<10]={};

signed main(){
  int n,k;
  cin>>n>>k>>MOD;

  for(int i=0;i<k;i++)
    dp[1][i][1<<i]=1;

  for(int i=1;i<n;i++){
    for(int a=0;a<202;a++)
      for(int b=0;b<11;b++)
        for(int c=0;c<(1<<10);c++)
          nx[a][b][c]=M(0);

    for(int a=0;a<202;a++){
      for(int b=0;b<11;b++){
        for(int c=0;c<(1<<10);c++){
          if(dp[a][b][c].v==0) continue;
          int na=a+1;
          while((c>>(na-(a+1)))&1) na++;
          if(na>a+k) continue;

          int nc=c>>(na-a);
          for(int nb=0;nb<k;nb++){
            if((nc>>nb)&1) continue;
            if(a+1+b>=na+1+nb) continue;
            if(a+1+b+k<na+1+nb) continue;
            nx[na][nb][nc|(1<<nb)]+=dp[a][b][c];
          }
        }
      }
    }

    for(int a=0;a<202;a++)
      for(int b=0;b<11;b++)
        for(int c=0;c<(1<<10);c++)
          dp[a][b][c]=nx[a][b][c];
  }

  M ans{0};
  for(int a=0;a<202;a++){
    for(int b=0;b<11;b++){
      for(int c=0;c<(1<<10);c++){
        if(dp[a][b][c].v==0) continue;
        int na=a+1;
        while((c>>(na-(a+1)))&1) na++;
        int nc=c>>(na-a);
        if((a+1+b==2*n)&&(na==2*n+1)&&(nc==0)) ans+=dp[a][b][c];
      }
    }
  }
  cout<<ans<<endl;
  return 0;
}
accuracy: 1
time_ms: 440
memory_kb: 20884
final_rank: 6

code_snippet: #include<bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 99999999999999999
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;


#define SIZE 11

int POW[SIZE];
int N,K;
ll dp[205][SIZE][SIZE][1024]; //dp[最後に処理した数][最後に処理した数-左下の数][左下の数-その上の数][上の行の状態] = 場合の数
ll M;

int calc_state(int base_number,int work[2][SIZE]){

	int base_col = 0;
	if(work[1][1] != 0){

		base_col = 1;
	}

	int ret = 0;

	for(int col = base_col+1; col <= K; col++){
		if(work[0][col] == 0)break;

		ret += POW[base_number-work[0][col]];
	}

	return ret;
}

ll recursive(int last_number,int left_under_diff,int left_vertical_diff,int top_state){

	if(dp[last_number][left_under_diff][left_vertical_diff][top_state] != -1){

		return dp[last_number][left_under_diff][left_vertical_diff][top_state];
	}

	int work[2][SIZE];

	for(int row = 0; row < 2; row++){
		for(int col = 0; col < SIZE; col++){
			work[row][col] = 0;
		}
	}

	//左下の数
	int left_under = last_number-left_under_diff;
	work[1][0] = left_under;
	//その上の数
	work[0][0] = left_under-left_vertical_diff;

	//状態の復元
	int top_index = 0;
	for(int diff = (K-1); diff >= 0; diff--){

		if(top_state & (1 << (diff))){

			work[0][1+top_index++] = last_number-diff;
		}
	}

	if(last_number == 2*N){

		if(left_under == 2*N && top_index == 0){

			return 1;
		}else{

			return 0;
		}
	}

	ll ret = 0;

	bool FLG = true;

	//左下更新
	if(top_index == 0){ //上段の列数==下段の列数

		if(work[0][0] != 0 && last_number+1-work[0][0] > K){

			FLG = false;
		}

	}else{
		if(left_under == 0){

			work[1][1] = last_number+1;
			int next_state = calc_state(last_number+1,work);
			if(next_state >= POW[K]){

				//BUG

			}else{
				ret = recursive(last_number+1,0,work[1][1]-work[0][1],next_state);
				work[1][1] = 0;
			}

		}else{

			int tmp = min(work[1][0],work[0][1]);
			if((last_number+1)-tmp > K){

				FLG = false; //差がKより大きくなるので不可
			}else{

				work[1][1] = last_number+1;
				int next_state = calc_state(last_number+1,work);
				if(next_state >= POW[K]){

					//BUG

				}else{
					ret = recursive(last_number+1,0,work[1][1]-work[0][1],next_state);
					work[1][1] = 0;
				}
			}
		}
	}

	//上の行の右端更新
	if(FLG == true && top_index <= K-1 && (last_number+1-work[0][top_index] <= K)){

		work[0][1+top_index] = last_number+1;

		int next_state = calc_state(last_number+1,work);
		if(next_state >= POW[K]){

			//BUG

		}else{

			ret += recursive(last_number+1,last_number+1-left_under,left_vertical_diff,next_state);
			ret %= M;
		}
	}

	return dp[last_number][left_under_diff][left_vertical_diff][top_state] = ret;
}

int main(){

	POW[0] = 1;
	for(int i = 1; i < SIZE; i++){

		POW[i] = POW[i-1]*2;
	}

	scanf("%d %d %lld",&N,&K,&M);

	for(int num = 0; num <= 2*N; num++){
		for(int diff = 0; diff <= K; diff++){
			for(int diff_2 = 0; diff_2 <= K; diff_2++){
				for(int state = 0; state < POW[K]; state++){

					dp[num][diff][diff_2][state] = -1;
				}
			}
		}
	}

	printf("%lld\n",recursive(0,0,0,0)%M);

	return 0;
}
accuracy: 1
time_ms: 60
memory_kb: 197784
final_rank: 10

code_snippet: #include<bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 99999999999999999
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;


#define SIZE 11

int POW[SIZE];
int N,K;
ll dp[205][SIZE][SIZE][1024]; //dp[最後に処理した数][最後に処理した数-左下の数][左下の数-その上の数][上の行の状態] = 場合の数
ll M;

int calc_state(int base_number,int work[2][SIZE]){

	int base_col = 0;
	if(work[1][1] != 0){

		base_col = 1;
	}

	int ret = 0;

	for(int col = base_col+1; col <= K; col++){
		if(work[0][col] == 0)break;

		ret += POW[base_number-work[0][col]];
	}

	return ret;
}

ll recursive(int last_number,int left_under_diff,int left_vertical_diff,int top_state){

	if(dp[last_number][left_under_diff][left_vertical_diff][top_state] != -1){

		return dp[last_number][left_under_diff][left_vertical_diff][top_state];
	}

	int work[2][SIZE];

	for(int row = 0; row < 2; row++){
		for(int col = 0; col < SIZE; col++){
			work[row][col] = 0;
		}
	}

	//左下の数
	int left_under = last_number-left_under_diff;
	work[1][0] = left_under;
	//その上の数
	work[0][0] = left_under-left_vertical_diff;

	//状態の復元
	int top_index = 0;
	for(int diff = (K-1); diff >= 0; diff--){

		if(top_state & (1 << (diff))){

			work[0][1+top_index++] = last_number-diff;
		}
	}

	if(last_number == 2*N){

		if(left_under == 2*N && top_index == 0){

			return 1;
		}else{

			return 0;
		}
	}

	ll ret = 0;

	bool FLG = true;

	//左下更新
	if(top_index == 0){ //上段の列数==下段の列数

		if(work[0][0] != 0 && last_number+1-work[0][0] > K){

			FLG = false;
		}

	}else{
		if(left_under == 0){

			work[1][1] = last_number+1;
			int next_state = calc_state(last_number+1,work);
			if(next_state >= POW[K]){

				//BUG

			}else{
				ret = recursive(last_number+1,0,work[1][1]-work[0][1],next_state);
				work[1][1] = 0;
			}

		}else{

			int tmp = min(work[1][0],work[0][1]);
			if((last_number+1)-tmp > K){

				FLG = false; //差がKより大きくなるので不可
			}else{

				work[1][1] = last_number+1;
				int next_state = calc_state(last_number+1,work);
				if(next_state >= POW[K]){

					//BUG

				}else{
					ret = recursive(last_number+1,0,work[1][1]-work[0][1],next_state);
					work[1][1] = 0;
				}
			}
		}
	}

	//上の行の右端更新
	if(FLG == true && top_index <= K-1 && (last_number+1-work[0][top_index] <= K)){

		work[0][1+top_index] = last_number+1;

		int next_state = calc_state(last_number+1,work);
		if(next_state > POW[K]){

			//BUG

		}else{

			ret += recursive(last_number+1,last_number+1-left_under,left_vertical_diff,next_state);
			ret %= M;
		}
	}

	return dp[last_number][left_under_diff][left_vertical_diff][top_state] = ret;
}

int main(){

	POW[0] = 1;
	for(int i = 1; i < SIZE; i++){

		POW[i] = POW[i-1]*2;
	}

	scanf("%d %d %lld",&N,&K,&M);

	for(int num = 0; num <= 2*N; num++){
		for(int diff = 0; diff <= K; diff++){
			for(int diff_2 = 0; diff_2 <= K; diff_2++){
				for(int state = 0; state < POW[K]; state++){

					dp[num][diff][diff_2][state] = -1;
				}
			}
		}
	}

	printf("%lld\n",recursive(0,0,0,0)%M);

	return 0;
}
accuracy: 1
time_ms: 70
memory_kb: 197692
final_rank: 11

code_snippet: #include<bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 99999999999999999
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;


#define SIZE 11

int POW[SIZE];
int N,K;
ll dp[205][SIZE][SIZE][1024]; //dp[最後に処理した数][最後に処理した数-左下の数][左下の数-その上の数][上の行の状態] = 場合の数
ll M;

int calc_state(int base_number,int work[2][SIZE]){

	int base_col = 0;
	if(work[1][1] != 0){

		base_col = 1;
	}

	int ret = 0;

	for(int col = base_col+1; col <= K; col++){
		if(work[0][col] == 0)break;

		ret += POW[base_number-work[0][col]];
	}

	return ret;
}

ll recursive(int last_number,int left_under_diff,int left_vertical_diff,int top_state){

	if(dp[last_number][left_under_diff][left_vertical_diff][top_state] != -1){

		return dp[last_number][left_under_diff][left_vertical_diff][top_state];
	}

	int work[2][SIZE];

	for(int row = 0; row < 2; row++){
		for(int col = 0; col <= SIZE; col++){
			work[row][col] = 0;
		}
	}

	//左下の数
	int left_under = last_number-left_under_diff;
	work[1][0] = left_under;
	//その上の数
	work[0][0] = left_under-left_vertical_diff;

	//状態の復元
	int top_index = 0;
	for(int diff = (K-1); diff >= 0; diff--){

		if(top_state & (1 << (diff))){

			work[0][1+top_index++] = last_number-diff;
		}
	}

	if(last_number == 2*N){

		if(left_under == 2*N && top_index == 0){

			return 1;
		}else{

			return 0;
		}
	}

	ll ret = 0;

	bool FLG = true;

	//左下更新
	if(top_index == 0){ //上段の列数==下段の列数

		if(work[0][0] != 0 && last_number+1-work[0][0] > K){

			FLG = false;
		}

	}else{
		if(left_under == 0){

			work[1][1] = last_number+1;
			int next_state = calc_state(last_number+1,work);
			ret = recursive(last_number+1,0,work[1][1]-work[0][1],next_state);
			work[1][1] = 0;

		}else{

			int tmp = min(work[1][0],work[0][1]);
			if((last_number+1)-tmp > K){

				FLG = false; //差がKより大きくなるので不可
			}else{

				work[1][1] = last_number+1;
				int next_state = calc_state(last_number+1,work);
				ret = recursive(last_number+1,0,work[1][1]-work[0][1],next_state);
				work[1][1] = 0;
			}
		}
	}

	//上の行の右端更新
	if(FLG == true && top_index <= K-1 && (last_number+1-work[0][top_index] <= K)){

		work[0][1+top_index] = last_number+1;
		int next_state = calc_state(last_number+1,work);
		ret += recursive(last_number+1,last_number+1-left_under,left_vertical_diff,next_state);
		ret %= M;
	}

	return dp[last_number][left_under_diff][left_vertical_diff][top_state] = ret;
}

int main(){

	POW[0] = 1;
	for(int i = 1; i < SIZE; i++){

		POW[i] = POW[i-1]*2;
	}

	scanf("%d %d %lld",&N,&K,&M);

	for(int num = 0; num <= 2*N; num++){
		for(int diff = 0; diff <= K; diff++){
			for(int diff_2 = 0; diff_2 <= K; diff_2++){
				for(int state = 0; state < POW[K]; state++){

					dp[num][diff][diff_2][state] = -1;
				}
			}
		}
	}

	printf("%lld\n",recursive(0,0,0,0)%M);

	return 0;
}
accuracy: 0.3076923076923077
time_ms: 80
memory_kb: 153212
final_rank: 18

code_snippet: #include<bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 99999999999999999
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;


#define SIZE 11

int POW[SIZE];
int N,K;
ll dp[205][SIZE][SIZE][1024]; //dp[最後に処理した数][最後に処理した数-左下の数][左下の数-その上の数][上の行の状態] = 場合の数
ll M;

int calc_state(int base_number,int work[2][SIZE]){

	int base_col = 0;
	if(work[1][1] != 0){

		base_col = 1;
	}

	int ret = 0;

	for(int col = base_col+1; col <= K; col++){
		if(work[0][col] == 0)break;

		ret += POW[base_number-work[0][col]];
	}

	return ret;
}

ll recursive(int last_number,int left_under_diff,int left_vertical_diff,int top_state){

	if(dp[last_number][left_under_diff][left_vertical_diff][top_state] != -1){

		return dp[last_number][left_under_diff][left_vertical_diff][top_state];
	}

	int work[2][SIZE];

	for(int row = 0; row < 2; row++){
		for(int col = 0; col < SIZE; col++){
			work[row][col] = 0;
		}
	}

	//左下の数
	int left_under = last_number-left_under_diff;
	work[1][0] = left_under;
	//その上の数
	work[0][0] = left_under-left_vertical_diff;

	//状態の復元
	int top_index = 0;
	for(int diff = (K-1); diff >= 0; diff--){

		if(top_state & (1 << (diff))){

			work[0][1+top_index++] = last_number-diff;
		}
	}

	if(last_number == 2*N){

		if(left_under == 2*N && top_index == 0){

			return 1;
		}else{

			return 0;
		}
	}

	ll ret = 0;

	bool FLG = true;

	//左下更新
	if(top_index == 0){ //上段の列数==下段の列数

		if(work[0][0] != 0 && last_number+1-work[0][0] > K){

			FLG = false;
		}

	}else{
		if(left_under == 0){

			work[1][1] = last_number+1;
			int next_state = calc_state(last_number+1,work);
			ret = recursive(last_number+1,0,work[1][1]-work[0][1],next_state);
			work[1][1] = 0;

		}else{

			int tmp = min(work[1][0],work[0][1]);
			if((last_number+1)-tmp > K){

				FLG = false; //差がKより大きくなるので不可
			}else{

				work[1][1] = last_number+1;
				int next_state = calc_state(last_number+1,work);
				ret = recursive(last_number+1,0,work[1][1]-work[0][1],next_state);
				work[1][1] = 0;
			}
		}
	}

	//上の行の右端更新
	if(FLG == true && top_index <= K-1 && (last_number+1-work[0][top_index] <= K)){

		work[0][1+top_index] = last_number+1;
		int next_state = calc_state(last_number+1,work);
		ret += recursive(last_number+1,last_number+1-left_under,left_vertical_diff,next_state);
		ret %= M;
	}

	return dp[last_number][left_under_diff][left_vertical_diff][top_state] = ret;
}

int main(){

	POW[0] = 1;
	for(int i = 1; i < SIZE; i++){

		POW[i] = POW[i-1]*2;
	}

	scanf("%d %d %lld",&N,&K,&M);

	for(int num = 0; num <= 2*N; num++){
		for(int diff = 0; diff <= K; diff++){
			for(int diff_2 = 0; diff_2 <= K; diff_2++){
				for(int state = 0; state < POW[K]; state++){

					dp[num][diff][diff_2][state] = -1;
				}
			}
		}
	}

	printf("%lld\n",recursive(0,0,0,0)%M);

	return 0;
}
accuracy: 0.3076923076923077
time_ms: 70
memory_kb: 153212
final_rank: 14

code_snippet: #include<bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 99999999999999999
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;


#define SIZE 11

int POW[SIZE];
int N,K,M;
ll dp[205][SIZE][SIZE][1024]; //dp[最後に処理した数][最後に処理した数-左下の数][左下の数-その上の数][上の行の状態] = 場合の数

int calc_state(int base_number,int work[2][SIZE]){

	int base_col = 0;
	if(work[1][1] != 0){

		base_col = 1;
	}

	int ret = 0;

	for(int col = base_col+1; col <= K; col++){
		if(work[0][col] == 0)break;

		ret += POW[base_number-work[0][col]];
	}

	return ret;
}

ll recursive(int last_number,int left_under_diff,int left_vertical_diff,int top_state){

	if(dp[last_number][left_under_diff][left_vertical_diff][top_state] != -1){

		return dp[last_number][left_under_diff][left_vertical_diff][top_state];
	}

	int work[2][SIZE];

	for(int row = 0; row < 2; row++){
		for(int col = 0; col < SIZE; col++){
			work[row][col] = 0;
		}
	}

	//左下の数
	int left_under = last_number-left_under_diff;
	work[1][0] = left_under;
	//その上の数
	work[0][0] = left_under-left_vertical_diff;

	//状態の復元
	int top_index = 0;
	for(int diff = (K-1); diff >= 0; diff--){

		if(top_state & (1 << (diff))){

			work[0][1+top_index++] = last_number-diff;
		}
	}

	if(last_number == 2*N){

		if(left_under == 2*N && top_index == 0){

			return 1;
		}else{

			return 0;
		}
	}

	ll ret = 0;

	bool FLG = true;

	//左下更新
	if(top_index == 0){ //上段の列数==下段の列数

		if(work[0][0] != 0 && last_number+1-work[0][0] > K){

			FLG = false;
		}

	}else{
		if(left_under == 0){

			work[1][1] = last_number+1;
			int next_state = calc_state(last_number+1,work);
			ret = recursive(last_number+1,0,work[1][1]-work[0][1],next_state);
			work[1][1] = 0;

		}else{

			int tmp = min(work[1][0],work[0][1]);
			if((last_number+1)-tmp > K){

				FLG = false; //差がKより大きくなるので不可
			}else{

				work[1][1] = last_number+1;
				int next_state = calc_state(last_number+1,work);
				ret = recursive(last_number+1,0,work[1][1]-work[0][1],next_state);
				work[1][1] = 0;
			}
		}
	}

	//上の行の右端更新
	if(FLG == true && top_index <= K-1 && (last_number+1-work[0][top_index] <= K)){

		work[0][1+top_index] = last_number+1;
		int next_state = calc_state(last_number+1,work);
		ret += recursive(last_number+1,last_number+1-left_under,left_vertical_diff,next_state);
		ret %= M;
	}

	return dp[last_number][left_under_diff][left_vertical_diff][top_state] = ret;
}

int main(){

	POW[0] = 1;
	for(int i = 1; i < SIZE; i++){

		POW[i] = POW[i-1]*2;
	}

	scanf("%d %d %d",&N,&K,&M);

	for(int num = 0; num <= 2*N; num++){
		for(int diff = 0; diff <= K; diff++){
			for(int diff_2 = 0; diff_2 <= K; diff_2++){
				for(int state = 0; state < POW[K]; state++){

					dp[num][diff][diff_2][state] = -1;
				}
			}
		}
	}

	printf("%lld\n",recursive(0,0,0,0)%M);

	return 0;
}
accuracy: 0.3076923076923077
time_ms: 70
memory_kb: 153212
final_rank: 14

code_snippet: #include<bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 99999999999999999
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;


#define SIZE 11

int POW[SIZE];
int N,K,M;
ll dp[205][SIZE][SIZE][1024]; //dp[最後に処理した数][最後に処理した数-左下の数][左下の数-その上の数][上の行の状態] = 場合の数

int calc_state(int base_number,int work[2][SIZE]){

	int base_col = 0;
	if(work[1][1] != 0){

		base_col = 1;
	}

	int ret = 0;

	for(int col = base_col+1; col <= K; col++){
		if(work[0][col] == 0)break;

		ret += POW[base_number-work[0][col]];
	}

	return ret;
}

ll recursive(int last_number,int left_under_diff,int left_vertical_diff,int top_state){

	if(dp[last_number][left_under_diff][left_vertical_diff][top_state] != -1){

		return dp[last_number][left_under_diff][left_vertical_diff][top_state];
	}

	int work[2][SIZE];

	for(int row = 0; row < 2; row++){
		for(int col = 0; col < SIZE; col++){
			work[row][col] = 0;
		}
	}

	//左下の数
	int left_under = last_number-left_under_diff;
	work[1][0] = left_under;
	//その上の数
	work[0][0] = left_under-left_vertical_diff;

	//状態の復元
	int top_index = 0;
	for(int diff = (K-1); diff >= 0; diff--){

		if(top_state & (1 << (diff))){

			work[0][1+top_index++] = last_number-diff;
		}
	}

	if(last_number == 2*N){

		if(left_under == 2*N && top_index == 0){

			return 1;
		}else{

			return 0;
		}
	}

	ll ret = 0;

	bool FLG = true;

	//左下更新
	if(top_index == 0){ //上段の列数==下段の列数

		if(work[0][0] != 0 && last_number+1-work[0][0] > K){

			FLG = false;
		}

	}else{
		if(left_under == 0){

			if(last_number+1-work[0][1] <= K){

				work[1][1] = last_number+1;
				int next_state = calc_state(last_number+1,work);
				ret = recursive(last_number+1,0,work[1][1]-work[0][1],next_state);
				work[1][1] = 0;

			}

		}else{

			int tmp = min(work[1][0],work[0][1]);
			if((last_number+1)-tmp > K){

				FLG = false; //差がKより大きくなるので不可
			}else{

				work[1][1] = last_number+1;
				int next_state = calc_state(last_number+1,work);
				ret = recursive(last_number+1,0,work[1][1]-work[0][1],next_state);
				work[1][1] = 0;
			}
		}
	}

	if(FLG == true && top_index < K && (last_number+1-work[0][1+top_index-1] <= K)){

		work[0][1+top_index] = last_number+1;
		int next_state = calc_state(last_number+1,work);
		ret += recursive(last_number+1,last_number+1-left_under,left_vertical_diff,next_state);
		ret %= M;
	}

	return dp[last_number][left_under_diff][left_vertical_diff][top_state] = ret;
}

int main(){

	POW[0] = 1;
	for(int i = 1; i < SIZE; i++){

		POW[i] = POW[i-1]*2;
	}

	scanf("%d %d %d",&N,&K,&M);

	for(int num = 0; num <= 2*N; num++){
		for(int diff = 0; diff <= K; diff++){
			for(int diff_2 = 0; diff_2 <= K; diff_2++){
				for(int state = 0; state < POW[K]; state++){

					dp[num][diff][diff_2][state] = -1;
				}
			}
		}
	}

	printf("%lld\n",recursive(0,0,0,0));

	return 0;
}
accuracy: 0.3076923076923077
time_ms: 80
memory_kb: 153264
final_rank: 19

code_snippet: #include<bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 99999999999999999
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;


#define SIZE 11

int POW[SIZE];
int N,K,M;
ll dp[205][SIZE][SIZE][1024]; //dp[最後に処理した数][最後に処理した数-左下の数][左下の数-その上の数][上の行の状態] = 場合の数

int calc_state(int base_number,int work[2][SIZE]){

	int base_col = 0;
	if(work[1][1] != 0){

		base_col = 1;
	}

	int ret = 0;

	for(int col = base_col+1; col <= K; col++){
		if(work[0][col] == 0)break;

		ret += POW[base_number-work[0][col]];
	}

	return ret;
}

ll recursive(int last_number,int left_under_diff,int left_vertical_diff,int top_state){

	if(dp[last_number][left_under_diff][left_vertical_diff][top_state] != -1){

		return dp[last_number][left_under_diff][left_vertical_diff][top_state];
	}

	int work[2][SIZE];

	for(int row = 0; row < 2; row++){
		for(int col = 0; col < SIZE; col++){
			work[row][col] = 0;
		}
	}

	//左下の数
	int left_under = last_number-left_under_diff;
	work[1][0] = left_under;
	//その上の数
	work[0][0] = left_under-left_vertical_diff;

	//状態の復元
	int top_index = 0;
	for(int diff = (K-1); diff >= 0; diff--){

		if(top_state & (1 << (diff))){

			work[0][1+top_index++] = last_number-diff;
		}
	}

	if(last_number == 2*N){

		if(left_under == 2*N && top_index == 0){

			return 1;
		}else{

			return 0;
		}
	}

	ll ret = 0;

	bool FLG = true;

	//左下更新
	if(top_index == 0){ //上段の列数==下段の列数

		if(work[0][0] != 0 && last_number+1-work[0][0] > K){

			FLG = false;
		}

	}else{
		if(left_under == 0){

			work[1][1] = last_number+1;
			int next_state = calc_state(last_number+1,work);
			ret = recursive(last_number+1,0,work[1][1]-work[0][1],next_state);
			work[1][1] = 0;

		}else{

			int tmp = min(work[1][0],work[0][1]);
			if((last_number+1)-tmp > K){

				FLG = false; //差がKより大きくなるので不可
			}else{

				work[1][1] = last_number+1;
				int next_state = calc_state(last_number+1,work);
				ret = recursive(last_number+1,0,work[1][1]-work[0][1],next_state);
				work[1][1] = 0;
			}
		}
	}

	if(FLG == true && top_index < K && (last_number+1-work[0][1+top_index-1] <= K)){

		work[0][1+top_index] = last_number+1;
		int next_state = calc_state(last_number+1,work);
		ret += recursive(last_number+1,last_number+1-left_under,left_vertical_diff,next_state);
		ret %= M;
	}

	return dp[last_number][left_under_diff][left_vertical_diff][top_state] = ret;
}

int main(){

	POW[0] = 1;
	for(int i = 1; i < SIZE; i++){

		POW[i] = POW[i-1]*2;
	}

	scanf("%d %d %d",&N,&K,&M);

	for(int num = 0; num <= 2*N; num++){
		for(int diff = 0; diff <= K; diff++){
			for(int diff_2 = 0; diff_2 <= K; diff_2++){
				for(int state = 0; state < POW[K]; state++){

					dp[num][diff][diff_2][state] = -1;
				}
			}
		}
	}

	printf("%lld\n",recursive(0,0,0,0));

	return 0;
}
accuracy: 0.3076923076923077
time_ms: 80
memory_kb: 153196
final_rank: 17

code_snippet: #include<bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 99999999999999999
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;


#define SIZE 11

int POW[SIZE];
int N,K,M;
ll dp[205][SIZE][SIZE][1024]; //dp[最後に処理した数][最後に処理した数-左下の数][左下の数-その上の数][上の行の状態] = 場合の数

int calc_state(int base_number,int work[2][SIZE]){

	int base_col = 0;
	if(work[1][1] != 0){

		base_col = 1;
	}

	int ret = 0;

	for(int col = base_col+1; col <= K; col++){
		if(work[0][col] == 0)break;

		ret += POW[base_number-work[0][col]];
	}

	return ret;
}

ll recursive(int last_number,int left_under_diff,int left_vertical_diff,int top_state){

	if(dp[last_number][left_under_diff][left_vertical_diff][top_state] != -1){

		return dp[last_number][left_under_diff][left_vertical_diff][top_state];
	}

	int work[2][SIZE];

	for(int row = 0; row < 2; row++){
		for(int col = 0; col < SIZE; col++){
			work[row][col] = 0;
		}
	}

	//左下の数
	int left_under = last_number-left_under_diff;
	work[1][0] = left_under;
	//その上の数
	work[0][0] = left_under-left_vertical_diff;

	//状態の復元
	int top_index = 0;
	for(int diff = (K-1); diff >= 0; diff--){

		if(top_state & (1 << (diff))){

			work[0][1+top_index++] = last_number-diff;
		}
	}

	if(last_number == 2*N){

		if(left_under == 2*N && top_index == 0){

			return 1;
		}else{

			return 0;
		}
	}

	ll ret = 0;

	bool FLG = true;

	//左下更新
	if(top_index == 0){ //上段の列数==下段の列数

		if(work[0][0] != 0 && last_number+1-work[0][0] > K){

			FLG = false;
		}

	}else{
		if(left_under == 0){

			work[1][1] = last_number+1;
			int next_state = calc_state(last_number+1,work);
			ret = recursive(last_number+1,0,work[1][1]-work[0][1],next_state);
			work[1][1] = 0;

		}else{

			int tmp = min(work[1][0],work[0][1]);
			if((last_number+1)-tmp > K){

				FLG = false; //差がKより大きくなるので不可
			}else{

				work[1][1] = last_number+1;
				int next_state = calc_state(last_number+1,work);
				ret = recursive(last_number+1,0,work[1][1]-work[0][1],next_state);
				work[1][1] = 0;
			}
		}
	}

	if(FLG == true && top_index < K){

		work[0][1+top_index] = last_number+1;
		int next_state = calc_state(last_number+1,work);
		ret += recursive(last_number+1,last_number+1-left_under,left_vertical_diff,next_state);
		ret %= M;
	}

	return dp[last_number][left_under_diff][left_vertical_diff][top_state] = ret;
}

int main(){

	POW[0] = 1;
	for(int i = 1; i < SIZE; i++){

		POW[i] = POW[i-1]*2;
	}

	scanf("%d %d %d",&N,&K,&M);

	for(int num = 0; num <= 2*N; num++){
		for(int diff = 0; diff <= K; diff++){
			for(int diff_2 = 0; diff_2 <= K; diff_2++){
				for(int state = 0; state < POW[K]; state++){

					dp[num][diff][diff_2][state] = -1;
				}
			}
		}
	}

	printf("%lld\n",recursive(0,0,0,0));

	return 0;
}
accuracy: 0.3076923076923077
time_ms: 70
memory_kb: 153260
final_rank: 16

code_snippet: #include <bits/stdc++.h>
#pragma GCC diagnostic ignored "-Wsign-compare"
#pragma GCC diagnostic ignored "-Wsign-conversion"
#define NDEBUG
#define SHOW(...) static_cast<void>(0)
//!===========================================================!//
//!  dP     dP                          dP                    !//
//!  88     88                          88                    !//
//!  88aaaaa88a .d8888b. .d8888b. .d888b88 .d8888b. 88d888b.  !//
//!  88     88  88ooood8 88'  '88 88'  '88 88ooood8 88'  '88  !//
//!  88     88  88.  ... 88.  .88 88.  .88 88.  ... 88        !//
//!  dP     dP  '88888P' '88888P8 '88888P8 '88888P' dP        !//
//!===========================================================!//
template <typename T>
T read()
{
    T v;
    return std::cin >> v, v;
}
template <typename T>
std::vector<T> readVec(const std::size_t l)
{
    std::vector<T> v(l);
    for (auto& e : v) { std::cin >> e; }
    return v;
}
using ld = long double;
using uint = unsigned int;
using ll = long long;
using ull = unsigned long long;
constexpr unsigned int MOD = 1000000007;
template <typename T>
constexpr T INF = std::numeric_limits<T>::max() / 4;
template <typename F>
constexpr F PI = static_cast<F>(3.1415926535897932385);
std::mt19937 mt{std::random_device{}()};
template <typename T>
bool chmin(T& a, const T& b) { return (a > b ? a = b, true : false); }
template <typename T>
bool chmax(T& a, const T& b) { return (a < b ? a = b, true : false); }
template <typename T>
std::vector<T> Vec(const std::size_t n, T v) { return std::vector<T>(n, v); }
template <class... Args>
auto Vec(const std::size_t n, Args... args) { return std::vector<decltype(Vec(args...))>(n, Vec(args...)); }
template <typename T>
constexpr T popCount(const T u)
{
#ifdef __has_builtin
    return u == 0 ? T(0) : (T)__builtin_popcountll(u);
#else
    unsigned long long v = static_cast<unsigned long long>(u);
    return v = (v & 0x5555555555555555ULL) + (v >> 1 & 0x5555555555555555ULL), v = (v & 0x3333333333333333ULL) + (v >> 2 & 0x3333333333333333ULL), v = (v + (v >> 4)) & 0x0F0F0F0F0F0F0F0FULL, static_cast<T>(v * 0x0101010101010101ULL >> 56 & 0x7f);
#endif
}
template <typename T>
constexpr T log2p1(const T u)
{
#ifdef __has_builtin
    return u == 0 ? T(0) : T(64 - __builtin_clzll(u));
#else
    unsigned long long v = static_cast<unsigned long long>(u);
    return v = static_cast<unsigned long long>(v), v |= (v >> 1), v |= (v >> 2), v |= (v >> 4), v |= (v >> 8), v |= (v >> 16), v |= (v >> 32), popCount(v);
#endif
}
template <typename T>
constexpr T clog(const T v) { return v == 0 ? T(0) : log2p1(v - 1); }
template <typename T>
constexpr T msbp1(const T v) { return log2p1(v); }
template <typename T>
constexpr T lsbp1(const T v)
{
#ifdef __has_builtin
    return __builtin_ffsll(v);
#else
    return v == 0 ? T(0) : popCount((v & (-v)) - T(1)) + T(1);
#endif
}
template <typename T>
constexpr bool ispow2(const T v) { return popCount(v) == 1; }
template <typename T>
constexpr T ceil2(const T v) { return v == 0 ? T(1) : T(1) << log2p1(v - 1); }
template <typename T>
constexpr T floor2(const T v) { return v == 0 ? T(0) : T(1) << (log2p1(v) - 1); }
//!===============================================================!//
//!   88888888b            dP       .88888.   a88888b. 888888ba   !//
//!   88                   88      d8'   '88 d8'   '88 88    '8b  !//
//!  a88aaaa    dP.  .dP d8888P    88        88        88     88  !//
//!   88         '8bd8'    88      88   YP88 88        88     88  !//
//!   88         .d88b.    88      Y8.   .88 Y8.   .88 88    .8P  !//
//!   88888888P dP'  'dP   dP       '88888'   Y88888P' 8888888P   !//
//!===============================================================!//
template <typename T>
constexpr std::pair<T, T> extgcd(const T a, const T b)
{
    if (b == 0) { return std::pair<T, T>{1, 0}; }
    const auto p = extgcd(b, a % b);
    return {p.second, p.first - p.second * (a / b)};
}
template <typename T>
constexpr T inverse(const T a, const T mod) { return (mod + extgcd((mod + a % mod) % mod, mod).first % mod) % mod; }
//!===============================================================!//
//!  888888ba  8888ba.88ba                 dP dP            dP    !//
//!  88    '8b 88  '8b  '8b                88 88            88    !//
//!  88     88 88   88   88 .d8888b. .d888b88 88 88d888b. d8888P  !//
//!  88     88 88   88   88 88'  '88 88'  '88 88 88'  '88   88    !//
//!  88    .8P 88   88   88 88.  .88 88.  .88 88 88    88   88    !//
//!  8888888P  dP   dP   dP '88888P' '88888P8 dP dP    dP   dP    !//
//!===============================================================!//
class DModInt
{
private:
    uint v;
    static uint norm(const uint x, const uint mod) { return x < mod ? x : x - mod; }
    static DModInt make(const uint x, const uint mod)
    {
        DModInt m(mod);
        return m.v = x, m;
    }
    static DModInt power(DModInt x, ll n)
    {
        assert(n >= 0);
        DModInt ans{1, x.mod};
        for (; n; n >>= 1, x *= x) {
            if (n & 1) { ans *= x; }
        }
        return ans;
    }
    static DModInt inv(const DModInt& x) { return DModInt{inverse(static_cast<ll>(x.v), static_cast<ll>(x.mod)), x.mod}; }

public:
    DModInt(const uint mod) : v{0}, mod{mod} {}
    DModInt(const ll val, const uint mod) : v{norm(static_cast<uint>(val % static_cast<ll>(mod) + static_cast<ll>(mod)), mod)}, mod{mod} {}
    DModInt(const DModInt& n) : v{n()}, mod{n.mod} {}
    explicit operator bool() const { return v != 0; }
    DModInt& operator=(const DModInt& n) { return assert(mod == n.mod), v = n(), (*this); }
    DModInt& operator=(const ll val) { return v = norm(static_cast<uint>(val % static_cast<ll>(mod) + static_cast<ll>(mod)), mod), (*this); }
    friend DModInt operator+(const DModInt& m) { return m; }
    friend DModInt operator-(const DModInt& m) { return make(norm(m.mod - m.v, m.mod), m.mod); }
    friend DModInt operator+(const DModInt& m1, const DModInt& m2) { return assert(m1.mod == m2.mod), make(norm(m1.v + m2.v, m1.mod), m1.mod); }
    friend DModInt operator-(const DModInt& m1, const DModInt& m2) { return assert(m1.mod == m2.mod), make(norm(m1.mod + m1.v - m2.v, m1.mod), m1.mod); }
    friend DModInt operator*(const DModInt& m1, const DModInt& m2) { return assert(m1.mod == m2.mod), make(static_cast<uint>(static_cast<ll>(m1.v) * static_cast<ll>(m2.v) % static_cast<ll>(m1.mod)), m1.mod); }
    friend DModInt operator/(const DModInt& m1, const DModInt& m2) { return assert(m1.mod == m2.mod), m1* inv(m2); }
    friend DModInt operator+(const DModInt& m, const ll val) { return DModInt{m.v + val, m.mod}; }
    friend DModInt operator-(const DModInt& m, const ll val) { return DModInt{m.v - val, m.mod}; }
    friend DModInt operator*(const DModInt& m, const ll val) { return DModInt{static_cast<ll>(m.v) * (val % m.mod), m.mod}; }
    friend DModInt operator/(const DModInt& m, const ll val) { return m * inv(DModInt{val, m.mod}); }
    friend DModInt operator+(const ll val, const DModInt& m) { return m + val; }
    friend DModInt operator-(const ll val, const DModInt& m) { return DModInt{val - static_cast<ll>(m.v), m.mod}; }
    friend DModInt operator*(const ll val, const DModInt& m) { return m * val; }
    friend DModInt operator/(const ll val, const DModInt& m) { return DModInt(val, m.mod) / m; }
    friend DModInt& operator+=(DModInt& m1, const DModInt& m2) { return assert(m1.mod == m2.mod), m1 = m1 + m2; }
    friend DModInt& operator-=(DModInt& m1, const DModInt& m2) { return assert(m1.mod == m2.mod), m1 = m1 - m2; }
    friend DModInt& operator*=(DModInt& m1, const DModInt& m2) { return assert(m1.mod == m2.mod), m1 = m1* m2; }
    friend DModInt& operator/=(DModInt& m1, const DModInt& m2) { return assert(m1.mod == m2.mod), m1 = m1 / m2; }
    friend DModInt& operator+=(DModInt& m, const ll val) { return m = m + val; }
    friend DModInt& operator-=(DModInt& m, const ll val) { return m = m - val; }
    friend DModInt& operator*=(DModInt& m, const ll val) { return m = m * val; }
    friend DModInt& operator/=(DModInt& m, const ll val) { return m = m / val; }
    friend DModInt operator^(const DModInt& m, const ll n) { return power(m, n); }
    friend DModInt& operator^=(DModInt& m, const ll n) { return m = m ^ n; }
    friend bool operator==(const DModInt& m1, const DModInt& m2) { return assert(m1.mod == m2.mod), m1.v == m2.v; }
    friend bool operator!=(const DModInt& m1, const DModInt& m2) { return assert(m1.mod == m2.mod), not(m1 == m2); }
    friend bool operator==(const ll val, const DModInt& m) { return m == val; }
    friend bool operator!=(const ll val, const DModInt& m) { return not(val == m); }
    friend bool operator==(const DModInt& m, const ll val) { return m.v == norm(static_cast<uint>(static_cast<ll>(m.mod) + val % static_cast<ll>(m.mod)), m.mod); }
    friend bool operator!=(const DModInt& m, const ll val) { return not(m == val); }
    friend std::istream& operator>>(std::istream& is, DModInt& m)
    {
        uint v;
        return is >> v, m = v, is;
    }
    friend std::ostream& operator<<(std::ostream& os, const DModInt& m) { return os << m.v; }
    static std::vector<DModInt> invVec(const uint mod, const std::size_t N)
    {
        std::vector<DModInt> ans(N + 1, 1);
        for (std::size_t i = 2; i <= N; i++) { ans[i] = -ans[mod % i] * (mod / i); }
        return ans;
    }
    uint operator()() const { return v; }
    const uint mod;
};
//!========================================================!//
//!  8888ba.88ba                 dP    dP            dP    !//
//!  88  '8b  '8b                88    88            88    !//
//!  88   88   88 .d8888b. .d888b88    88 88d888b. d8888P  !//
//!  88   88   88 88'  '88 88'  '88    88 88'  '88   88    !//
//!  88   88   88 88.  .88 88.  .88    88 88    88   88    !//
//!  dP   dP   dP '88888P' '88888P8    dP dP    dP   dP    !//
//!========================================================!//
template <uint mod>
class ModInt
{
private:
    uint v;
    static uint norm(const uint& x) { return x < mod ? x : x - mod; }
    static ModInt make(const uint& x)
    {
        ModInt m;
        return m.v = x, m;
    }
    static ModInt power(ModInt x, ll n)
    {
        ModInt ans = 1;
        for (; n; n >>= 1, x *= x) {
            if (n & 1) { ans *= x; }
        }
        return ans;
    }
    static ModInt inv(const ModInt& x) { return ModInt{inverse(static_cast<ll>(x.v), static_cast<ll>(mod))}; }

public:
    ModInt() : v{0} {}
    ModInt(const ll val) : v{norm(uint(val % static_cast<ll>(mod) + static_cast<ll>(mod)))} {}
    ModInt(const ModInt& n) : v{n()} {}
    explicit operator bool() const { return v != 0; }
    bool operator!() const { return not static_cast<bool>(*this); }
    ModInt& operator=(const ModInt& m) { return v = m(), (*this); }
    ModInt& operator=(const ll val) { return v = norm(uint(val % static_cast<ll>(mod) + static_cast<ll>(mod))), (*this); }
    friend ModInt operator+(const ModInt& m) { return m; }
    friend ModInt operator-(const ModInt& m) { return make(norm(mod - m.v)); }
    friend ModInt operator+(const ModInt& m1, const ModInt& m2) { return make(norm(m1.v + m2.v)); }
    friend ModInt operator-(const ModInt& m1, const ModInt& m2) { return make(norm(m1.v + mod - m2.v)); }
    friend ModInt operator*(const ModInt& m1, const ModInt& m2) { return make(static_cast<uint>(static_cast<ll>(m1.v) * static_cast<ll>(m2.v) % static_cast<ll>(mod))); }
    friend ModInt operator/(const ModInt& m1, const ModInt& m2) { return m1 * inv(m2.v); }
    friend ModInt operator+(const ModInt& m, const ll val) { return ModInt{static_cast<ll>(m.v) + val}; }
    friend ModInt operator-(const ModInt& m, const ll val) { return ModInt{static_cast<ll>(m.v) - val}; }
    friend ModInt operator*(const ModInt& m, const ll val) { return ModInt{static_cast<ll>(m.v) * (val % static_cast<ll>(mod))}; }
    friend ModInt operator/(const ModInt& m, const ll val) { return ModInt{static_cast<ll>(m.v) * inv(val)}; }
    friend ModInt operator+(const ll val, const ModInt& m) { return ModInt{static_cast<ll>(m.v) + val}; }
    friend ModInt operator-(const ll val, const ModInt& m) { return ModInt{-static_cast<ll>(m.v) + val}; }
    friend ModInt operator*(const ll val, const ModInt& m) { return ModInt{static_cast<ll>(m.v) * (val % static_cast<ll>(mod))}; }
    friend ModInt operator/(const ll val, const ModInt& m) { return ModInt{val * inv(static_cast<ll>(m.v))}; }
    friend ModInt& operator+=(ModInt& m1, const ModInt& m2) { return m1 = m1 + m2; }
    friend ModInt& operator-=(ModInt& m1, const ModInt& m2) { return m1 = m1 - m2; }
    friend ModInt& operator*=(ModInt& m1, const ModInt& m2) { return m1 = m1 * m2; }
    friend ModInt& operator/=(ModInt& m1, const ModInt& m2) { return m1 = m1 / m2; }
    friend ModInt& operator+=(ModInt& m, const ll val) { return m = m + val; }
    friend ModInt& operator-=(ModInt& m, const ll val) { return m = m - val; }
    friend ModInt& operator*=(ModInt& m, const ll val) { return m = m * val; }
    friend ModInt& operator/=(ModInt& m, const ll val) { return m = m / val; }
    friend ModInt operator^(const ModInt& m, const ll n) { return power(m.v, n); }
    friend ModInt& operator^=(ModInt& m, const ll n) { return m = m ^ n; }
    friend bool operator==(const ModInt& m1, const ModInt& m2) { return m1.v == m2.v; }
    friend bool operator!=(const ModInt& m1, const ModInt& m2) { return not(m1 == m2); }
    friend bool operator==(const ModInt& m, const ll val) { return m.v == norm(static_cast<uint>(static_cast<ll>(mod) + val % static_cast<ll>(mod))); }
    friend bool operator!=(const ModInt& m, const ll val) { return not(m == val); }
    friend bool operator==(const ll val, const ModInt& m) { return m.v == norm(static_cast<uint>(static_cast<ll>(mod) + val % static_cast<ll>(mod))); }
    friend bool operator!=(const ll val, const ModInt& m) { return not(m == val); }
    friend std::istream& operator>>(std::istream& is, ModInt& m)
    {
        uint v;
        return is >> v, m = v, is;
    }
    friend std::ostream& operator<<(std::ostream& os, const ModInt& m) { return os << m(); }
    static std::vector<ModInt> invVec(const std::size_t N)
    {
        std::vector<ModInt> ans(N + 1, 1);
        for (std::size_t i = 2; i <= N; i++) { ans[i] = -ans[mod % i] * (mod / i); }
        return ans;
    }
    uint operator()() const { return v; }
};
//!=====================================!//
//!  8888ba.88ba           oo           !//
//!  88  '8b  '8b                       !//
//!  88   88   88 .d8888b. dP 88d888b.  !//
//!  88   88   88 88'  '88 88 88'  '88  !//
//!  88   88   88 88.  .88 88 88    88  !//
//!  dP   dP   dP '88888P8 dP dP    dP  !//
//!=====================================!//
int main()
{
    using mint = DModInt;
    const int N = read<int>(), K = read<int>(), M = read<uint>();
    std::uniform_int_distribution<int> dist{2, MOD - 2};
    std::vector<ModInt<MOD>> r1(2 * N + 3, 0), r2(2 * N + 3, 0);
    for (int i = 0; i <= 2 * N + 2; i++) { r1[i] = dist(mt), r2[i] = dist(mt); }
    using P = std::pair<int, int>;
    using T = std::pair<int, P>;
    std::map<T, int> memo;
    auto dp = [&, N, K, M](auto&& self, const int n, const int l, const int u, const std::deque<int>& us) -> mint {
        if (n == 2 * N + 2) { return u == N + 1 ? mint(1, M) : mint(0, M); }
        ModInt<MOD> hash1 = 1, hash2 = 1;
        for (const auto& d : us) { hash1 *= r1[d], hash2 *= r2[d]; }
        if (memo.find(T{l, {hash1(), hash2()}}) != memo.end()) { return mint(memo[T{l, {hash1(), hash2()}}], M); }
        mint ans(0, M);
        if (n % 2 == 0 and u == n / 2) {
            if (n > us.back() + K) {
                ans = mint(0, M);
            } else {
                auto v = us;
                v.pop_front(), v.push_back(n), ans = self(self, n + 1, l, u + 1, v);
            }
        } else {
            const int lm = std::min(us.front(), l) + K;
            if (n <= lm) {
                auto v = us;
                if (v.size() > 1) { v.pop_front(); }
                ans += self(self, n + 1, n, u, v);
            }
            if (u < N + 1 and n <= us.back() + K and n + 1 - l <= K) {
                auto v = us;
                v.push_back(n), ans += self(self, n + 1, l, u + 1, v);
            }
        }
        return memo[T{l, {hash1(), hash2()}}] = ans(), ans;
    };
    std::cout << dp(dp, 3, INF<int>, 2, {2}) << std::endl;
    return 0;
}
accuracy: 1
time_ms: 210
memory_kb: 32128
final_rank: 5