求大佬帮助67分1和4过不去
查看原帖
求大佬帮助67分1和4过不去
874765
852810242_121楼主2023/5/30 22:04
#include"iostream"
#include"cmath"
#include"algorithm"
using namespace std;
bool isprime(int x);
const int N = 100010;
int best[N];
int a[N];
bool path[N] = { false };
int n, m;
int cnt;
int dfs(int x, int last)
{
	if (x == m)
	{
		int sum = 0;
		for (int i = 0; i < m; i++)
		{
			sum += best[i];
		}
		if (isprime(sum))
		{
			cnt++;
		}
	}

	for (int i = 0; i < n; i++)
	{
		if (path[i] == false && a[i]>last)
		{
			best[x] = a[i];
			path[i] = true;
			dfs(x + 1,a[i]);
			path[i] = false;
		}
	}
	return 0;
}
bool isprime(int x)
{
	for (int i = 2; i <= x/2; i++)
	{
		if (x % i == 0)
			return false;
	}
	return true;
}

int main()
{
	cin >> n >> m;
	for (int i = 0; i < n; i++)
	{
		cin >> a[i];
	}
	sort(a, a + n);
	dfs(0,0);
	cout << cnt;
	return 0;
}
2023/5/30 22:04
加载中...