luogu P1036 [NOIP2002 Popularity Group] Selection

P1036
subject description
known integers nn x_1, x_2, ..., x_nx
. 1 , X 2 , ..., X n- , 11 integers and kk (k <nk <n) . Add kk integers randomly from nn integers to get a series of sums respectively. For example, when n=4, k=3n=4, k=3, and 44 integers are 3, 7, 12, 193, 7, 12, 19, all the combinations and their sum are:







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

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

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

3+12+19=343+12+19=34。

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

For example, in the above example, only one kind of sum is prime: 3+7+19=293+7+19=29.

Input format
Keyboard input, the format is:

n, kn, k (1 \ le n \ le 20, k <n1≤n≤20, k <n)

x_1, x_2, ..., x n (1 \ the x_i \ the 5000000) x
1 , x 2 , ..., x n (1≤x i ≤5000000)










Output format
Screen output, the format is: 11 integers (the number of species that meet the conditions).

Sample input and output
input #1 copy
4 3
3 7 12 19
output #1 copy
1
description/prompt
[question source]

NOIP 2002 Universal Group Question 2

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define INF 0x3f3f3f3f
int n, k, a[25] = {
    
    },ans=0;
bool isprime(int a) {
    
    
	if(a==0||a==1)return false;
	for (int i = 2; i <= sqrt(a); i++) {
    
    
		if (a % i == 0)return false;
	}
	return true;
}
void dfs(int m,int sum,int startx){
    
    
	if(m==k){
    
    
		if(isprime(sum))ans++;
		return;
	}
	for(int i=startx;i<n;i++){
    
    
		dfs(m+1,sum+a[i],i+1);
	
	}return;
} 
int main() {
    
    
	
	cin >> n >> k;
	for (int i = 0; i < n; i++)scanf("%d", &a[i]);
	dfs(0, 0, 0);
	printf("%d\n", ans);
	return 0;

}

Guess you like

Origin blog.csdn.net/Minelois/article/details/113855027