Luo Gu P1036. Selected tree

Luo Gu P1036. Selected tree

Title Description

Known n integers x1, x2, ..., x, and an integer k (k <n). Optionally adding integers k from n integers in a range of available respectively. For example, when n = 4, k = 3,4 are integers when 3,7,12,19, available with all combinations thereof and is:

3+7+12=22

3+7+19=29

7+12+19=38

3+12+19=34。

Now, we ask you to calculate the total number of species and is a prime number.

For example the embodiment, only one, and is a prime number: 3 + 7 + 19 = 29.

Input Format

Keyboard input format:

n,k(1 ≤ n ≤ 20,k < n)

x1,x2,…,xn(1≤xi≤5000000)

Output Format

Screen output format is: an integer (satisfy several criteria).

Sample input and output

Input Sample # 1
4 3
3 7 12 19
Sample Output # 1
1

Topic ideas

#include<bits/stdc++.h>
using namespace std;
const int N = 30;
int a[N], n, k;

bool zs(int num)    //求质数
{
    int n = sqrt(num);
    for (int i = 2; i <= n; i++)
    {
        if (!(num % i))
            return false;
    }
    return true;
}
//idx角标 sum和 num还需要多少个数
int fun(int idx, int sum, int num)
{
    int res = 0;
    sum += a[idx];
    num--;
    if (num)
    {
        int max = n-num+1;
        for (int i = idx + 1; i < max; i++)
        {
            res += fun(i, sum, num);
        }
    }
    else
    {
        if (zs(sum))
            return 1;
    }
    return res;
}

int main()
{
    scanf("%d%d", &n, &k);
    for (int i = 0; i < n; i++)scanf("%d", &a[i]);
    int res = 0;
    for (int i = 0; i <= n - k; i++)
        res += fun(i, 0, k);
    printf("%d", res);
    return 0;
}

Guess you like

Origin www.cnblogs.com/fsh001/p/12320997.html