Advertisement
Guest User

Untitled

a guest
Nov 21st, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.97 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <set>
  4. #include <map>
  5. #include <unordered_set>
  6. #include <unordered_map>
  7. #include <queue>
  8. #include <deque>
  9. #include <algorithm>
  10. #include <stack>
  11. #include <string>
  12. #include <cmath>
  13. #include <fstream>
  14. #include <iomanip>
  15.  
  16.  
  17. using namespace std;
  18.  
  19. #define fq(qq) for (int q = 0; q < qq; ++q)
  20. #define pb push_back
  21. #define fi(n) for (int i = 0; i < n; ++i)
  22.  
  23. bool prime(int n) {
  24.     for (int i = 2; i <= sqrt(n) + 1; ++i) {
  25.         if (n % i == 0) {
  26.             return false;
  27.         }
  28.     }
  29.     return true;
  30. }
  31.  
  32. bool pal(int n, int k) {
  33.     vector<int> v;
  34.     while (n > 0) {
  35.         v.pb(n % k);
  36.         n /= k;
  37.     }
  38.     n = (int)v.size();
  39.     fi(n / 2) {
  40.         if (v[i] != v[n - i - 1]) {
  41.             return false;
  42.         }
  43.     }
  44.     return true;
  45. }
  46.  
  47. int main() {
  48.     int qq = 1000;
  49.     fq(qq) {
  50.         int n = rand() % 200;
  51.         bool ans = true;
  52.         for (int i = 2; i <= n - 2; ++i) {
  53.             if (pal(n, i)) {
  54.                 ans = false;
  55.             }
  56.         }
  57.        
  58.         if (n <= 4 || n == 6) {
  59.            // cout << "YES\n";
  60.             if (ans == false) {
  61.                 cout << n << " " << ans << "\n";
  62.             }
  63.             continue;
  64.         }
  65.         if (!prime(n)) {
  66.             //cout << "NO\n";
  67.             if (ans) {
  68.                 cout << n << " " << ans << "\n";
  69.             }
  70.             continue;
  71.         }
  72.         bool flag = false;
  73.         for (int i = 2; i <= min(n - 2, (int)sqrt(n)); ++i) {
  74.             if (pal(n, i)) {
  75.                 flag = true;
  76.             }
  77.         }
  78.         if (flag) {
  79.             if (ans) {
  80.                 cout << n << " " << ans << "\n";
  81.             }
  82.             //cout << "NO\n";
  83.         } else {
  84.             if (!ans) {
  85.                 cout << n << " " << ans << "\n";
  86.             }
  87.             //cout << "YES\n";
  88.         }
  89.     }
  90.     return 0;
  91. }
  92.  
  93. /*
  94.  3 -1 -3
  95.  0 3 3 0
  96.  */
  97.  
  98. /*
  99.  2 1000 2000
  100.  1299 1701
  101.  */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement