-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCombination_Sum_2.cpp
More file actions
73 lines (73 loc) · 1000 Bytes
/
Combination_Sum_2.cpp
File metadata and controls
73 lines (73 loc) · 1000 Bytes
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
62
63
64
65
66
67
68
69
70
71
72
73
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
int n, k, ok = 1;
int x[15];
vector<vector<int>> v;
vector<int> prime;
bool nto(int n)
{
if(n < 2) return false;
else if(n == 2) return true;
else
{
for(int i = 2; i <= sqrt(n); i++)
{
if(n % i == 0)
{
return false;
}
}
return true;
}
}
void Try(int i, int pos, int sum)
{
for(int j = pos; j < prime.size(); j++)
{
if(sum <= n)
{
sum += prime[j];
x[i] = prime[j];
if(i == k)
{
if(sum == n)
{
ok = 0;
vector<int> tmp(x + 1, x + i + 1);
v.push_back(tmp);
}
}
else if(sum < n)
{
Try(i + 1, j + 1, sum);
}
sum -= prime[j];
}
}
}
int main()
{
cin >> n >> k;
for(int i = 2; i <= 500; i++)
{
if(nto(i)) prime.push_back(i);
}
Try(1, 0, 0);
for(auto it : v)
{
for(int i = 0; i < it.size(); i++)
{
cout << it[i];
if(i < it.size() - 1)
{
cout << " + ";
}
}
cout << endl;
}
if(ok)
{
cout << "NOT FOUND" << endl;
}
}