Code Submission Evaluation System Login

CSES - HIIT Open 2016

HIIT Open 2016

Contest start:2016-05-28 11:00:00
Contest end:2016-05-28 16:00:00

Task list | Submit code | Submissions | Messages | Scoreboard | Statistics


History
2016-05-28 11:45:25
Task:Interesting number
Sender:Anonyymit Algoritmistit
Submission time:2016-05-28 11:45:25
Status:READY
Result:ACCEPTED

Show test data

Code

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

bool palindrome(int x) {
  stringstream s;
  s << x;
  string s1;
  s >> s1;
  string s2(s1.rbegin(), s1.rend());
  return s1 == s2;
}

bool prime(int x) {
  if (x == 1) return false;
  if (x == 2) return true;
  if ((x % 2) == 0) return false;
  for (int i = 3; i*i <= x; i += 2) {
    if ((x % i) == 0) return false;
  }
  return true;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  
  int t;
  cin >> t;
  
  for (int i = 0; i < t; ++i) {
    int n;
    cin >> n;
    int v = -1;
    for (int j = 0; j < n; ++j) {
      int x;
      cin >> x;
      if (v == -1) {
	if (prime(x) && palindrome(x))
	  v = x;
      }
    }
    cout << v << endl;
  }
}