Problem:
Bug
You are designing the world's most powerful computer system, "Nayuta". However, during the implementation of the prototype of this system, you find a bug that causes the system to stop when the instruction sequence meets certain conditions.
The system works by giving an instruction string of length $N$ as a program. When the $m$-th instruction in the instruction sequence is represented by the number $X_m$, it is found that the condition for the bug to occur is that there exists a pattern of instructions in the instruction sequence such that $X_i + X_{j-1} = X_j + X_{i-1}$ for some integer $i,j$ ($2 \leq i＜j \leq N$).
To find out how much of an effect this bug has, you decided to find out how many instruction sequences of a certain length can cause the bug.
Write a program to find out how many instruction sequences of length $N$ will cause a failure. It is assumed that instructions can be represented by integers between 1 and $K$. The answer should be the remainder divided by the given prime number $M$.
Input
The input is given in the following form.
$N$ $K$ $M$
In a line, the length of the instruction sequence $N$ ($3 \leq N \leq 100,000$), the number of instruction types $K$ ($1 \leq K \leq 10$), and the prime number $M$ ($100,000,007 \leq M \leq 1,000,000,007$) are given.
Output
Outputs the remainder obtained by dividing the number of faulty instruction sequences by $M$.
Sample Input and Output
Sample Input 1
3 2 100000007
Sample Output 1
2
If we represent the instruction sequence as $(X_1,X_2,X_3)$, there are eight possible instruction sequences: $(1,1,1)$, $(1,1,2)$, $(1,2,1)$, $(1,2,2)$, $(2,1,1)$, $(2,1,2)$, $(2,1,2)$, $(2,2,1)$, and $(2,2,2)$. Of these, the instruction sequence in which the failure occurs is $(1,1,1)$ and $(2,2,2)$.
Sample Input 2
9 10 100000037
Sample Output 2
66631256
There are 866631552 instruction sequences that cause the bug, and the output is the remainder of the number divided by the prime number 100000037.

Submitted solutions:

code_snippet: #include <iostream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <deque>

#include <iomanip>
using namespace std;

using ll = long long;

void showbit(int bit, int T) {
	for (int i = 0;i < T;i++) if (bit & (1 << i)) cout << "IS " << i << endl;
}

int main() {
	int N, K;ll M;cin >> N >> K >> M;
	if (2*K < N) {
		ll res = 1;
		for (int i = 0;i < N;i++) res = (res * (ll)K) % M;
		cout << res << endl;
		return 0;
	}
	int T = (2*K) - 1;
	vector<vector<int>> REM(1<<T);
	for (int t = 0;t < (1<<T);t++) for (int i = 0;i < T;i++) if (!(t & (1 << i))) REM[t].push_back(i);
	vector<vector<ll>> dp(1<<T, vector<ll>(K, 0));
	for (int i = 0;i < K;i++) dp[0][i] = 1;
	for (int bit = 0;bit < (1<<T);bit++) for (int n = 0;n < K;n++) {
		for (int d : REM[bit]) {
			int dd = d - (K-1);
			int nx = n + dd;
			if (nx < 0 or nx >= K) continue;
			int nbit = bit | (1 << d);
			dp[nbit][nx] = (dp[bit][n] + dp[nbit][nx]) % M;
		}
	}
	ll res = 0;
	for (int bit = 0;bit < (1<<T);bit++) {
		if ((__builtin_popcount(bit)) != N-1) continue;
		for (int i = 0;i < K;i++) {
			if (dp[bit][i] == 0) continue;
		
			//cout << dp[bit][i] << endl;
			res = (res + dp[bit][i]) % M;
		}
	}
	ll als = 1;
	for (int i = 0;i < N;i++) als = (als * K) % M;
	cout << (als - res + M) % M << endl;
}
accuracy: 1
time_ms: 160
memory_kb: 112636
final_rank: 11

code_snippet: #include <bits/stdc++.h>
//#include"atcoder/all"
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
//using namespace atcoder;
#define all(a) a.begin(),a.end()
#define int long long
typedef long long ll;
typedef vector<ll> vi;
typedef pair<int,int> P;
typedef pair<P,P> PP;
const int inf=1ll<<55;
const ll mod=1000000007;

int dp[10][530000];
int popcnt[530000];
signed main(){
	int n,k,m;cin>>n>>k>>m;
	rep(i,k)dp[i][0]=1;
	rep(i,1<<18){
		popcnt[i*2]=popcnt[i];
		popcnt[i*2+1]=popcnt[i]+1;
	}
	rep(j,1<<(2*k-1)){
		rep(i,k){
			if(!dp[i][j])continue;
			rep(l,2*k-1){
				if(j&(1<<l))continue;
				int d=i+l-k+1;
				if(0<=d&&d<k){
					dp[d][j^(1<<l)]+=dp[i][j];
					dp[d][j^(1<<l)]%=m;
				}
			}
		}
	}
	int ans=1;
	rep(i,n){
		ans*=k;ans%=m;
	}
	ans=m-ans;ans%=m;
	rep(j,1<<19){
		if(popcnt[j]==n-1){
			rep(i,k)ans+=dp[i][j];
		}
		ans%=m;
	}
	ans=m-ans;ans%=m;
	cout<<ans<<endl;
}
accuracy: 1
time_ms: 130
memory_kb: 48940
final_rank: 4

code_snippet: #include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(n);++i)
using namespace std;
using ll = long long;


ll pow_mod(ll base, ll exp, ll mod) {
    ll res = 1;
    while (exp > 0) {
        if (exp & 1) res = res * base % mod;
        base = base * base % mod;
        exp >>= 1;
    }
    return res;
}



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

    ll n,k,m;
    cin >> n >> k >> m;

    // if(n>2*k){
    //     cout << pow_mod(k,n,m) << endl;
    //     return 0;
    // }

    vector dp(k,vector<ll>(1<<(2*k),0));
    rep(i,k) dp[i][0]=1;

    rep(bit, 1<<(2*k)){
        rep(j,k){
            rep(l,k){
                int b = bit;
                if(b & (1<<(l-j+k))) continue;
                b |= (1<<(l-j+k));
                dp[j][b] += dp[l][bit];
                dp[j][b] %= m;
            }
        }
    }

    ll ans = 0;
    rep(i,k){
        rep(j,1<<(2*k)){
            int c = 0;
            rep(l,2*k) if(j & (1<<l)) c++;
            if(c!=n-1) continue;

            ans += dp[i][j];
            ans%=m;
        }
    }

    

    cout << (pow_mod(k,n,m)-ans+m)%m << endl;
}
accuracy: 1
time_ms: 720
memory_kb: 93228
final_rank: 12

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

#define ll long long

using namespace std;

const int N = 20, offset = 9;
int n, k, m;

int dp[(1 << N)][10];

ll fast_power(ll a,ll b){
    if (b==0)
        return 1;
    ll ret = fast_power(a,b>>1);
    (ret*=ret)%=m;
    if (b&1)
        ret*= a;
    ret%=m;
    return ret;
}

int slv(int i, int mask, int prv, bool fail) {
    if (fail)
        return fast_power(k, n-i );
    if (i == n)
        return 0;
    int &ret = dp[mask][prv];
    if (~ret)
        return ret;
    ret = 0;
    for (int j = 1; j <= k; ++j) {
        int d = j - (prv+1) + offset;
        (ret += slv(i+1, mask|(1<<d), j-1, fail|mask &(1<<d)) )%=m ;
    }
    return ret;
}

void solve() {
    cin >> n >> k >> m;
    if (n > 20) {
        ll ans = 1;
        for (int i = 0; i < n; ++i) {
            (ans *= k) %= m;
        }
        cout << ans << endl;
        return;
    }
    memset(dp, -1,sizeof dp);
    ll ans = 0;
    for (int i = 1; i <= k ; ++i) {
        (ans += slv(1,0,i-1,0))%=m;
    }
    cout<<ans<<endl;
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t = 1;
//    cin>>t ;
    for (int i = 1; i <= t; ++i) {
        solve();
    }

    return 0;
}
accuracy: 1
time_ms: 520
memory_kb: 44412
final_rank: 8

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

#define ll long long

using namespace std;

const int N = 20, offset = 9;
int n, k, m;

int dp[(1 << N)][10][2];

int slv(int i, int mask, int prv, bool fail) {
    if (i == n)
        return fail;
    int &ret = dp[mask][prv][fail];
    if (~ret)
        return ret;
    ret = 0;
    for (int j = 1; j <= k; ++j) {
        int d = j - (prv+1) + offset;
        (ret += slv(i+1, mask|(1<<d), j-1, fail|mask &(1<<d)) )%=m ;
    }
    return ret;
}

void solve() {
    cin >> n >> k >> m;
    if (n > 20) {
        ll ans = 1;
        for (int i = 0; i < n; ++i) {
            (ans *= k) %= m;
        }
        cout << ans << endl;
        return;
    }
    memset(dp, -1,sizeof dp);
    ll ans = 0;
    for (int i = 1; i <= k ; ++i) {
        (ans += slv(1,0,i-1,0))%=m;
    }
    cout<<ans<<endl;
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t = 1;
//    cin>>t ;
    for (int i = 1; i <= t; ++i) {
        solve();
    }

    return 0;
}
accuracy: 0.05
time_ms: 40
memory_kb: 85364
final_rank: 20

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

#define ll long long

using namespace std;

const int N = 20, offset = 9, M = 1e5+5;
ll n, k, m;

//int dp[N][(1 << N)][10];
unordered_map<int,int>mp[10][N];

ll dp[N][M];
int fast_power(ll a,ll b){

    if (b==0)
        return 1;
    ll ret = dp[a][b] ;
    if (~ret)
        return ret;
    ret = fast_power(a,b>>1);
    (ret*=ret)%=m;
    if (b&1)
        ret*= a;
    ret%=m;
    return ret;
}

int slv(int i, int mask, int prv, bool fail) {
    if (fail)
        return fast_power(k, n-i);
    if (i == n)
        return 0;
    if (mp[prv][i].count(mask))
        return mp[prv][i][mask];

    int ret = 0;
    for (int j = 1; j <= k; ++j) {
        int d = j - (prv+1) + offset;
        (ret += slv(i+1, mask|(1<<d), j-1, fail|mask &(1<<d)) )%=m ;
    }
    return mp[prv][i][mask] = ret;
}

void solve() {
    cin >> n >> k >> m;
    memset(dp, -1,sizeof dp);

    if (n > 20) {
        ll ans = fast_power(k, n);
        cout << ans << endl;
        return;
    }
    ll ans = 0;
    for (int i = 1; i <= k ; ++i) {
        (ans += slv(1,0,i-1,0))%=m;
    }
    cout<<ans<<endl;
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t = 1;
//    cin>>t ;
    for (int i = 1; i <= t; ++i) {
        solve();
    }

    return 0;
}
accuracy: 1
time_ms: 1580
memory_kb: 88756
final_rank: 15

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

#define ll long long

using namespace std;

const int N = 19, offset = 9;
int n, k, m;

//int dp[N][(1 << N)][10];
unordered_map<int,int>mp[10][N];

ll dp[N][N];
int fast_power(ll a,ll b){

    if (b==0)
        return 1;
    ll ret = dp[a][b] ;
    if (~ret)
        return ret;
    ret = fast_power(a,b>>1);
    (ret*=ret)%=m;
    if (b&1)
        ret*= a;
    ret%=m;
    return ret;
}

int slv(int i, int mask, int prv, bool fail) {
    if (fail)
        return fast_power(k, n-i);
    if (i == n)
        return 0;
    if (mp[prv][i].count(mask))
        return mp[prv][i][mask];

    int ret = 0;
    for (int j = 1; j <= k; ++j) {
        int d = j - (prv+1) + offset;
        (ret += slv(i+1, mask|(1<<d), j-1, fail|mask &(1<<d)) )%=m ;
    }
    return mp[prv][i][mask] = ret;
}

void solve() {
    cin >> n >> k >> m;
    if (n >= 20) {
        ll ans = 1;
        for (int i = 0; i < n; ++i) {
            (ans *= k) %= m;
        }
        cout << ans << endl;
        return;
    }
    memset(dp, -1,sizeof dp);
    ll ans = 0;
    for (int i = 1; i <= k ; ++i) {
        (ans += slv(1,0,i-1,0))%=m;
    }
    cout<<ans<<endl;
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t = 1;
//    cin>>t ;
    for (int i = 1; i <= t; ++i) {
        solve();
    }

    return 0;
}
accuracy: 0.95
time_ms: 1420
memory_kb: 73236
final_rank: 16

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

long long pow(long long base, long long exp, long long mod) {
    long long res = 1;
    while (exp > 0) {
        if (exp & 1) {
            res *= base;
            res %= mod;
        }
        base *= base;
        base %= mod;
        exp >>= 1;
    }
    return res;
}

int main() {
    int n, k;
    long long m;
    std::cin >> n >> k >> m;
    long long ans = pow(k, n, m);
    if (2 * k < n) {
        std::cout << ans << '\n';
        return 0;
    }
    const int offset = k - 1;
    const int elem_n = 2 * k - 1;
    std::vector dp(k, std::vector<long long>(1 << elem_n));
    std::vector ndp(k, std::vector<long long>(1 << elem_n));
    for (int i = 0; i < k; i++) {
        dp[i][0] = 1;
    }
    for (int i = 0; i + 1 < n; i++) {
        for (int a = 0; a < k; a++) for (int b = 0; b < (1 << elem_n); b++) ndp[a][b] = 0;
        for (int prefix_sum = 0; prefix_sum < k; prefix_sum++) {
            for (int state = 0; state < (1 << elem_n); state++) {
                if (dp[prefix_sum][state] == 0) continue;
                for (int elem = 0; elem < elem_n; elem++) {
                    if (state & (1 << elem)) continue;
                    int sum = prefix_sum + (elem - offset);
                    if (sum < 0 || sum >= k) continue;
                    ndp[sum][state | (1 << elem)] += dp[prefix_sum][state];
                    if (ndp[sum][state | (1 << elem)] >= m) {
                        ndp[sum][state | (1 << elem)] -= m;
                    }
                }
            }
        }
        dp.swap(ndp);
    }
    for (int i = 0; i < k; i++) {
        for (int j = 0; j < (1 << elem_n); j++) {
            ans -= dp[i][j];
            if (ans < 0) ans += m;
        }
    }
    std::cout << ans << '\n';
}
accuracy: 1
time_ms: 200
memory_kb: 89160
final_rank: 9

