P1036 [NOIP2002 Popularization Group] Selection Number

[NOIP2002 Popularization Group] Selection Number

Question description

Wachi n n n 个整数 x 1 , x 2 , ⋯   , x n x_1,x_2,\cdots,x_n x1,x2,,xn, hereafter 1 1 1 integer k k k k < n k<n k<n).从 n n n Integer selection k k Adding k integers can produce a series of sums. For example, when n = 4 n=4 n=4 k = 3 k=3 k=3 4 4 4 Integer division 3 , 7 , 12 , 19 3,7,12,19 3,7,12,When 19, all combinations and their sum can be obtained:

3 + 7 + 12 = 22 3+7+12=22 3+7+12=22

3 + 7 + 19 = 29 3+7+19=29 3+7+19=29

7 + 12 + 19 = 38 7+12+19=38 7+12+19=38

3 + 12 + 19 = 34 3+12+19=34 3+12+19=34

Now, you are asked to calculate how many kinds of sums are prime numbers.

For example, in the above example, there is only one sum that is a prime number: 3 ​​+ 7 + 19 = 29 3+7+19=29 3+7+19=29

Input format

Two spaces separated integers in the first line n , k n,k n,k( 1 ≤ n ≤ 20 1 \and n \and 20 1n20 k < n k<n k<n)。

Second line n n n integers, respectively x 1 , x 2 , ⋯ , x n x_1,x_2,\cdots,x_n x1,x2,,xn 1 ≤ x i ≤ 5 × 1 0 6 1 \le x_i \le 5\times 10^6 1xi5×106)。

Output format

Output an integer representing the number of categories.

Example #1

Sample input #1

4 3
3 7 12 19

Sample output #1

1

hint

[Source of the question]

NOIP 2002 Popularization Group Question 2

solution

#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
int n,k,ans,a[30],b[30];
bool zs(int x)
{
    
    
	if (x==1)
		return 1;
	for (int i=2;i*i<=x;i++)
		if (x%i==0)
			return 0;
	return 1;
}
int main()
{
    
    
	scanf ("%d%d",&n,&k);
	for (int i=1;i<=n;i++)
		scanf ("%d",&a[i]);
	while (b[0]==0)
	{
    
    
		int kk,sum=0,shu=0;
		for (kk=n;b[kk];kk--);
		b[kk]=1;
		for (int j=kk+1;j<=n;j++)
			b[j]=0;
		for (int i=1;i<=n;i++)
			if (b[i])
			{
    
    
				sum++;
				shu+=a[i];
			}
		if (sum==k)
			if (zs(shu))
				ans++;
	}
	printf ("%d",ans);
	return 0;
}

Guess you like

Origin blog.csdn.net/DUXS11/article/details/134890980