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 14:02:14
Task:Interesting number
Sender:LTR
Submission time:2016-05-28 14:02:14
Status:READY
Result:ACCEPTED

Show test data

Code

#include <iostream>
#include <vector>
#include <cstdio>
#include <string>
#include <algorithm>

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

bool ispalindrome(int x)
{
	std::string s = std::to_string(x);
	for (int i = 0; i < int(s.size()) / 2; ++i) {
		if (s[i] != s[s.size() - i - 1]) return false;
	}
	return true;
}

int main()
{
	std::vector<int> S;
	for (int i = 2; i < 1000; ++i) {
		if (isprime(i) && ispalindrome(i)) 
			S.push_back(i);
	}

	int t;
	std::cin >> t;
	for (int ti = 0; ti < t; ++ti) {
		int n;
		std::cin >> n;
		std::vector<int> vals;
		for (int ni = 0; ni < n; ++ni) {
			int x;
			std::cin >> x;
			vals.push_back(x);
		}
		for (int x : vals) {
			if (std::binary_search(S.begin(), S.end(), x)) {
				std::cout << x << std::endl;
				break;
			}
		}
	}
}