#include <iostream> #include <unordered_map> using namespace std; unordered_map<long, bool> primals; bool isCorrect(string num) { return num[0] != '0'; } bool isPrimal(string num) { char* endptr = NULL; long number = strtoll(num.c_str(), &endptr, 10); if (primals.find(number) == primals.end()) { for (int i = 2; i * i < number; i += 1) { if (number % i == 0) { primals[number] = false; return false; } } primals[number] = true; return true; } return primals[number]; } int main(int argc, char *argv) { primals[1] = false; primals[2] = true; string num; cin >> num; for (int i = 1; i < num.length(); i++) { string num1 = num.substr(0, i); string num2 = num.substr(i, num.length()); if (isCorrect(num1) && isCorrect(num2) && isPrimal(num1) && isPrimal(num2)) { cout << "TAK" << endl; return 0; } } cout << "NIE" << endl; return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | #include <iostream> #include <unordered_map> using namespace std; unordered_map<long, bool> primals; bool isCorrect(string num) { return num[0] != '0'; } bool isPrimal(string num) { char* endptr = NULL; long number = strtoll(num.c_str(), &endptr, 10); if (primals.find(number) == primals.end()) { for (int i = 2; i * i < number; i += 1) { if (number % i == 0) { primals[number] = false; return false; } } primals[number] = true; return true; } return primals[number]; } int main(int argc, char *argv) { primals[1] = false; primals[2] = true; string num; cin >> num; for (int i = 1; i < num.length(); i++) { string num1 = num.substr(0, i); string num2 = num.substr(i, num.length()); if (isCorrect(num1) && isCorrect(num2) && isPrimal(num1) && isPrimal(num2)) { cout << "TAK" << endl; return 0; } } cout << "NIE" << endl; return 0; } |