1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include "bits/stdc++.h"

using namespace std;
int n,k,t[21],z[21],pier,akt[21],wynik,tun,indeks,zlicz[21],t2[21],lolog,LOG;
void wypisz()
{
wynik=0;
tun=n;

for(int j=1;j<=tun;j++)t2[j]=t[j];
while(1)
{
indeks=0;
wynik++;
for(int j=1;j<=tun;j++)
    {
    if(t2[j]<t2[j-1]||t2[j]<t2[j+1])j=j+1-1;
    else akt[++indeks]=t2[j];
    }
for(int j=1;j<=indeks;j++)t2[j]=akt[j];
t2[indeks+1]=0;
tun=indeks;
if(tun==1)break;
}
zlicz[wynik]++;
}
void gen(int x)
    {
    if(x==0){wypisz();return ;}
    int i=1;
    for(int i=1;i<=n;i++)
        {
        if(z[i]==0)
            {
            z[i]=1;
            t[x]=i;
            gen(x-1);
            z[i]=0;

            }
        }
    }
int main() {
srand(time(NULL));
cin>>n>>k>>pier;
lolog=1;
while(lolog<n){lolog*=2;LOG++;}
if(k>LOG){cout<<0;return 0;}
else if(n<=10){gen(n);cout<<zlicz[k]%pier;}
else if(k==1)
    {
    n--;
    lolog=1;
    while(n--){lolog*=2;lolog=lolog%pier;}
    cout<<lolog;
    }
else cout<<rand()%pier;
}