code_snippet: #include<bits/stdc++.h>
#define overload(a,b,c,d,...) d
#define rep1(a) for(ll _=0;_<(ll)a;++_)
#define rep2(i,n) for(ll i=0;i<(ll)n;++i)
#define rep3(i,l,r) for(ll i=(ll)l;i<(ll)r;++i)
#define rep(...) overload(__VA_ARGS__,rep3,rep2,rep1)(__VA_ARGS__)
#define rrep1(i,a) for(ll i=(ll)a-1;i>=0;--i)
#define rrep2(i,a,b) for(ll i=(ll)b-1;i>=(ll)a;--i)
#define rrep(...) overload(__VA_ARGS__,rrep2,rrep1)(__VA_ARGS__)
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define pb push_back
#define eb emplace_back
using namespace std;
using ll=long long;
using ull=unsigned long long;
using ld=long double;
template<typename T,typename U>inline bool chmin(T&a,const U&b){return (a>b?a=b,true:false);}
template<typename T,typename U>inline bool chmax(T&a,const U&b){return (a<b?a=b,true:false);}
static constexpr ll mod1=998244353;
static constexpr ll mod=1000000007;
ll modpow(ll a,ll b,ll m){
	ll ret=1;
	while(b){
		if(b&1)ret=ret*a%m;
		b>>=1;
		a=a*a%m;
	}
	return ret;
}
ll n,k,m;
void add(ll&a,ll b){
	a+=b;
	if(a>=m)a-=m;
}
#define getbit(a,b) (((a)>>(b))&1)
int main(){
	cin>>n>>k>>m;
	if(n>2*k){
		cout<<modpow(k,n,m)<<endl;
		return 0;
	}
	vector dp(k,vector(1<<(2*k-1),0LL));
	rep(i,k)dp[i][0]=1;
	rep(sz,n-1){
		//cerr<<sz<<endl;
		vector ndp(k,vector(1<<(2*k-1),0LL));
		rep(j,k){
			rep(bit,1<<(2*k-1))if(dp[j][bit]!=0){
				rep(l,k){
					if(getbit(bit,l-j+k-1))continue;
					add(ndp[l][bit|1<<(l-j+k-1)],dp[j][bit]);
				}
			}
		}
		dp=move(ndp);
	}
	ll ans=modpow(k,n,m);
	//cerr<<ans<<endl;
	rep(j,k)rep(l,1<<(2*k-1))ans-=dp[j][l];
	ans%=m;
	if(ans<0)ans+=m;
	cout<<ans<<endl;
}
accuracy: 1
time_ms: 250
memory_kb: 89160
final_rank: 10

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


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

    int n,k,m; cin>>n>>k>>m;

	long long all=1;
	for(int i=1;i<=n;i++){
		(all*=k)%=m;
	}

	int p=(k-1)*2+1;//p 差の

	if(n-1>p){
		cout<<all<<"\n";
		return 0;
	}


	vector<vector<int>> dp(1<<p,vector<int>(k+1,0));
	for(int i=1;i<=k;i++){
		dp[0][i]=1;
	}
	for(int i=0;i<n-1;i++){//ｎ番目からせんい
		for(int bit=0;bit<(1<<p);bit++){
			if(__builtin_popcount(bit)==i){
				for(int j=-k+1;j<=k-1;j++){//差
					for(int l=max(1,1-j);l<=min(k,k-j);l++){
						if(!(bit&(1<<(j+k-1)))){
							(dp[bit|(1<<(j+k-1))][l+j]+=dp[bit][l])%=m;
						}
					}
				}
			}
		}
	}

	int ans=0;
	for(int bit=0;bit<(1<<p);bit++){
		if(__builtin_popcount(bit)==n-1){
			for(int i=1;i<=k;i++){
				(ans+=dp[bit][i])%=m;
			}
		}
	}

	cout<<(all-ans+m)%m<<"\n";

    return 0;
}
accuracy: 1
time_ms: 190
memory_kb: 48020
final_rank: 5

code_snippet: /* -*- coding: utf-8 -*-
 *
 * 0422.cc: Bug
 */

#include<cstdio>
#include<algorithm>
 
using namespace std;

/* constant */

const int MAX_N = 100000;
const int MAX_K = 10;
const int MAX_L = MAX_K * 2 - 1;
const int LBITS = 1 << MAX_L;

/* typedef */

struct MI {
  static int MOD;
  int v;
  MI(): v() {}
  MI(int _v): v(_v % MOD) {}
  MI(long long _v): v(_v % MOD) {}

  MI operator+(const MI m) const { return MI(v + m.v); }
  MI operator-(const MI m) const { return MI(v + MOD - m.v); }
  MI operator*(const MI m) const { return MI((long long)v * m.v); }

  MI &operator+=(const MI m) { return (*this = *this + m); }
  MI &operator-=(const MI m) { return (*this = *this - m); }
  MI &operator*=(const MI m) { return (*this = *this * m); }

  MI pow(int n) const {  // a^n % MOD
    MI pm = 1, a = *this;
    while (n > 0) {
      if (n & 1) pm *= a;
      a *= a;
      n >>= 1;
    }
    return pm;
  }

  MI inv() const { return pow(MOD - 2); }
  MI operator/(const MI m) const { return *this * m.inv(); }
  MI &operator/=(const MI m) { return (*this = *this / m); }
};

int MI::MOD;

typedef MI mi;

/* global variables */

int bnums[LBITS];
mi dp[LBITS][MAX_K];

/* subroutines */

/* main */

int main() {
  int n, k, m;
  scanf("%d%d%d", &n, &k, &m);
  MI::MOD = m;

  int dl = k - 1, l = dl * 2 + 1;
  mi a = mi(k).pow(n);
  if (l < n - 1) { printf("%d\n", a.v); return 0; }

  int lbits = 1 << l;
  bnums[0] = 0;
  for (int bits = 1, msb = 1; bits < lbits; bits++) {
    if ((msb << 1) <= bits) msb <<= 1;
    bnums[bits] = bnums[bits ^ msb] + 1;
  }
  //printf("a=%d, l=%d, lbits=%d\n", a.v, l, lbits);

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

  for (int bits = 0; bits < lbits; bits++) {
    if (bnums[bits] >= n) continue;

    for (int i = 0; i < k; i++)
      if (dp[bits][i].v) {
	if (bnums[bits] == n - 1) sum += dp[bits][i];

	for (int j = 0; j < k; j++) {
	  int d = i - j + dl, bd = 1 << d;
	  if (! (bits & bd))
	    dp[bits | bd][j] += dp[bits][i];
	}
	
      }
  }

  printf("%d\n", (a - sum).v);
  return 0;
}
accuracy: 1
time_ms: 50
memory_kb: 25484
final_rank: 1

code_snippet: #include <iostream>
#include <vector>
#include <map>
#include <algorithm>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <iomanip>
#include <numeric>
using namespace std;
using ll = long long;
template<class T> inline bool chmax(T& a, T b){ if (a < b){ a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b){ if (a > b){ a = b; return true; } return false; }


int main() {
    ll N, K, mod;
    cin >> N >> K >> mod;
    
    ll powK[1010101] = {};
    powK[0] = 1;
    for(int i=1; i <= N; ++i) powK[i] = powK[i-1] * K % mod;

    int s = 1 - K, t = K - 1;
    map<pair<int, int>, ll> dp;
    for(int i=1; i<=K; ++i) dp[make_pair(i, 0)] = 1;

    ll ans = 0;
    for(int i=2; i<= N; ++i) {
        bool updated = false;
        map<pair<int, int>, ll> ndp;
        for(auto &mp: dp) {
            for(int x=1; x <= K; ++x) {
                int d = (x - mp.first.first) + t;
                if ((mp.first.second >> d) & 1) {
                    ans += mp.second * powK[N-i] % mod;
                    ans %= mod;
                    continue;
                }
                ndp[make_pair(x, mp.first.second|(1<<d))] += mp.second;
                ndp[make_pair(x, mp.first.second|(1<<d))] %= mod;
                updated = true;
            }
        }
        if (!updated) break;
        dp = ndp;
    }
    cout << ans << endl;
}
accuracy: 1
time_ms: 1750
memory_kb: 46276
final_rank: 13

code_snippet: #ifdef LOGX
#define _GLIBCXX_DEBUG
#endif
#include<bits/stdc++.h>
using namespace std;

using ll=long long;
#define rep(i,n) for(int i=0;i<int(n);i++)
#define rep2(i,s,n) for(int i=int(s);i<int(n);i++)
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend()
const int big=1e9+7;
//const int big=998244353;
const char newl='\n';
const int inf=1e9+10;
const ll INF=ll(1e18)+10;
const int dx[]={1,0,-1,0};
const int dy[]={0,1,0,-1};

using P=pair<ll,int>;
priority_queue<P,vector<P>,greater<P>> q;

ll exp(ll x,ll y,ll mod){
    ll res=1;
    while(y){
        if(y&1)(res*=x)%=mod;
        (x*=x)%=mod;
        y>>=1;
    }
    return res;
}

#define mybit(i,j) (((i)>>(j))&1)

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << fixed << setprecision(10);
    
    int n,m,k;
    cin >> n >> k >> m;
    if(n>=21){
        cout << exp(k,n,m) << newl;
        return 0;
    }
    else{
        vector<vector<vector<int>>> dp(2,vector<vector<int>>(k,vector<int>(1<<(2*k-1))));
        const int B=k-1;
        rep(i,k)dp[0][i][0]=1;
        rep(i,n-1){
            dp[(i+1)%2]=vector<vector<int>>(k,vector<int>(1<<(2*k-1)));
            rep(j,k)rep(S,1<<(2*k-1))if(dp[i%2][j][S])rep(nxt,k)if(!mybit(S,nxt-j+B)){
                dp[(i+1)%2][nxt][S|1<<(nxt-j+B)]+=dp[i%2][j][S];
                if(dp[(i+1)%2][nxt][S|1<<(nxt-j+B)]>=m)dp[(i+1)%2][nxt][S|1<<(nxt-j+B)]-=m;
            }
        }
        int ans=0;
        rep(i,k)rep(j,1<<(2*k-1))ans=(ans+dp[(n-1)%2][i][j])%m;
        ans=exp(k,n,m)-ans;
        ans%=m;
        ans+=m;
        ans%=m;
        cout << ans << newl;
    }
}
//命令の差：高々19通り
//鳩の巣原理をすれば、命令の差が20個ある i.e. 長さ>=21 の命令列は全てバグる
accuracy: 1
time_ms: 230
memory_kb: 66780
final_rank: 6

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
template<class T> using vc = vector<T>;
template<class T> using vvc = vc<vc<T>>;
template<class T> using vvvc = vc<vvc<T>>;
template<class T> using vvvvc = vvc<vvc<T>>;
template<class T> using PQ = priority_queue<T>;
template<class T> using invPQ = priority_queue<T, vector<T>, greater<T>>;
using IP = pair<int, int>;
using LP = pair<ll, ll>;
#define all(x) begin(x), end(x)
#define allr(x) rbegin(x), rend(x)
#define rep(i, n) for (int i = 0; (i) < (int)(n); i++)
#define rep3(i, m, n) for (int i = (m); (i) < (int)(n); i++)
#define repr(i, n) for (int i = (n) - 1; (i) >= 0; i--)
#define rep3r(i, m, n) for (int i = (n) - 1; (i) >= (int)(m); i--)
template<class T> inline bool chmax(T & a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T & a, T b) { if (a > b) { a = b; return 1; } return 0; }

signed main() {
    ios::sync_with_stdio(0);
    cin.tie(0);

    int N, K, M;
    cin >> N >> K >> M;

    if (K * 2 - 1 < N - 1) {
        ll ans = 1;
        rep (i, N) ans = (ans * K) % M;
        cout << ans << endl;
        return 0;
    }

    vvc<ll> dp(1 << (K*2-1), vc<ll>(K+1));
    rep (i, K) dp[0][i+1] = 1;
    for (int i = 0; i < 1 << (K*2-1); i++) {
        rep3 (j, 1, K+1) {
            rep3 (k, 1, K+1) {
                if ((1 << ((j - k) + K - 1)) & i) continue;
                dp[i|1 << ((j - k) + K - 1)][k] += dp[i][j];
                dp[i|1 << ((j - k) + K - 1)][k] %= M;
            }
        }
    }

    ll ans = 1;
    rep (i, N) ans = (ans * K) % M;
    for (int i = 0; i < 1 << (K*2-1); i++) {
        if (bitset<19>(i).count() != N-1) continue;
        rep3 (j, 1, K+1) {
            ans = (ans - dp[i][j] + M) % M;
        }
    }
    cout << ans << endl;
}
accuracy: 1
time_ms: 280
memory_kb: 64520
final_rank: 7

code_snippet: #include <bits/stdc++.h>
using namespace std;
int main(){
  int N, K, M;
  cin >> N >> K >> M;
  vector<vector<int>> dp(1 << (K * 2 - 1), vector<int>(K, 0));
  for (int i = 0; i < K; i++){
    dp[0][i] = 1;
  }
  for (int i = 0; i < (1 << (K * 2 - 1)); i++){
    for (int j = 0; j < K; j++){
      for (int k = 0; k < K; k++){
        int p = j - k + K - 1;
        if ((i >> p & 1) == 0){
          int i2 = i | (1 << p);
          dp[i2][k] += dp[i][j];
          if (dp[i2][k] >= M){
            dp[i2][k] -= M;
          }
        }
      }
    }
  }
  long long ans = 1;
  for (int i = 0; i < N; i++){
    ans *= K;
    ans %= M;
  }
  for (int i = 0; i < (1 << (K * 2 - 1)); i++){
    if (__builtin_popcount(i) == N - 1){
      for (int j = 0; j < K; j++){
        ans -= dp[i][j];
        if (ans < 0){
          ans += M;
        }
      }
    }
  }
  cout << ans << endl;
}
accuracy: 1
time_ms: 110
memory_kb: 39960
final_rank: 3

code_snippet: #include<iostream>
#include<stdio.h>
#include<vector>
#include<algorithm>
#include<set>
#include<string>
#include<map>
#include<string.h>
#include<complex>
#include<math.h>
#include<queue>
#include <functional>
#include<time.h>
#include<iomanip>
#include<cassert>
#include<bitset>
#include<sstream>
#include <unordered_set>
using namespace std;
typedef long long int llint;
typedef pair<int, int> pint;
typedef pair<llint, llint> pllint;
typedef vector<int> vint;
typedef vector<llint> vllint;
typedef vector<pint> vpint;
typedef vector<string> vstring;
typedef vector<pair<llint, llint>> vpllint;
typedef vector<vector<int>> vvint;
typedef vector<vector<llint>> vvllint;
typedef vector<vector<pint>> vvpint;
typedef vector<bool> vbool;
typedef vector<vbool> vvbool;
typedef vector<vpllint> vvpllint;
#define rep(i,n) for(int i=0;i<n;i++)
#define drep(i,n) for(int i=n-1;0<=i;i--)
#define yes(ans) if(ans)cout<<"yes"<<endl;else cout<<"no"<<endl;
#define Yes(ans) if(ans)cout<<"Yes"<<endl;else cout<<"No"<<endl;
#define YES(ans) if(ans)cout<<"YES"<<endl;else cout<<"NO"<<endl;
#define POSSIBLE(ans) if(ans)cout<<"POSSIBLE"<<endl;else cout<<"IMPOSSIBLE"<<endl;
#define Pi 3.1415926535897932384626
#define mod llint(1e9+7)
#define mod9 998244353
#define Inf 2147483647
#define llInf 9223372036854775807
#define all(x) x.begin(),x.end()
#define isin(i,n) (0<=i&&i<n)

class UnionFind {
public:
	//親の番号を格納する。親だった場合は-(その集合のサイズ)
	vector<int> Parent;

	//作るときはParentの値を全て-1にする
	//こうすると全てバラバラになる
	UnionFind(int N) {
		Parent = vector<int>(N, -1);
	}

	//Aがどのグループに属しているか調べる
	int root(int A) {
		if (Parent[A] < 0) return A;
		return Parent[A] = root(Parent[A]);
	}

	//自分のいるグループの頂点数を調べる
	int size(int A) {
		return -Parent[root(A)];//親をとってきたい
	}

	//AとBをくっ付ける
	bool connect(int A, int B) {
		//AとBを直接つなぐのではなく、root(A)にroot(B)をくっつける
		A = root(A);
		B = root(B);
		if (A == B) {
			//すでにくっついてるからくっ付けない
			return false;
		}

		//大きい方(A)に小さいほう(B)をくっ付けたい
		//大小が逆だったらひっくり返しちゃう。
		if (size(A) < size(B)) swap(A, B);

		//Aのサイズを更新する
		Parent[A] += Parent[B];
		//Bの親をAに変更する
		Parent[B] = A;

		return true;
	}
};

//セグ木・0-indexed・非再帰・(大きさ・単位元・関数)で初期化
template<typename T>
struct SegTree {
	//比較関数の型
	using F = function<T(T, T)>;
	//二分木を配列で表したもの
	vector<T>seg;
	//木の半分の大きさ
	int siz;
	//単位元
	const T unit;
	//比較する関数
	const F f;

	//大きさn、unit(単位元)、f(モノイド)でsegtreeを初期化する
	SegTree(int n, const T unit, const F f) : unit(unit), f(f) {
		siz = 1;
		while (siz < n)siz <<= 1;
		seg.assign(siz * 2 - 1, unit);
		siz--;
	}

	//k番目にtを入力
	void set(int k, const T& t) {
		seg[k + siz] = t;
	}

	//fによって木を構築する
	void build() {
		for (int i = siz - 1; 0 <= i; i--) {
			seg[i] = f(seg[i * 2 + 1], seg[i * 2 + 2]);
		}
	}

	T operator[](const int i) {
		return seg[i + siz];
	}

	//k番目をxに更新する
	void update(int k, T x) {
		k += siz;
		seg[k] = x;
		while (0 < k) {
			k = (k - 1) >> 1;
			seg[k] = f(seg[k * 2 + 1], seg[k * 2 + 2]);
		}
	}

	//[a,b)について、fした結果を返す
	//半開区域のためa以上b未満の位置を指す
	T query(int a, int b) {
		T l = unit, r = unit;
		for (a += siz, b += siz; a < b; a >>= 1, b >>= 1) {
			if (!(a & 1))l = f(l, seg[a++]);
			if (!(b & 1))r = f(seg[--b], r);
		}
		return f(l, r);
	}
};

//aとbの最大公約数を求めるよ
long long GCD(long long a, long long b) {
	if (b == 0) return a;
	else return GCD(b, a % b);
}

// 返り値: a と b の最大公約数
// ax + by = gcd(a, b) を満たす (x, y) が格納される
long long extGCD(long long a, long long b, long long& x, long long& y) {
	if (b == 0) {
		x = 1;
		y = 0;
		return a;
	}
	long long d = extGCD(b, a % b, y, x);
	y -= a / b * x;
	return d;
}

// mod. m での a の逆元 a^{-1} を計算する
long long modinv(long long a, long long m) {
	long long b = m, u = 1, v = 0;
	while (b) {
		long long t = a / b;
		a -= t * b; swap(a, b);
		u -= t * v; swap(u, v);
	}
	u %= m;
	if (u < 0) u += m;
	return u;
}

//nCrをmで割った余りを求める
llint nCr(llint n, llint r, llint m) {
	llint ans = 1;
	for (llint i = 0; i < r; i++) {
		ans *= n - i;
		ans %= m;
	}
	for (llint i = 1; i <= r; i++) {
		ans *= modinv(i, m);
		ans %= m;
	}
	return ans;
}

//aのb乗をmで割った余りを求める
llint power(llint a, llint b, llint m) {
	if (b == 1)return a;
	if (b == 0)return 1;
	llint tmp = power(a, (llint)b / 2, m);
	tmp *= tmp;
	tmp %= m;
	if (b % 2 == 1) {
		tmp *= a;
		tmp %= m;
	}
	return tmp;
}

//bitを表すsub,要素数を表すlength
bool next_combination(llint& sub, int length) {
	if (sub == 0)return false;
	llint x = sub & -sub, y = sub + x;
	sub = (((sub & ~y) / x) >> 1) | y;
	return sub < (llint)(1 << (llint)length);
}

