16 pts
查看原帖
16 pts
916380
yyjw14楼主2023/10/12 18:29
#include<bits/stdc++.h>
using namespace std;
const int N = 25;
int a[N];
int i,j,k,m,n,cnt,sum;
bool vis[N];
bool isprime(int xxx)
{
	for(m=2;m*m<=xxx;m++)
	{
		if(xxx%m==0)
		{
			return false;
		}
	}
	return true;
}
void dfs(int dep)
{
	if(dep==j)
	{
		if(isprime(sum)==true)
		{
			cnt++;
		}
		return;
	}
	else
	{
		for(n=1;n<=i;n++)
		{
			if(vis[n]==0)
			{
				sum+=a[n];
				vis[n]=1;
				dfs(dep+1);
			
				if(dep==0)
				{
					continue;
				}
            	vis[n]=0;
			}
		}
		return;
	}
}
	
int main()
{
	ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
	cin>>i>>j;
	for(register int k=1;k<=i;k++)
	{
		cin>>a[k];
	}
	dfs(0); 
	cout<<cnt;
	return 0;
}
2023/10/12 18:29
加载中...