#include <bits/stdc++.h>
int n, k, p;
int sposoby;
int tab[1003];
std::vector<int>akt[2];
int main()
{
scanf("%d%d%d", &n, &k, &p);
for(int i=1; i<=n; i++)
tab[i]=i;
do{
akt[0].push_back(0);
for(int i=1; i<=n; i++)
akt[0].push_back(tab[i]);
akt[0].push_back(0);
int ile=0;
while(akt[0].size()!=3)
{
ile++;
akt[1].push_back(0);
for(int i=1; i<akt[0].size()-1; i++)
if(akt[0][i]>akt[0][i-1] && akt[0][i]>akt[0][i+1])
akt[1].push_back(akt[0][i]);
akt[1].push_back(0);
akt[0]=akt[1];
akt[1].clear();
if(ile>k)
break;
}
if(ile==k)
sposoby=(sposoby+1)%p;
}while(std::next_permutation(tab+1, tab+n+1));
printf("%d\n", sposoby);
}
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 | #include <bits/stdc++.h> int n, k, p; int sposoby; int tab[1003]; std::vector<int>akt[2]; int main() { scanf("%d%d%d", &n, &k, &p); for(int i=1; i<=n; i++) tab[i]=i; do{ akt[0].push_back(0); for(int i=1; i<=n; i++) akt[0].push_back(tab[i]); akt[0].push_back(0); int ile=0; while(akt[0].size()!=3) { ile++; akt[1].push_back(0); for(int i=1; i<akt[0].size()-1; i++) if(akt[0][i]>akt[0][i-1] && akt[0][i]>akt[0][i+1]) akt[1].push_back(akt[0][i]); akt[1].push_back(0); akt[0]=akt[1]; akt[1].clear(); if(ile>k) break; } if(ile==k) sposoby=(sposoby+1)%p; }while(std::next_permutation(tab+1, tab+n+1)); printf("%d\n", sposoby); } |
English