void Zalgorithm(string& s, vint& a) {
	a[0] = s.size();
	int i = 1, j = 0;
	while (i < s.size()) {
		while (i + j < s.size() && s[j] == s[i + j])j++;
		a[i] = j;
		if (j == 0) { i++; continue; }
		int k = 1;
		while (i + k < s.size() && a[k] + k < j) {
			a[i + k] += a[k]; k++;
		}
		i += k; j -= k;
	}
	return;
}

int main() {
	int n, k, m;
	cin >> n >> k >> m;
	llint sum = 1;
	rep(i, n) {
		(sum *= (llint)k) %= m;
	}
	if (n > 2 * k) {
		cout << sum << endl;
	}
	else {
		//-k+1~k-1までの差で1~kまでの数字しか使えない長さNの配列の通り数
		//bitDPで解く

		//bitdp[i][j][k]…i番目がjで、kを使ったものの通り数
		vector bitdp(k, vint(1 << (2 * k - 1), 0));
		rep(i, n - 1) {
			if (i == 0) {
				rep(j, k) {
					bitdp[j][0] = 1;
				}
			}
			rep(j, k) {
				llint l = (1 << i) - 1;
				do {
					rep(nj, k) {
						if (l & (1 << (nj - j + k - 1)))continue;
						(bitdp[nj][l | (1 << (nj - j + k - 1))] += bitdp[j][l]) %= m;
					}
				} while (next_combination(l, 2 * k - 1));
			}
		}
		int ans = 0;
		rep(i, k) {
			llint l = (1 << (n - 1)) - 1;
			do {
				(ans += bitdp[i][l]) %= m;
			} while (next_combination(l, 2 * k - 1));
		}
		(ans = sum - ans) %= m;
		if (ans < 0)ans += m;
		cout << ans << endl;
	}
	return 0;
}
accuracy: 1
time_ms: 200
memory_kb: 25816
final_rank: 2

code_snippet: #include<iostream>
#include<stdio.h>
#include<vector>
#include<algorithm>
#include<set>
#include<string>
#include<map>
#include<string.h>
#include<complex>
#include<math.h>
#include<queue>
#include <functional>
#include<time.h>
#include<iomanip>
#include<cassert>
#include<bitset>
#include<sstream>
#include <unordered_set>
using namespace std;
typedef long long int llint;
typedef pair<int, int> pint;
typedef pair<llint, llint> pllint;
typedef vector<int> vint;
typedef vector<llint> vllint;
typedef vector<pint> vpint;
typedef vector<string> vstring;
typedef vector<pair<llint, llint>> vpllint;
typedef vector<vector<int>> vvint;
typedef vector<vector<llint>> vvllint;
typedef vector<vector<pint>> vvpint;
typedef vector<bool> vbool;
typedef vector<vbool> vvbool;
typedef vector<vpllint> vvpllint;
#define rep(i,n) for(int i=0;i<n;i++)
#define drep(i,n) for(int i=n-1;0<=i;i--)
#define yes(ans) if(ans)cout<<"yes"<<endl;else cout<<"no"<<endl;
#define Yes(ans) if(ans)cout<<"Yes"<<endl;else cout<<"No"<<endl;
#define YES(ans) if(ans)cout<<"YES"<<endl;else cout<<"NO"<<endl;
#define POSSIBLE(ans) if(ans)cout<<"POSSIBLE"<<endl;else cout<<"IMPOSSIBLE"<<endl;
#define Pi 3.1415926535897932384626
#define mod llint(1e9+7)
#define mod9 998244353
#define Inf 2147483647
#define llInf 9223372036854775807
#define all(x) x.begin(),x.end()
#define isin(i,n) (0<=i&&i<n)

class UnionFind {
public:
	//親の番号を格納する。親だった場合は-(その集合のサイズ)
	vector<int> Parent;

	//作るときはParentの値を全て-1にする
	//こうすると全てバラバラになる
	UnionFind(int N) {
		Parent = vector<int>(N, -1);
	}

	//Aがどのグループに属しているか調べる
	int root(int A) {
		if (Parent[A] < 0) return A;
		return Parent[A] = root(Parent[A]);
	}

	//自分のいるグループの頂点数を調べる
	int size(int A) {
		return -Parent[root(A)];//親をとってきたい
	}

	//AとBをくっ付ける
	bool connect(int A, int B) {
		//AとBを直接つなぐのではなく、root(A)にroot(B)をくっつける
		A = root(A);
		B = root(B);
		if (A == B) {
			//すでにくっついてるからくっ付けない
			return false;
		}

		//大きい方(A)に小さいほう(B)をくっ付けたい
		//大小が逆だったらひっくり返しちゃう。
		if (size(A) < size(B)) swap(A, B);

		//Aのサイズを更新する
		Parent[A] += Parent[B];
		//Bの親をAに変更する
		Parent[B] = A;

		return true;
	}
};

//セグ木・0-indexed・非再帰・(大きさ・単位元・関数)で初期化
template<typename T>
struct SegTree {
	//比較関数の型
	using F = function<T(T, T)>;
	//二分木を配列で表したもの
	vector<T>seg;
	//木の半分の大きさ
	int siz;
	//単位元
	const T unit;
	//比較する関数
	const F f;

	//大きさn、unit(単位元)、f(モノイド)でsegtreeを初期化する
	SegTree(int n, const T unit, const F f) : unit(unit), f(f) {
		siz = 1;
		while (siz < n)siz <<= 1;
		seg.assign(siz * 2 - 1, unit);
		siz--;
	}

	//k番目にtを入力
	void set(int k, const T& t) {
		seg[k + siz] = t;
	}

	//fによって木を構築する
	void build() {
		for (int i = siz - 1; 0 <= i; i--) {
			seg[i] = f(seg[i * 2 + 1], seg[i * 2 + 2]);
		}
	}

	T operator[](const int i) {
		return seg[i + siz];
	}

	//k番目をxに更新する
	void update(int k, T x) {
		k += siz;
		seg[k] = x;
		while (0 < k) {
			k = (k - 1) >> 1;
			seg[k] = f(seg[k * 2 + 1], seg[k * 2 + 2]);
		}
	}

	//[a,b)について、fした結果を返す
	//半開区域のためa以上b未満の位置を指す
	T query(int a, int b) {
		T l = unit, r = unit;
		for (a += siz, b += siz; a < b; a >>= 1, b >>= 1) {
			if (!(a & 1))l = f(l, seg[a++]);
			if (!(b & 1))r = f(seg[--b], r);
		}
		return f(l, r);
	}
};

//aとbの最大公約数を求めるよ
long long GCD(long long a, long long b) {
	if (b == 0) return a;
	else return GCD(b, a % b);
}

// 返り値: a と b の最大公約数
// ax + by = gcd(a, b) を満たす (x, y) が格納される
long long extGCD(long long a, long long b, long long& x, long long& y) {
	if (b == 0) {
		x = 1;
		y = 0;
		return a;
	}
	long long d = extGCD(b, a % b, y, x);
	y -= a / b * x;
	return d;
}

