#include<iostream> #include<cmath> #include<cstring> #include<algorithm> #include<cstdio> using namespace std; class PrimePalindromic { public: string str(int n) { char arr[10]; sprintf(arr, "%d", n); return arr; } bool pal(string s) { int len = s.size(); for (int i = 0; i < len / 2; i++) if(s[i] != s[len - i - 1]) return false; return true; } int count(int A, int B) { int T[20]; int res = 0; for (int k = A; k <= B; k++) { int num = k, j = 0; for (int i = 2; i <= sqrt(num); i++) while(num % i == 0) num /= i, T[j++] = i; if(num > 1) T[j++] = num; bool find = false; do { string s = ""; for (int i = 0; i < j; i++) s += str(T[i]); if(pal(s)) find = true; } while(next_permutation(T, T + j) && !find); if(find) res++; } return res; } }; int main() { int a, b; while(cin >> a >> b) { cout << PrimePalindromic().count(a, b) << endl; } return 0; }
miércoles, 19 de octubre de 2011
palindromics primes TopCoder
Suscribirse a:
Enviar comentarios (Atom)
No hay comentarios:
Publicar un comentario