Advertisement
Guest User

AntiHash

a guest
Nov 8th, 2013
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.76 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <string>
  5.  
  6. using namespace std;
  7.  
  8.  
  9. struct Number {
  10.   long long value;
  11.   vector<int> add;
  12.   vector<int> sub;
  13. };
  14.  
  15. bool operator<(Number n1, Number n2) {
  16.   return n1.value < n2.value;
  17. }
  18.  
  19. Number sub(Number n1, const Number& n2, long long m) {
  20.   n1.value = ((n1.value - n2.value) % m + m) % m;
  21.   copy(n2.add.begin(), n2.add.end(), back_inserter(n1.sub));
  22.   copy(n2.sub.begin(), n2.sub.end(), back_inserter(n1.add));            
  23.   return n1;
  24. }
  25.  
  26. long long h(string s, long long p, long long m) {
  27.   long long h = 0;
  28.   long long q = 1;
  29.   for (int i = 0; i < s.size(); ++i) {
  30.     h = (h + s[i] * q) % m;
  31.     q = (p * q) % m;
  32.   }
  33.   return h;
  34. }
  35.  
  36. bool solve(long long p, long long m, int n) {  
  37.   long long q = 1;
  38.   vector<Number> numbers(n);
  39.   for (int i = 0; i < n; ++i) {
  40.     numbers[i].value = q;  
  41.     numbers[i].add.push_back(i);
  42.     q = (q * p) % m;
  43.   }
  44.  
  45.  
  46.   while (numbers.size() > 1) {
  47.     sort(numbers.begin(), numbers.end());
  48.     if (numbers[0].value == 0) break;
  49.     for (int i = 0; i < numbers.size() / 2; ++i) {
  50.       Number& n1 = numbers[2 * i + 0];
  51.       Number& n2 = numbers[2 * i + 1];
  52.       numbers[i] = sub(n2, n1, m);
  53.     }
  54.     numbers.resize(numbers.size() / 2);
  55.   }
  56.   if (numbers[0].value != 0) {
  57.     return false;
  58.   }
  59.   string s1 = "";
  60.   string s2 = "";
  61.   for (int i = 0; i < n; ++i) {
  62.     s1 += 'a';
  63.     s2 += 'a';
  64.   }
  65.   for (int i : numbers[0].add) s1[i]++;
  66.   for (int i : numbers[0].sub) s2[i]++;
  67.   cout << s1 << endl;
  68.   cout << s2 << endl;
  69.   cout << h(s1, p, m) << endl;
  70.   cout << h(s2, p, m) << endl;
  71.   return true;
  72. }
  73.  
  74. int main() {
  75.   for (int n = 1; n <= 1000; ++n) {
  76.     if (solve(31, 1000000007, n)) {
  77.       break;
  78.     }
  79.   }
  80.   return 0;
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement