#include <iostream>
#include <algorithm>
#include <vector>
#include <stack>
#include <set>
using namespace std;
int n,k,p,w,tura,a=2,pot=1;
int t[1007];
vector <int> v;
stack <int> s;
set<pair<int,int> >S;
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cin>>n>>k>>p;
while (a<1025)
{
S.insert({a,pot});
a*=2;
pot++;
}
if (k>S.lower_bound({n,0})->second)
{
cout<<0;
return 0;
}
if (k==1)
{
w=1;
for (int i=1; i<n; i++)
{
w*=2;
w%=p;
}
cout<<w;
return 0;
}
for (int i=1; i<=n; i++)
{
t[i]=i;
}
do
{
v.resize(0);
for (int i=0; i<n; i++)
{
v.push_back(t[i]);
}
tura=0;
while (v.size()>1)
{
if (v[0]<v[1])
s.push(0);
for (int i=1; i<v.size()-1; i++)
{
if ((v[i]<v[i-1])||(v[i]<v[i+1]))
s.push(i);
}
if (v[v.size()-2]>v[v.size()-1])
s.push(v.size()-1);
while (!s.empty())
{
v.erase(v.begin()+s.top());
s.pop();
}
tura++;
}
if (tura==k)
w++;
}
while (next_permutation(t,t+n));
w%=p;
cout<<w;
return 0;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 | #include <iostream> #include <algorithm> #include <vector> #include <stack> #include <set> using namespace std; int n,k,p,w,tura,a=2,pot=1; int t[1007]; vector <int> v; stack <int> s; set<pair<int,int> >S; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin>>n>>k>>p; while (a<1025) { S.insert({a,pot}); a*=2; pot++; } if (k>S.lower_bound({n,0})->second) { cout<<0; return 0; } if (k==1) { w=1; for (int i=1; i<n; i++) { w*=2; w%=p; } cout<<w; return 0; } for (int i=1; i<=n; i++) { t[i]=i; } do { v.resize(0); for (int i=0; i<n; i++) { v.push_back(t[i]); } tura=0; while (v.size()>1) { if (v[0]<v[1]) s.push(0); for (int i=1; i<v.size()-1; i++) { if ((v[i]<v[i-1])||(v[i]<v[i+1])) s.push(i); } if (v[v.size()-2]>v[v.size()-1]) s.push(v.size()-1); while (!s.empty()) { v.erase(v.begin()+s.top()); s.pop(); } tura++; } if (tura==k) w++; } while (next_permutation(t,t+n)); w%=p; cout<<w; return 0; } |
English