#include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <vector> using namespace std; const int K = 20; struct Numbers { long long num1; long long num2; }; const long long powerOf2[31] = { 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536, 1<<17, 1<<18, 1<<19, 1<<20, 1<<21, 1<<22, 1<<23, 1<<24, 1<<25, 1<<26, 1<<27, 1<<28, 1<<29, 1<<30 }; // a^b mod m long long powerModulo(const long long a, const long long b, const long long m) { long long result = 1; long long x = a%m; for(long long i = 1; i <= b; i <<= 1) { x %= m; if (b&i) { result *= x; result %= m; } x *= x; } return result; } bool millerRabin(const long long n) { if (n < 4) { return n > 1; } if (n%2 == 0) { return false; } int s = 0; long long s2 = 1; while((s2 & (n - 1)) == 0) { s++; s2 <<= 1; } const long long d = n/s2; for(int i = 0; i < K; i++) { const long long a = 1 + (long long)((n - 1)*rand()/(RAND_MAX + 1.0)); if (powerModulo(a, d, n) != 1) { for(int r = 0; r <= s - 1; r++) { if (powerModulo(a, powerOf2[r]*d, n) == n - 1) { return true; } } return false; } } return true; } bool isPrime(const long long n) { if (n > 2 && (n & 1)) { int t = 0; for(long long m = 1 + sqrt(n); m > 1; m--) { t += ((n % m) == 0); } return t == 0; } else { return n == 2; } } vector<Numbers> extract(const char* const s) { Numbers a = { 0, atoll(s) }; vector<Numbers> result; for(const char* p = s; p[1]; p++) { a.num1 = 10*a.num1 + p[0] - '0'; a.num2 = atoll(p + 1); if (p[1] != '0') { result.push_back(a); } } return result; } int main() { char a[16]; while(scanf("%s", a) > 0) { vector<Numbers> numbers = extract(a); const char* result = "NIE\n"; for(vector<Numbers>::const_iterator i = numbers.begin(); i != numbers.end(); i++) { if (millerRabin(i->num1) && millerRabin(i->num2) && isPrime(i->num1) && isPrime(i->num2)) { result = "TAK\n"; break; } } printf(result); } 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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 | #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <vector> using namespace std; const int K = 20; struct Numbers { long long num1; long long num2; }; const long long powerOf2[31] = { 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536, 1<<17, 1<<18, 1<<19, 1<<20, 1<<21, 1<<22, 1<<23, 1<<24, 1<<25, 1<<26, 1<<27, 1<<28, 1<<29, 1<<30 }; // a^b mod m long long powerModulo(const long long a, const long long b, const long long m) { long long result = 1; long long x = a%m; for(long long i = 1; i <= b; i <<= 1) { x %= m; if (b&i) { result *= x; result %= m; } x *= x; } return result; } bool millerRabin(const long long n) { if (n < 4) { return n > 1; } if (n%2 == 0) { return false; } int s = 0; long long s2 = 1; while((s2 & (n - 1)) == 0) { s++; s2 <<= 1; } const long long d = n/s2; for(int i = 0; i < K; i++) { const long long a = 1 + (long long)((n - 1)*rand()/(RAND_MAX + 1.0)); if (powerModulo(a, d, n) != 1) { for(int r = 0; r <= s - 1; r++) { if (powerModulo(a, powerOf2[r]*d, n) == n - 1) { return true; } } return false; } } return true; } bool isPrime(const long long n) { if (n > 2 && (n & 1)) { int t = 0; for(long long m = 1 + sqrt(n); m > 1; m--) { t += ((n % m) == 0); } return t == 0; } else { return n == 2; } } vector<Numbers> extract(const char* const s) { Numbers a = { 0, atoll(s) }; vector<Numbers> result; for(const char* p = s; p[1]; p++) { a.num1 = 10*a.num1 + p[0] - '0'; a.num2 = atoll(p + 1); if (p[1] != '0') { result.push_back(a); } } return result; } int main() { char a[16]; while(scanf("%s", a) > 0) { vector<Numbers> numbers = extract(a); const char* result = "NIE\n"; for(vector<Numbers>::const_iterator i = numbers.begin(); i != numbers.end(); i++) { if (millerRabin(i->num1) && millerRabin(i->num2) && isPrime(i->num1) && isPrime(i->num2)) { result = "TAK\n"; break; } } printf(result); } return 0; } |