补充条件:m(arry存储)个里面选取n个。同时设置布尔数组(m个的)
以后统一设置前n个数组为1,用prev_permutation
例子:洛谷1249
#include<bits/stdc++.h> using namespace std; bool isnprimes[100000000];int
primes[10000000];int cnt,sum;int count1; int k,n; void ola(int n) { for(int
i=2;i<=n;i++) { if(isnprimes[i]==0)primes[++cnt]=i; for(int
j=1;i*primes[j]<=n;j++) { isnprimes[i*primes[j]]=1; if(i%primes[j]==0)break; }
} } int main() { scanf("%d%d",&n,&k); int arry[n+1]={0};for(int
i=1;i<=n;i++)scanf("%d",&arry[i]);int book[n+1]={0}; for(int
i=1;i<=k;i++)book[i]=1; ola(100000000); do { count1=0; for(int i=1;i<=n;i++){
if(book[i]==1)count1+=arry[i]; } if(isnprimes[count1]==0)sum++; }
while(prev_permutation(book+1,book+1+n)); printf("%d",sum); return 0; }