2021ACM Club Reserve Camp Individual Training Game 12th (Question B: Pick 1 dfs backtracking

Question B: Choose number 1
Time limit: 1 Sec Memory limit: 128 MB

Title description
Known n integers x1, x2,..., xn, and an integer k (k<n). Choose k integers from n integers and add them to get a series of sums respectively. For example, when n=4, k=3, and 4 integers are
3, 7, 12,
19 , all the combinations and their sums are: 3+7+12=22 3+7+19=29
7+12+19=38
3+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=29).
Input
Keyboard input, the format is:
n, k (1<=n<=20, k<n)
x1,x2,...,xn (1<=xi<=5000000)
output
Screen output, the format is:
an integer (satisfies The number of conditions).
Sample input Copy
4 3
3 7 12 19
Sample output Copy
1

Ideas,
search search search search enough to judge k, ah, ah, that’s it,
I’m super, really nothing to say

#include<bits/stdc++.h>
int cnt=0;
using namespace std;
typedef long long ll;
const int maxn=1e3+199;
int k,nn;
int sum=0;
int pan(ll n)
{
    
    
    for(int i=2;i<=sqrt(n);i++)
    {
    
    
        if(n%i==0)
            return 0;
    }
    return 1;
}

void dfs(int n,int m)
{
    
    
    if(m==k)
    {
    
    
        if(pan(bn))
            sum++;
            return;
    }
    if(n>nn)
        return;
    bn+=a[n];
    dfs(n+1,m+1);
    bn-=a[n];
    dfs(n+1,m);
}
int main(){
    
    

  cin>>nn>>k;
   for(int i=1;i<=nn;i++)
    cin>>a[i];
  dfs(1,0);
   cout<<sum;
	return 0;
}

Guess you like

Origin blog.csdn.net/qq_52172364/article/details/113091782