-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1118.cpp
61 lines (50 loc) · 964 Bytes
/
1118.cpp
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
#include<iostream>
#include<cstring>
using namespace std;
bool succ;
int n,k;
int ans[15];
bool vis[15];
int sjx[13][13];
void yh(){
for(int i=1;i<=n;i++)
for(int j=1;j<=i;j++){
sjx[i][j]=sjx[i-1][j]+sjx[i-1][j-1];if(j==1||j==i) sjx[i][j]=1;
}
for(int i=1;i<=n;i++){
for(int j=1;j<=i;j++) cout<<sjx[i][j]<<" ";
cout<<endl;
}
}
int count(){
int sum=0;
for(int i=1;i<=n;i++) sum+=ans[i]*sjx[n][i];
return sum;
}
void dfs(int now,int depth){
if(succ||count()>k) return;
cout<<count()<<endl; for(int i=1;i<=n;i++) cout<<ans[i]<<" ";cout<<endl;
if(depth>n){
if(depth>n&&count()==k){
for(int i=1;i<=n;i++) cout<<ans[i]<<" ";
succ=1;
}
return;
}
for(int i=1;i<=n;i++)
if(!vis[i]){
ans[depth+1]=i;
vis[i]=1;
dfs(i,depth+1);
vis[i]=0;
ans[depth+1]=0;
}
ans[now]=0;
}
int main(){
cin>>n>>k;
yh();
for(int i=1;i<=n;i++)
vis[i]=1,ans[1]=i,dfs(i,1),memset(vis,0,sizeof(vis)),memset(ans,0,sizeof(ans));
return 0;
}