-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathP1036_DFS.cpp
53 lines (53 loc) · 946 Bytes
/
P1036_DFS.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
#include <iostream>
using namespace std;
int element[20];
int ans = 0;
int sum = 0;
int n, k;
bool flag[20];
bool Is_prim(int num)
{
if (num == 0 && num == 1)
return false;
if (num == 2)
return true;
for (int i = 2; i < num; i++)
if (num % i == 0)
return false;
return true;
}
void dfs(int cur, int start)
{
if (cur == k)
{
if (Is_prim(sum))
ans++;
return;
}
for (int i = start; i <= n; i++)
{
if (!flag[i])
{
flag[i] = true;
sum += element[i];
dfs(cur + 1, i + 1);
flag[i] = false;
sum -= element[i];
}
}
return;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> n >> k;
for (int i = 1; i <= n; i++)
{
cin >> element[i];
}
dfs(0, 1);
cout << ans;
cout << "\n" << sum;
return 0;
}