#include<bits/stdc++.h>
using namespace std;
//g++ -O3 -static 4B/4B.cpp -std=c++11 -o 4B/4b
stack<pair<int, int> > matches;
int n, m, p, res;
void explore(){
// cout << matches.top().first << " " << matches.top().second << endl;
if(matches.size() == n){
res ++;
res %= p;
return;
}
pair<int, int> last = matches.top();
for(int i = 0; i < m; i++){
if(i > last.second){
break;
}
for(int j = i; j < m; j++){
if(j < last.first){
continue;
}
matches.push({i, j});
explore();
matches.pop();
}
}
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m >> p;
for(int i = 0; i < m; i++){
for(int j = i; j < m; j++){
matches.push({i, j});
explore();
matches.pop();
}
}
cout << res;
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 | #include<bits/stdc++.h> using namespace std; //g++ -O3 -static 4B/4B.cpp -std=c++11 -o 4B/4b stack<pair<int, int> > matches; int n, m, p, res; void explore(){ // cout << matches.top().first << " " << matches.top().second << endl; if(matches.size() == n){ res ++; res %= p; return; } pair<int, int> last = matches.top(); for(int i = 0; i < m; i++){ if(i > last.second){ break; } for(int j = i; j < m; j++){ if(j < last.first){ continue; } matches.push({i, j}); explore(); matches.pop(); } } } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> m >> p; for(int i = 0; i < m; i++){ for(int j = i; j < m; j++){ matches.push({i, j}); explore(); matches.pop(); } } cout << res; return 0; } |
English