CSES - HIIT Open 2016 - Results
Submission details
Task:Interesting number
Sender:Anonyymit Algoritmistit
Submission time:2016-05-28 11:45:25 +0300
Language:C++
Status:READY
Result:ACCEPTED
Test results
testverdicttime
#1ACCEPTED0.05 sdetails
#2ACCEPTED0.06 sdetails

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;
  }
}

Test details

Test 1

Verdict: ACCEPTED

input
1000
9
300 988 956 931 116 3 386 202 ...

correct output
3
3
181
919
191
...

user output
3
3
181
919
191
...

Test 2

Verdict: ACCEPTED

input
1
100000
72 247 605 249 10 422 594 490 ...

correct output
191

user output
191