// // main.cpp // Liczby drugie // // Created by Cezary Chodun on 11/14/17. // Copyright © 2017 CCH. All rights reserved. // #include <algorithm> #include <iostream> #include <vector> #include <bitset> #include <cmath> using namespace std; const int size = 10 * 1000 * 1000 + 12; int tab[size], tsize = 0; bitset<size> bts; bool isSmallPrime(int x){ int s = 0; int e = tsize-1; while(s <= e){ int m = (s+e)>>1; if(tab[m] > x) e = m-1; else if(tab[m] < x) s = m+1; else return true; } return false; } bool isBigPrime(long long x){ long long mm = sqrt(x); for(int i = 0; i < tsize; i++){ if(tab[i] > mm) break; if(x == (long long)tab[i]) return true; if(x%tab[i] == 0) return false; } return true; } long long tens[15]; int len; long long clamp(int off, long long val, bool left){ if(left) return val/(tens[len-off]); else return val%(tens[off]); } int calcLength(long long a){ int o = 0; while(a > 0){ o++; a /= 10; } return o; } int main(int argc, const char * argv[]) { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); bts.reset(); for(int i = 2; i < size; i++) if(bts[i] == 0){ tab[tsize] = i; tsize++; long long d = (long long) i*i; if(d < size) for(int j = (int)d; j < size; j+=i) bts[j] = 1; } tens[0] = 1; for(int i = 1; i < 15; i++) tens[i] = tens[i-1]*10; long long a; cin >> a; len = calcLength(a); int dlen = len/2; for(int i = 1; i <= dlen; i++){ int c = (int)clamp(i, a, true); long long r = clamp(len-i, a, false); //cout << c << " " << r << endl; if(c < tens[i-1] || r < (long long)tens[len-i-1]); //cout << "huj\n"; else if(isSmallPrime(c)) if(isBigPrime(r)){ cout << "TAK\n"; return 0; } c = (int)clamp(i, a, false); r = clamp(len-i, a, true); if(c < tens[i-1] && r < tens[len-i-1]); else if(isSmallPrime(c)) if(isBigPrime(r)){ cout << "TAK\n"; return 0; } } cout << "NIE\n"; 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 | // // main.cpp // Liczby drugie // // Created by Cezary Chodun on 11/14/17. // Copyright © 2017 CCH. All rights reserved. // #include <algorithm> #include <iostream> #include <vector> #include <bitset> #include <cmath> using namespace std; const int size = 10 * 1000 * 1000 + 12; int tab[size], tsize = 0; bitset<size> bts; bool isSmallPrime(int x){ int s = 0; int e = tsize-1; while(s <= e){ int m = (s+e)>>1; if(tab[m] > x) e = m-1; else if(tab[m] < x) s = m+1; else return true; } return false; } bool isBigPrime(long long x){ long long mm = sqrt(x); for(int i = 0; i < tsize; i++){ if(tab[i] > mm) break; if(x == (long long)tab[i]) return true; if(x%tab[i] == 0) return false; } return true; } long long tens[15]; int len; long long clamp(int off, long long val, bool left){ if(left) return val/(tens[len-off]); else return val%(tens[off]); } int calcLength(long long a){ int o = 0; while(a > 0){ o++; a /= 10; } return o; } int main(int argc, const char * argv[]) { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); bts.reset(); for(int i = 2; i < size; i++) if(bts[i] == 0){ tab[tsize] = i; tsize++; long long d = (long long) i*i; if(d < size) for(int j = (int)d; j < size; j+=i) bts[j] = 1; } tens[0] = 1; for(int i = 1; i < 15; i++) tens[i] = tens[i-1]*10; long long a; cin >> a; len = calcLength(a); int dlen = len/2; for(int i = 1; i <= dlen; i++){ int c = (int)clamp(i, a, true); long long r = clamp(len-i, a, false); //cout << c << " " << r << endl; if(c < tens[i-1] || r < (long long)tens[len-i-1]); //cout << "huj\n"; else if(isSmallPrime(c)) if(isBigPrime(r)){ cout << "TAK\n"; return 0; } c = (int)clamp(i, a, false); r = clamp(len-i, a, true); if(c < tens[i-1] && r < tens[len-i-1]); else if(isSmallPrime(c)) if(isBigPrime(r)){ cout << "TAK\n"; return 0; } } cout << "NIE\n"; return 0; } |