1 solutions

  • 0
    @ 2025-11-5 16:43:12

    C++ :

    #include <iostream>
    #include <cstring>
    #include <cstdio>
    #include <cmath>
    
    using namespace std ;
    
    int n,m;
    int a[21];
    int t = 0;
    int ans = 0;
    
    void in();
    void serch(int x,int k);
    bool judge(int x);
    
    int main()
    {
    	//freopen("select.in","r",stdin);
    	//freopen("select.out","w",stdout);
    	in();
    	serch(0,m);
    	cout << ans << endl;
    	return 0;
    }
    
    void in()
    {
    	cin >> n >> m ;
    	for (int i = 1;i<=n;i ++ )
    	{
    		cin >> a[i];
    	}
    }
    
    void serch(int x,int k)
    {
    	if (k == 0)
    	{
    		if (judge(t)) ans ++ ;
    		return;
    	}
    	for (int i = x+1;i <= n-k+1;i ++ )
    	{
    		t+=a[i];
    		serch(i,k-1);
    		t-=a[i];
    	}
    }
    
    bool judge(int x)
    {
    	if (x == 1)return false;
    	if (x == 2)return true;
    	int temp = floor(sqrt(x + 1));
    	for (int i = 2 ;i <= temp ; i ++ )
    	{
    		if (x % i == 0) return false;
    	}
    	return true;
    }
    
    
    • 1

    Information

    ID
    17963
    Time
    1000ms
    Memory
    128MiB
    Difficulty
    (None)
    Tags
    # Submissions
    0
    Accepted
    0
    Uploaded By