// mod. m での a の逆元 a^{-1} を計算する
long long modinv(long long a, long long m) {
	long long b = m, u = 1, v = 0;
	while (b) {
		long long t = a / b;
		a -= t * b; swap(a, b);
		u -= t * v; swap(u, v);
	}
	u %= m;
	if (u < 0) u += m;
	return u;
}

//nCrをmで割った余りを求める
llint nCr(llint n, llint r, llint m) {
	llint ans = 1;
	for (llint i = 0; i < r; i++) {
		ans *= n - i;
		ans %= m;
	}
	for (llint i = 1; i <= r; i++) {
		ans *= modinv(i, m);
		ans %= m;
	}
	return ans;
}

//aのb乗をmで割った余りを求める
llint power(llint a, llint b, llint m) {
	if (b == 1)return a;
	if (b == 0)return 1;
	llint tmp = power(a, (llint)b / 2, m);
	tmp *= tmp;
	tmp %= m;
	if (b % 2 == 1) {
		tmp *= a;
		tmp %= m;
	}
	return tmp;
}

//bitを表すsub,要素数を表すlength
bool next_combination(llint& sub, int length) {
	if (sub == 0)return false;
	llint x = sub & -sub, y = sub + x;
	sub = (((sub & ~y) / x) >> 1) | y;
	return sub < (llint)(1 << (llint)length);
}

void Zalgorithm(string& s, vint& a) {
	a[0] = s.size();
	int i = 1, j = 0;
	while (i < s.size()) {
		while (i + j < s.size() && s[j] == s[i + j])j++;
		a[i] = j;
		if (j == 0) { i++; continue; }
		int k = 1;
		while (i + k < s.size() && a[k] + k < j) {
			a[i + k] += a[k]; k++;
		}
		i += k; j -= k;
	}
	return;
}

int main() {
	int n, k, m;
	cin >> n >> k >> m;
	llint sum = 1;
	rep(i, n) {
		(sum *= (llint)k) %= m;
	}
	if (n >= 2 * k) {
		cout << sum << endl;
	}
	else {
		//-k+1~k-1までの差で1~kまでの数字しか使えない長さNの配列の通り数
		//bitDPで解く

		//bitdp[i][j][k]…i番目がjで、kを使ったものの通り数
		vector bitdp(k, vint(1 << (2 * k - 1), 0));
		rep(i, n - 1) {
			if (i == 0) {
				rep(j, k) {
					bitdp[j][0] = 1;
				}
			}
			rep(j, k) {
				llint l = (1 << i) - 1;
				do {
					rep(nj, k) {
						if (l & (1 << (nj - j + k - 1)))continue;
						(bitdp[nj][l | (1 << (nj - j + k - 1))] += bitdp[j][l]) %= m;
					}
				} while (next_combination(l, 2 * k - 1));
			}
		}
		int ans = 0;
		rep(i, k) {
			llint l = (1 << (n - 1)) - 1;
			do {
				(ans += bitdp[i][l]) %= m;
			} while (next_combination(l, 2 * k - 1));
		}
		(ans = sum - ans) %= m;
		if (ans < 0)ans += m;
		cout << ans << endl;
	}
	return 0;
}
accuracy: 0.9
time_ms: 200
memory_kb: 25632
final_rank: 18

code_snippet: #include<iostream>
#include<stdio.h>
#include<vector>
#include<algorithm>
#include<set>
#include<string>
#include<map>
#include<string.h>
#include<complex>
#include<math.h>
#include<queue>
#include <functional>
#include<time.h>
#include<iomanip>
#include<cassert>
#include<bitset>
#include<sstream>
#include <unordered_set>
using namespace std;
typedef long long int llint;
typedef pair<int, int> pint;
typedef pair<llint, llint> pllint;
typedef vector<int> vint;
typedef vector<llint> vllint;
typedef vector<pint> vpint;
typedef vector<string> vstring;
typedef vector<pair<llint, llint>> vpllint;
typedef vector<vector<int>> vvint;
typedef vector<vector<llint>> vvllint;
typedef vector<vector<pint>> vvpint;
typedef vector<bool> vbool;
typedef vector<vbool> vvbool;
typedef vector<vpllint> vvpllint;
#define rep(i,n) for(int i=0;i<n;i++)
#define drep(i,n) for(int i=n-1;0<=i;i--)
#define yes(ans) if(ans)cout<<"yes"<<endl;else cout<<"no"<<endl;
#define Yes(ans) if(ans)cout<<"Yes"<<endl;else cout<<"No"<<endl;
#define YES(ans) if(ans)cout<<"YES"<<endl;else cout<<"NO"<<endl;
#define POSSIBLE(ans) if(ans)cout<<"POSSIBLE"<<endl;else cout<<"IMPOSSIBLE"<<endl;
#define Pi 3.1415926535897932384626
#define mod llint(1e9+7)
#define mod9 998244353
#define Inf 2147483647
#define llInf 9223372036854775807
#define all(x) x.begin(),x.end()
#define isin(i,n) (0<=i&&i<n)

class UnionFind {
public:
	//親の番号を格納する。親だった場合は-(その集合のサイズ)
	vector<int> Parent;

	//作るときはParentの値を全て-1にする
	//こうすると全てバラバラになる
	UnionFind(int N) {
		Parent = vector<int>(N, -1);
	}

	//Aがどのグループに属しているか調べる
	int root(int A) {
		if (Parent[A] < 0) return A;
		return Parent[A] = root(Parent[A]);
	}

	//自分のいるグループの頂点数を調べる
	int size(int A) {
		return -Parent[root(A)];//親をとってきたい
	}

	//AとBをくっ付ける
	bool connect(int A, int B) {
		//AとBを直接つなぐのではなく、root(A)にroot(B)をくっつける
		A = root(A);
		B = root(B);
		if (A == B) {
			//すでにくっついてるからくっ付けない
			return false;
		}

		//大きい方(A)に小さいほう(B)をくっ付けたい
		//大小が逆だったらひっくり返しちゃう。
		if (size(A) < size(B)) swap(A, B);

		//Aのサイズを更新する
		Parent[A] += Parent[B];
		//Bの親をAに変更する
		Parent[B] = A;

		return true;
	}
};

//セグ木・0-indexed・非再帰・(大きさ・単位元・関数)で初期化
template<typename T>
struct SegTree {
	//比較関数の型
	using F = function<T(T, T)>;
	//二分木を配列で表したもの
	vector<T>seg;
	//木の半分の大きさ
	int siz;
	//単位元
	const T unit;
	//比較する関数
	const F f;

	//大きさn、unit(単位元)、f(モノイド)でsegtreeを初期化する
	SegTree(int n, const T unit, const F f) : unit(unit), f(f) {
		siz = 1;
		while (siz < n)siz <<= 1;
		seg.assign(siz * 2 - 1, unit);
		siz--;
	}

	//k番目にtを入力
	void set(int k, const T& t) {
		seg[k + siz] = t;
	}

