Algo-Trian
This commit is contained in:
39
Algorithm/Graph/DFS/P1036 [NOIP 2002 普及组] 选数.cpp
Normal file
39
Algorithm/Graph/DFS/P1036 [NOIP 2002 普及组] 选数.cpp
Normal file
@@ -0,0 +1,39 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
using namespace std;
|
||||
|
||||
int js = 0;
|
||||
vector<int> arr;
|
||||
|
||||
bool checkprime(int x) {
|
||||
if (x < 2) return false;
|
||||
for (int i = 2; i * i <= x; i++) {
|
||||
if (x % i == 0) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void dfs(int idx, int k, int sum) {
|
||||
if (k == 0) { // <20>Ѿ<EFBFBD>ѡ<EFBFBD><D1A1>k<EFBFBD><6B>
|
||||
if (checkprime(sum)) js++;
|
||||
return;
|
||||
}
|
||||
if (idx >= arr.size()) return;
|
||||
|
||||
// ѡ<><D1A1><EFBFBD><EFBFBD>ǰ<EFBFBD><C7B0>
|
||||
dfs(idx + 1, k - 1, sum + arr[idx]);
|
||||
// <20><>ѡ<EFBFBD><D1A1>ǰ<EFBFBD><C7B0>
|
||||
dfs(idx + 1, k, sum);
|
||||
}
|
||||
|
||||
int main() {
|
||||
int n, k;
|
||||
cin >> n >> k;
|
||||
arr.resize(n);
|
||||
for (int i = 0; i < n; i++) cin >> arr[i];
|
||||
|
||||
dfs(0, k, 0);
|
||||
cout << js << endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user