#include <iostream> #include <cstdio> #include <cmath> #include <set> using namespace std; int dlugoscLiczby(long long l) { int i = 1; long long pot = 10; while (l / pot != 0) { pot *= 10; ++i; } return i; } long long potega10(int p) { long long wynik = 1; for (int i = 1; i <= p; ++i) wynik *= 10; return wynik; } int main() { //printf("Hello World!\n"); int MAX_PRIME; long long liczba; int dlugosc; scanf("%lld", &liczba); //liczba = 2017; dlugosc = dlugoscLiczby(liczba); MAX_PRIME = potega10(dlugosc /2); if (MAX_PRIME < 100) { MAX_PRIME = 100; } //printf("%d \n", MAX_PRIME); int * sito; sito = (int*) malloc(MAX_PRIME * sizeof(int)); // setting up sieve sito[0] = 0; sito[1] = 0; for (int i = 2; i < MAX_PRIME; ++i) { sito[i] = 1; } // finding primes up to 10^7 int num_primes; int i; num_primes = 0; i = 2; while (i < MAX_PRIME) { if (sito[i] == 1) { ++num_primes; for (int j = 2 * i; j < MAX_PRIME; j+= i) sito[j] = 0; // removing all repetition of prime number } ++i; } //printf("Number of prime numbers %d\n", num_primes); int *primes; set<int> primesSet; primes = (int*) malloc(num_primes * sizeof(int)); int pos; pos = 0; for (int i = 0; i < MAX_PRIME; ++i) { if (sito[i] == 1) { primes[pos] = i; primesSet.insert(i); ++pos; } } //for (int i = 0; i < 20; ++i) // printf("%d\n",primes[i]); int pot = 10; bool podwojniePierwsza = false; for (int i = 1; i < dlugosc; ++i) { int gora, dol; gora = liczba / pot; dol = liczba % pot; if (dol < (pot / 10)) { //printf("BEWARE\n"); //printf("%d %d %d %d", dol, pot, pot / 10, i); continue; // leading zeros BEWARE!!! } int gp = 1; int dp = 1; if (gora < MAX_PRIME) { gp = primesSet.count(gora); } else { for (int i = 0; primes[i] < round(sqrt(gora)+1); ++i) { if (gora % primes[i] == 0) { if (gora != primes[i]) { gp = 0; //printf("%d %d\n", gora, primes[i]); } break; } } } if (dol < MAX_PRIME) { dp = primesSet.count(dol); } else { for (int i = 0; primes[i] < round(sqrt(dol)+1); ++i) { if (dol % primes[i] == 0) { if (dol != primes[i]) { dp = 0; //printf("%d %d\n", dol, primes[i]); } break; } } } // poprawic /*if ((primesSet.count(gora)) and (primesSet.count(dol))){ //printf("%d %d %d\n", gora, dol, pot); podwojniePierwsza = true; break; }*/ if ((dp) and (gp)) { podwojniePierwsza = true; break; } pot *= 10; } if (podwojniePierwsza) { printf("TAK\n"); } else { printf("NIE\n"); } free(sito); free(primes); 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 149 | #include <iostream> #include <cstdio> #include <cmath> #include <set> using namespace std; int dlugoscLiczby(long long l) { int i = 1; long long pot = 10; while (l / pot != 0) { pot *= 10; ++i; } return i; } long long potega10(int p) { long long wynik = 1; for (int i = 1; i <= p; ++i) wynik *= 10; return wynik; } int main() { //printf("Hello World!\n"); int MAX_PRIME; long long liczba; int dlugosc; scanf("%lld", &liczba); //liczba = 2017; dlugosc = dlugoscLiczby(liczba); MAX_PRIME = potega10(dlugosc /2); if (MAX_PRIME < 100) { MAX_PRIME = 100; } //printf("%d \n", MAX_PRIME); int * sito; sito = (int*) malloc(MAX_PRIME * sizeof(int)); // setting up sieve sito[0] = 0; sito[1] = 0; for (int i = 2; i < MAX_PRIME; ++i) { sito[i] = 1; } // finding primes up to 10^7 int num_primes; int i; num_primes = 0; i = 2; while (i < MAX_PRIME) { if (sito[i] == 1) { ++num_primes; for (int j = 2 * i; j < MAX_PRIME; j+= i) sito[j] = 0; // removing all repetition of prime number } ++i; } //printf("Number of prime numbers %d\n", num_primes); int *primes; set<int> primesSet; primes = (int*) malloc(num_primes * sizeof(int)); int pos; pos = 0; for (int i = 0; i < MAX_PRIME; ++i) { if (sito[i] == 1) { primes[pos] = i; primesSet.insert(i); ++pos; } } //for (int i = 0; i < 20; ++i) // printf("%d\n",primes[i]); int pot = 10; bool podwojniePierwsza = false; for (int i = 1; i < dlugosc; ++i) { int gora, dol; gora = liczba / pot; dol = liczba % pot; if (dol < (pot / 10)) { //printf("BEWARE\n"); //printf("%d %d %d %d", dol, pot, pot / 10, i); continue; // leading zeros BEWARE!!! } int gp = 1; int dp = 1; if (gora < MAX_PRIME) { gp = primesSet.count(gora); } else { for (int i = 0; primes[i] < round(sqrt(gora)+1); ++i) { if (gora % primes[i] == 0) { if (gora != primes[i]) { gp = 0; //printf("%d %d\n", gora, primes[i]); } break; } } } if (dol < MAX_PRIME) { dp = primesSet.count(dol); } else { for (int i = 0; primes[i] < round(sqrt(dol)+1); ++i) { if (dol % primes[i] == 0) { if (dol != primes[i]) { dp = 0; //printf("%d %d\n", dol, primes[i]); } break; } } } // poprawic /*if ((primesSet.count(gora)) and (primesSet.count(dol))){ //printf("%d %d %d\n", gora, dol, pot); podwojniePierwsza = true; break; }*/ if ((dp) and (gp)) { podwojniePierwsza = true; break; } pot *= 10; } if (podwojniePierwsza) { printf("TAK\n"); } else { printf("NIE\n"); } free(sito); free(primes); return 0; } |