	//fによって木を構築する
	void build() {
		for (int i = siz - 1; 0 <= i; i--) {
			seg[i] = f(seg[i * 2 + 1], seg[i * 2 + 2]);
		}
	}

	T operator[](const int i) {
		return seg[i + siz];
	}

	//k番目をxに更新する
	void update(int k, T x) {
		k += siz;
		seg[k] = x;
		while (0 < k) {
			k = (k - 1) >> 1;
			seg[k] = f(seg[k * 2 + 1], seg[k * 2 + 2]);
		}
	}

	//[a,b)について、fした結果を返す
	//半開区域のためa以上b未満の位置を指す
	T query(int a, int b) {
		T l = unit, r = unit;
		for (a += siz, b += siz; a < b; a >>= 1, b >>= 1) {
			if (!(a & 1))l = f(l, seg[a++]);
			if (!(b & 1))r = f(seg[--b], r);
		}
		return f(l, r);
	}
};

//aとbの最大公約数を求めるよ
long long GCD(long long a, long long b) {
	if (b == 0) return a;
	else return GCD(b, a % b);
}

// 返り値: a と b の最大公約数
// ax + by = gcd(a, b) を満たす (x, y) が格納される
long long extGCD(long long a, long long b, long long& x, long long& y) {
	if (b == 0) {
		x = 1;
		y = 0;
		return a;
	}
	long long d = extGCD(b, a % b, y, x);
	y -= a / b * x;
	return d;
}

// mod. m での a の逆元 a^{-1} を計算する
long long modinv(long long a, long long m) {
	long long b = m, u = 1, v = 0;
	while (b) {
		long long t = a / b;
		a -= t * b; swap(a, b);
		u -= t * v; swap(u, v);
	}
	u %= m;
	if (u < 0) u += m;
	return u;
}

//nCrをmで割った余りを求める
llint nCr(llint n, llint r, llint m) {
	llint ans = 1;
	for (llint i = 0; i < r; i++) {
		ans *= n - i;
		ans %= m;
	}
	for (llint i = 1; i <= r; i++) {
		ans *= modinv(i, m);
		ans %= m;
	}
	return ans;
}

//aのb乗をmで割った余りを求める
llint power(llint a, llint b, llint m) {
	if (b == 1)return a;
	if (b == 0)return 1;
	llint tmp = power(a, (llint)b / 2, m);
	tmp *= tmp;
	tmp %= m;
	if (b % 2 == 1) {
		tmp *= a;
		tmp %= m;
	}
	return tmp;
}

//bitを表すsub,要素数を表すlength
bool next_combination(llint& sub, int length) {
	if (sub == 0)return false;
	llint x = sub & -sub, y = sub + x;
	sub = (((sub & ~y) / x) >> 1) | y;
	return sub < (llint)(1 << (llint)length);
}

void Zalgorithm(string& s, vint& a) {
	a[0] = s.size();
	int i = 1, j = 0;
	while (i < s.size()) {
		while (i + j < s.size() && s[j] == s[i + j])j++;
		a[i] = j;
		if (j == 0) { i++; continue; }
		int k = 1;
		while (i + k < s.size() && a[k] + k < j) {
			a[i + k] += a[k]; k++;
		}
		i += k; j -= k;
	}
	return;
}

int main() {
	int n, k, m;
	cin >> n >> k >> m;
	llint sum = 1;
	rep(i, n) {
		(sum *= k) %= m;
	}
	if (n >= 2 * k) {
		cout << sum << endl;
	}
	else {
		//-k+1~k-1までの差で1~kまでの数字しか使えない長さNの配列の通り数
		//bitDPで解く

		//bitdp[i][j][k]…i番目がjで、kを使ったものの通り数
		vector bitdp(k, vint(1 << (2 * k - 1), 0));
		rep(i, n - 1) {
			if (i == 0) {
				rep(j, k) {
					bitdp[j][0] = 1;
				}
			}
			rep(j, k) {
				llint l = (1 << i) - 1;
				do {
					rep(nj, k) {
						if (l & (1 << (nj - j + k - 1)))continue;
						(bitdp[nj][l | (1 << (nj - j + k - 1))] += bitdp[j][l]) %= m;
					}
				} while (next_combination(l, 2 * k - 1));
			}
		}
		int ans = 0;
		rep(i, k) {
			llint l = (1 << (n - 1)) - 1;
			do {
				(ans += bitdp[i][l]) %= m;
			} while (next_combination(l, 2 * k - 1));
		}
		(ans = sum - ans) %= m;
		if (ans < 0)ans += m;
		cout << ans << endl;
	}
	return 0;
}
accuracy: 0.9
time_ms: 190
memory_kb: 25748
final_rank: 17

code_snippet: #include<iostream>
using namespace std;
int N,K,M;
int dp[21][10][1<<19];
main()
{
	cin>>N>>K>>M;
	int ans=0;
	if(K*2>=N)
	{
		for(int i=0;i<K;i++)dp[0][i][0]=1;
		for(int i=0;i<N-1;i++)
		{
			int comb=(1<<i)-1;
			while(comb<1<<K*2-1)
			{
				for(int j=0;j<K;j++)for(int l=0;l<K;l++)
				{
					int d=l-j+K-1;
					if(comb>>d&1)continue;
					(dp[i+1][l][comb|1<<d]+=dp[i][j][comb])%=M;
				}
				if(comb==0)break;
				int x=comb&-comb,y=comb+x;
				comb=((comb&~y)/x>>1)|y;
			}
		}
		for(int j=0;j<K;j++)for(int l=0;l<1<<K*2-1;l++)
		{
			(ans+=dp[N-1][j][l])%=M;
		}
	}
    long all=1;
	for(int i=0;i<N;i++)(all*=K)%=M;
	cout<<(all-ans+M)%M<<endl;
}
accuracy: 1
time_ms: 310
memory_kb: 224236
final_rank: 14

code_snippet: #include<iostream>
using namespace std;
int N,K,M;
int dp[21][10][1<<19];
main()
{
	cin>>N>>K>>M;
	int ans=0;
	if(K*2>=N)
	{
		for(int i=0;i<K;i++)dp[0][i][0]=1;
		for(int i=0;i<N-1;i++)
		{
			int comb=(1<<i)-1;
			while(comb<1<<K*2-1)
			{
				for(int j=0;j<K;j++)for(int l=0;l<K;l++)
				{
					int d=l-j+K-1;
					if(comb>>d&1)continue;
					(dp[i+1][l][comb|1<<d]+=dp[i][j][comb])%=M;
				}
				if(comb==0)break;
				int x=comb&-comb,y=comb+x;
				comb=((comb&~y)/x>>1)|y;
			}
		}
		for(int j=0;j<K;j++)for(int l=0;l<1<<K*2-1;l++)
		{
			(ans+=dp[N-1][j][l])%=M;
		}
	}
	int all=1;
	for(int i=0;i<N;i++)(all*=K)%=M;
	cout<<(all-ans+M)%M<<endl;
}
accuracy: 0.45
time_ms: 130
memory_kb: 111664
final_rank: 19