请问这里去重用map怎么不行
查看原帖
请问这里去重用map怎么不行
1302337
mishearstar楼主2024/10/8 23:51
#include<iostream>
#include<map>

using namespace std;

const int N = 30;

map <int,int>q;
int a[N];
int n,k;
long long cnt;
bool st[N];

bool is_prime(int x)
{
    if(x == 1) return false;

    for(int i = 2; i <= x / i; i++)
    {
        if(x % i == 0) return false;
    }
    return true;
}

void dfs(int i,int sum)
{
    if(i == k)
    {
        if(is_prime(sum) && q[sum] != 1) 
        {
            cnt++;
            q[sum] = 1;
        }
        return ;
    }

    for(int j = 0; j < n; j++)
    {
        if(!st[j])
        {
            st[j] = true;
            dfs(i+1,sum + a[j]);
            st[j] = false;
        }
    }

}

int main()
{
    cin >>n >>k;

    for(int i = 0; i < n; i++) cin >>a[i];

    dfs(0,0);

    cout <<cnt;
    return 0;

}
2024/10/8 23:51
加载中...