【Data structure example】Select the number

//https://www.luogu.com.cn/problem/P1036
#include<cstdio>
#include<iostream>
using namespace std;
int n,m,k;
int cnt=0,ans=0; 
int b[100001];
int a[100001];
int c[100001];

bool isprime(){
    cnt = 0;
    for(int i = 1; i <= m; ++i) {
        cnt+=c[i];
    }
    if(cnt==1)return false;
    if(cnt==2)return true;
    for(int i=2;i*i<=cnt;i++)
        if(cnt%i==0) return false;
        
        return true;
}

void combination(int k){
    if(k==m+1){
        if (isprime()) ans++;
        return;
    }
    
    for(int i=b[k-1]+1;i<=n;i++)
    {
        b[k]=i;
        c[k]=a[b[k]]; 
        combination(k+1);
    }
}
int main(){
    cin>>n>>m;
    for(int i=1;i<=n;i++)scanf("%d",&a[i]);
    combination(1);
cout << ans;
    return 0;
}

 

 

*Thank you very much for a good friend of mine for the guidance and modification of this answer, thank you!

Guess you like

Origin blog.csdn.net/melon_sama/article/details/108426024
Recommended