@[58874__15157](/user/1113269) 递归,用迭代
by Betrayer_of_love @ 2024-04-18 17:37:02
判断质数时,n<2时不是质数,但你这样就是1
by Hhy140516 @ 2024-05-10 20:50:52
```cpp
#include <iostream>
#include <cstdio>
using namespace std;
bool isprime(int a){
for(int i = 2; i * i <= a; i++)
if(a % i == 0)
return false;
return true;
}
int n,k;
int a[25];
long long ans;
void dfs(int m, int sum, int startx){
if(m == k){
if(isprime(sum))
ans++;
return ;
}
for(int i = startx; i < n; i++)
dfs(m + 1, sum + a[i], i + 1);
return ;
}
int main(){
scanf("%d%d",&n,&k);
for(int i = 0; i < n; i++)
scanf("%d",&a[i]);
dfs(0, 0, 0);
printf("%d\n",ans);
return 0;
}
```
@[58874__15157](/user/1113269)
by MESSI_ @ 2024-05-11 19:44:13