Участник:Kachanov vv/MB1

Материал из DISCOPAL
Перейти к: навигация, поиск
https://www.spoj.com/problems/MB1/
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
 
bool is_prime[2097152];
vector<size_t> prime;
vector<size_t> pprime;
 
bool is_palin(const string &s) {
    auto t = s;
    reverse(t.begin(), t.end());
    return t == s;
}
 
int main() {
    is_prime[2] = true;
    for(size_t i = 3; i < 2097152; i+=2){
    	is_prime[i] = true;
    }
    for (size_t i = 3; i < 2097152; i += 2){
    	for (size_t j = i*i; j < 2097152 and is_prime[i]; j+=i){
    		is_prime[j] = false;
    	}
    }
 
    for(size_t i = 0; i < 2097152; i++)
        if(is_prime[i]){
        	prime.push_back(i);
        }
 
    for(size_t i = 0; i < prime.size(); i++){
        string s = to_string(prime[i]);
        if(is_palin(s)) {
            pprime.push_back(prime[i]);
        }
    }
 
    int t;
    cin >> t;
    while(t--){
    	int n;
        cin >> n;
 
        size_t pprime_prod = 1;
        for (size_t num = pprime[n - 1]; num; num /= 10 ){
        	size_t acc = num % 10;
        	if(acc){
        		pprime_prod *= num % 10;
        	}
        }
 
        cout << pprime[n - 1] << " " << prime[pprime_prod - 1] << endl;
    }
    return 0;
}