Guest User

Untitled

a guest
Oct 6th, 2013
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.64 KB | None | 0 0
  1. #include <algorithm>
  2. #include <cstdio>
  3. #include <iostream>
  4. #include <set>
  5. #include <string>
  6. #include <vector>
  7.  
  8. using namespace std;
  9. typedef long long i64;
  10.  
  11. set<int> ps;
  12. set<int> n_ps;
  13.  
  14. string Gen() {
  15.     string res;
  16.     for (int i = 0; i < 100; i++) {
  17.         res += (char)(rand() % 26 + 'a');
  18.     }
  19.     return res;
  20. }
  21.  
  22. int GetHash(const string &s, int x, int p) {
  23.     int xi = 1;
  24.     int res = 0;
  25.     for (int i = 0; i < s.size(); i++) {
  26.         res = (res + (s[i] - 'a' + 1) * (i64)xi) % p;
  27.         xi = ((i64)xi * x) % p;
  28.     }
  29.     return res;
  30. }
  31.  
  32. int main() {
  33.     string s;
  34.     cin >> s;
  35.     cout << "aa\n";
  36.     cout.flush();
  37.     int x;
  38.     cin >> x;
  39.     x--;
  40.     i64 xi = x;
  41.     int i = 1;
  42.     while ((xi - 1) / (x - 1) < 2000000000) {
  43.         xi *= x;
  44.         i++;
  45.     }
  46.     cout << string(i, 'a') << "\n";
  47.     cout.flush();
  48.     int h;
  49.     cin >> h;
  50.     i64 pi = (xi - 1) / (x - 1) - h;
  51.     for (int i = 300; i >= 1; i--) {
  52.         if (pi % i != 0) {
  53.             continue;
  54.         }
  55.         i64 tmp_p = pi / i;
  56.         if (tmp_p < 1000000000 || tmp_p > 2000000000) {
  57.             continue;
  58.         }
  59.         if ((xi - 1) / (x - 1) % tmp_p == h) {
  60.             ps.insert(tmp_p);
  61.         }
  62.     }
  63.     while (ps.size() > 1) {
  64.         s = Gen();
  65.         cout << s << "\n";
  66.         cout.flush();
  67.         cin >> h;
  68.         n_ps.clear();
  69.         for (set<int>::iterator it = ps.begin(); it != ps.end(); it++) {
  70.             int p = *it;
  71.             int curr_h = GetHash(s, x, p);
  72.             if (h == curr_h) {
  73.                 n_ps.insert(p);
  74.             }
  75.         }
  76.         ps = n_ps;
  77.     }
  78.     int p = *ps.begin();
  79.     cout << "answer: " << x << " " << p << "\n";
  80.     cout.flush();
  81. }
Advertisement
Add Comment
Please, Sign In to add comment