prime ring

#include<iostream> using namespace std; int a[55], n, cnt; int visit[55]={0}; int mang[55]; bool checknt(int x) { for(int i = 2; i < x; i++) { if( x % i == 0) return false; } return true; } void DFS(int cur, int step) { int sum = 0; //visit[cur] = 1; if(step == n){ if( checknt(a[0] + a[cur])) cnt ++; }else{ for(int i = 0; i < n ; i++) { if( visit[i] == 0) { visit[i] = 1; sum = a[i] + a[cur]; if( checknt(sum)) { DFS(i,step+1); } visit[i] =0; } } } } int main() { int test_case; int T; int Answer; ios::sync_with_stdio(false); freopen("input.txt", "r", stdin); cin >> T; for(test_case = 1; test_case <= T; ++test_case) { //Answer = 0; cin >> n; for(int i = 0; i < n; i++) cin >> a[i]; /*for(int i = 0; i < n; i++) cout << a[i];*/ cnt = 0; visit[0] = 1; DFS(0,1); cout << "Case " << test_case << ": " << cnt<< endl; } return 0; }

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.