Tarango

HR B

Oct 21st, 2015
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.84 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define Size 20000
  4.  
  5. int Set(int N,int pos){
  6.     return N = N | (1<<pos);
  7. }
  8.  
  9. bool Check(int N,int pos){
  10.     return (bool)(N & (1<<pos));
  11. }
  12.  
  13. bool mark[Size];
  14. vector <int> primeList;
  15. void seive(){
  16.     memset (mark, true, sizeof (mark));
  17.     mark [0] = mark [1] = false;
  18.     for(int i = 4;i<Size;i+=2) mark [i] = false;
  19.     for(int i=3;i*i<=Size;i+=2){
  20.         if(mark[i]){
  21.             for(int j=i*i;j<Size;j+=2*i)
  22.                 mark [j] = false;
  23.         }
  24.     }
  25.     primeList.clear ();
  26.     primeList.push_back (2);
  27.     for(int i=3;i<Size;i+=2){
  28.         if(mark[i]){
  29.             primeList.push_back (i);
  30.         }
  31.     }
  32. }
  33.  
  34. int L = 14;
  35. bool f[15];
  36. int N,num,last;
  37. vector<int> Graph[15];
  38. bool found = false;
  39.  
  40. void call(int cnt,int val){
  41.     if(val == num){
  42.         found = true;
  43.         return;
  44.     }
  45.     if(cnt > 4) return;
  46.  
  47.     for(int i = 0;i<15;i++){
  48.         if(Check(val,i) == false && f[i] == true){
  49.             int Sz = Graph[i].size();
  50.             int nVal = val;
  51.             for(int c = 0;c<Sz;c++){
  52.                 int adj = Graph[i][c];
  53.                 call(cnt+1,nVal | adj);
  54.                 if(found == true) return;
  55.             }
  56.         }
  57.     }
  58. }
  59.  
  60. int main() {
  61.     seive();
  62.     int last = primeList.size();
  63.     cin >> N;
  64.     for(int i = 0;i<N;i++){
  65.         cin >> num;
  66.         memset(f,false,sizeof(f));
  67.         for(int i = 0;i<15;i++){
  68.             if(Check(num,i) == true){
  69.                 f[i] = true;
  70.             }
  71.             Graph[i].clear();
  72.         }
  73.         for(int i = 0;i<last;i++){
  74.             int cur = primeList[i];
  75.             bool ok = true;
  76.             for(int c = 0;c<15;c++){
  77.                 if(Check(cur,c) == true && f[c] == false){
  78.                     ok = false;
  79.                     break;
  80.                 }
  81.             }
  82.             if(ok == true){
  83.                 for(int c = 0;c<15;c++){
  84.                     if(Check(cur,c) == true){
  85.                         Graph[c].push_back(cur);
  86.                     }
  87.                 }
  88.             }
  89.         }
  90.         found = false;
  91.         call(0,0);
  92.         if(found == true){
  93.             cout << "YES" << endl;
  94.         }else{
  95.             cout << "NO" << endl;
  96.         }
  97.     }
  98.     return 0;
  99. }
Advertisement
Add Comment
Please, Sign In to add comment