/*
递归:vector保存状态
*/
#include <iostream>
#include <vector>
using namespace std;
int n, m;
vector<int> res;
void dfs(int time, int pre){
if(n - pre < m - time) return ;
if(time == m){
for(int i = 0; i < m; i ++ ){
cout << res[i];
if(i != m - 1) cout << " ";
else cout << endl;
}
return ;
}
res.push_back(pre + 1);
dfs(time + 1, pre + 1);
res.pop_back();
dfs(time, pre + 1);
}
int main(){
cin >> n >> m;
dfs(0, 0);
return 0;
}
/*
递归:二进制状态表示
*/
#include <iostream>
using namespace std;
int n, m;
void dfs(int time, int pre, int state){
if(n - pre < m - time) return ;
if(time == m){
for(int i = 0; i < n; i ++ ){
if(state >> i & 1) cout << i + 1 << " ";
}
cout << endl;
return ;
}
dfs(time + 1, pre + 1, state | (1 << pre));
dfs(time, pre + 1, state);
}
int main(){
cin >> n >> m;
dfs(0, 0, 0);
return 0;
}
/*
非递归(栈模拟)
*/
#include <iostream>
#include <stack>
using namespace std;
struct node{
int pos;
int time;
int pre;
int state;
};
int main(){
int n, m; cin >> n >> m;
stack<node> stk;
stk.push({0, 0, 0, 0});
while(!stk.empty()){
node now = stk.top();
stk.pop();
if(now.pos == 0){
if(n - now.pre < m - now.time) continue;
if(now.time == m){
for(int i = 0; i < n; i ++ ){
if(now.state >> i & 1) cout << i + 1 << " ";
}
cout << endl;
continue;
}
now.pos = 1;
stk.push(now);
stk.push({0, now.time + 1, now.pre + 1, now.state | (1 << now.pre)});
}else if(now.pos == 1){
now.pos = 2;
stk.push(now);
stk.push({0, now.time, now.pre + 1, now.state});
}else continue;
}
return 0;
}