0%

[題解]質數環

a090. 質數環

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
#include <bits/stdc++.h>
#define ll long long
#define int long long
#define double long double
#define N 20
#define Orz ios::sync_with_stdio(0),cin.tie(0)
#define INF 2e18
#define rep(i,l,r) for(int i=l;i<=r;i++)
#define rrep(i,l,r) for(int i=l;i<r;i++)
#define pii pair<int,int>
#define x first
#define y second
using namespace std;
int n,prime[105],ans[N],cnt = 0;
bool visit[N];

void init(){
rep(i,0,100)prime[i] = 1;
prime[1] = 0;
for(int i=2;i<=100;i++){
if(prime[i]==0)continue;
for(int j=2*i;j<=100;j+=i){
prime[j] = 0;
}
}
}

void dfs(int id,int val){
ans[id] = val;
if(id>=n-1){
if(!prime[val+1])return;
for(int i=0;i<n;i++)cout<<ans[i]<<" ";
cout<<endl;
return;
}
for(int i=2;i<=n;i++){
if(visit[i])continue;
if(prime[val+i]==0)continue;
visit[i] = 1;
dfs(id+1,i);
visit[i] = 0;
}
}

signed main(){
Orz;
init();
while(cin>>n){
cnt+=1;
memset(ans,0,sizeof(ans));
memset(visit,0,sizeof(visit));
cout<<"Case "<<cnt<<":"<<endl;
dfs(0,1);
}
}