Advertisement
Manh_LK

Quà tặng

Mar 31st, 2020
1,053
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.78 KB | None | 0 0
  1. #include<iostream>
  2. using namespace std;
  3. int n, res, Count;
  4. int A[55];
  5. int B[55];
  6. bool checkPrime(int x)
  7. {
  8.     if (x == 2) return true;
  9.     for (int i = 2; i <= x / 2; i++)
  10.     {
  11.         if (x % i == 0) return false;
  12.     }
  13.     return true;
  14. }
  15. void Try(int i)
  16. {
  17.     B[i] = 1;
  18.     Count++;
  19.     for (int j = 0; j < n; j++)
  20.     {
  21.         if (B[j] == 0 && checkPrime(A[i] + A[j]))
  22.         {
  23.             if (Count == n-1&&checkPrime(A[j]+A[0]))
  24.             {
  25.                 res += 1;
  26.             }
  27.             else
  28.             {
  29.                 Try(j);
  30.             }
  31.         }
  32.     }
  33.     B[i] = 0;
  34.     Count--;
  35. }
  36. int main()
  37. {
  38.     freopen("input.txt", "r", stdin);
  39.     int testCase;
  40.     cin >> testCase;
  41.     for (int tc = 0; tc < testCase; tc++)
  42.     {
  43.         cin >> n;
  44.         res = 0;
  45.         for (int i = 0; i < n; i++)
  46.         {
  47.             cin >> A[i];
  48.             B[i] = 0;
  49.         }
  50.         Count = 0;
  51.         Try(0);
  52.         cout << res << endl;
  53.     }
  54.     return 0;
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement