Advertisement
Guest User

Untitled

a guest
May 8th, 2012
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.85 KB | None | 0 0
  1. #include <vector>
  2. #include <list>
  3. #include <string>
  4. #include <unordered_map>
  5.  
  6. #include <math.h>
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include <stdlib.h>
  10.  
  11. using namespace std;
  12.  
  13. int nlz(unsigned x)
  14. {
  15.     int n;
  16.     if (x == 0) return 32;
  17.     n = 1;
  18.     if ((x >> 16) == 0) { n = n + 16; x = x << 16; }
  19.     if ((x >> 24) == 0) { n = n + 8; x = x << 8; }
  20.     if ((x >> 28) == 0) { n = n + 4; x = x << 4; }
  21.     if ((x >> 30) == 0) { n = n + 2; x = x << 2; }
  22.     n = n - (x >> 31);
  23.     return n;
  24. }
  25.  
  26. int bitsize (int x)
  27. {
  28.     unsigned y = (unsigned)x;
  29.     return 32 - nlz(x);
  30. }
  31.  
  32. int main()
  33. {
  34.     int n = 999999;
  35.     vector < bool > A(n, true);
  36.     typedef unordered_map < string, vector < int > > res_t;
  37.     res_t res;
  38.  
  39.     for (int i = 2; i < sqrt(n); ++i) {
  40.         if (A[i]) {
  41.             int k = 2, j = k * i;
  42.             for (; j < n; ++k, j = k * i) {
  43.                 A[j] = false;
  44.             }
  45.         }
  46.     }
  47.     for (int i = 2; i < n; ++i) {
  48.         if (A[i]) {
  49.             char buf[1024];
  50.             sprintf(buf, "%d", i);
  51.             int digits = strlen(buf);
  52.             int mask = 1;
  53.             while (bitsize(mask) <= digits) {
  54.                 char smask[1024];
  55.                 char prev = 0;
  56.                 bool ok = true;
  57.                 strcpy(smask, buf);
  58.  
  59.                 for (int k = 0; k < digits; ++k) {
  60.                     if (mask & (1 << k)) {
  61.                         if (!prev) {
  62.                             prev = smask[k];
  63.                         } else if (prev != smask[k]) {
  64.                             ok = false;
  65.                             break;
  66.                         }
  67.                         smask[k] = '*';
  68.                     }
  69.                 }
  70.                 if (ok&&prev) {
  71.                     res[smask].push_back(i);
  72.                 }
  73.                 mask ++;
  74.             }
  75.         }
  76.     }
  77.  
  78.     res_t::iterator max_it = res.begin();
  79.     for (res_t::iterator it = res.begin(); it != res.end(); ++it) {
  80.         if (max_it->second.size() < it->second.size()) {
  81.             max_it = it;
  82.         }
  83.     }
  84.     printf("%s: %d:\n", max_it->first.c_str(), max_it->second.size());
  85.     if (!max_it->second.empty()) printf(" ");
  86.     for (int i = 0; i < (int)max_it->second.size(); ++i) {
  87.         printf("%d ", max_it->second[i]);
  88.     }
  89.     if (!max_it->second.empty()) printf("\n");
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement