#include #include #include #include #include #include #include //#define MAX_N 1000000001 void search(std::string curr_str, bool *is_prime, int depth, int *max_depth){ if(curr_str.empty() || !(is_prime[std::stoi(curr_str)])) { *max_depth = std::max(depth, *max_depth); return; } for(int i = 0; i < curr_str.size(); i++){ std::string new_str(""); for(int j = 0; j < curr_str.size(); j++){ if(j != i){ new_str += curr_str[j]; } } if(!(new_str.empty())) { new_str = std::to_string(std::stoi(new_str)); } search(new_str, is_prime, depth + 1, max_depth); } } int main() { char *in_str = NULL; scanf("%ms", &in_str); std::string s(in_str); int MAX_N = std::stoi(in_str) + 1; bool *is_prime = (bool *)malloc(sizeof(bool) * MAX_N); memset(is_prime, true, sizeof(bool) * MAX_N); is_prime[0] = false; is_prime[1] = false; for(int i = 2; i < sqrt(MAX_N); i++){ if(is_prime[i]){ for(int j = i + i; j < MAX_N; j += i){ is_prime[j] = false; } } } //std::vector in_vec; //char tmp; /* char *in_str = NULL; scanf("%ms", &in_str); std::string s(in_str); */ // std::set primes_found; int max_depth = 0; search(in_str, is_prime, 0, &max_depth); printf("%d\n", max_depth); free(in_str); in_str = NULL; free(is_prime); is_prime = NULL; return 0; }