10分求调
查看原帖
10分求调
1179906
hhy8399楼主2024/5/17 23:47
#include<bits/stdc++.h>
using namespace std;

int n,k,a[1000001],used[1000001],cnt = 0,tag = -1,sum = 0,cc[10000001];
vector <int> c; 

bool pd(int x)
{
    for (int i = 2; i * i <= x; i++)
    {
        if (x % i == 0)return false;
    }
    return true;
}

void dfs(int w)
{

    if(w == k + 1)
	{
		c.push_back(0);
		tag ++;
		for(int i = 1;i <= k;i++)
		{
			c[tag] += cc[i];
		}	
        return;
    }

    for(int i = 1; i <= n; i++)
        if(used[i]==0)
		{ 
            used[i] = 1;
            cc[w] = a[i]; 
            dfs(w+1); 
            used[i] = 0;
        }
}

int main()
{
	cin >> n >> k;
	for (int i = 1;i <= n;i++) cin >> a[i];
	dfs(1);
	sort(c.begin(),c.end());
	for(int i = 0;i <= c.size() - 1;i++)
	{
		if(i > 0 && c[i] == c[i - 1])break;
		if(pd(c[i])) sum ++;
	}
	cout << sum << "\n";
	return 0;
}

10分求调

2024/5/17 23:47
加载中...