Untitled
//Prime Ring #include<iostream> using namespace std; int n; int arr[100]; int ans; bool visited[100]; bool check(int k2){ if(visited[k2] == true) return false; } bool isPrime(int a){ if(a<2) return false; for(int i=2; i*i <= a; i++){ if(a%i == 0){ return false; } } return true; } void BT(int index, int cur, int prev, int head){ if(prev != -1 && !isPrime(arr[cur]+arr[prev])) { return; } if(index == n){ if(isPrime(arr[cur]+arr[head])){ ans++; } return; } for(int i=1; i< n; i++){ if(!visited[i]){ visited[i] = true; BT(index+1, i, cur, head); visited[i] = false; } } } int main(){ freopen("Text.txt", "r", stdin); int T; cin >> T; for(int tc=1; tc <= T; tc++){ cin >> n; for(int i=0; i<n; i++){ cin >> arr[i]; visited[i] = false; } ans = 0; visited[0] = true; BT(1, 0, -1, 0); cout<< "Case " << tc << ": " << ans << endl; } return 0; }
Leave a Comment