753_C. Interactive Bulls and Cows (Hard)

The only difference from the previous problem is the constraint on the number of requests. In this problem your program should guess the answer doing at most 7 requests.

This problem is a little bit unusual. Here you are to implement an interaction with a testing system. That means that you can make queries and get responses in the online mode. Please be sure to use the stream flushing operation after each query's output in order not to leave part of your output in some buffer. For example, in C++ you've got to use the fflush(stdout) function, in Java — call System.out.flush(), and in Pascal — flush(output).

Bulls and Cows (also known as Cows and Bulls or Pigs and Bulls or Bulls and Cleots) is an old code-breaking paper and pencil game for two players, predating the similar commercially marketed board game Mastermind.

On a sheet of paper, the first player thinks a secret string. This string consists only of digits and has the length 4. The digits in the string must be all different, no two or more equal digits are allowed.

Then the second player tries to guess his opponent's string. For every guess the first player gives the number of matches. If the matching digits are on their right positions, they are "bulls", if on different positions, they are "cows". Thus a response is a pair of numbers — the number of "bulls" and the number of "cows". A try can contain equal digits.

More formally, let's the secret string is s and the second player are trying to guess it with a string x. The number of "bulls" is a number of such positions i (1 ≤ i ≤ 4) where s[i] = x[i]. The number of "cows" is a number of such digits c that s contains c in the position i (i.e. s[i] = c), x contains c, but x[i] ≠ c.

For example, the secret string is "0427", the opponent's try is "0724", then the answer is 2 bulls and 2 cows (the bulls are "0" and "2", the cows are "4" and "7"). If the secret string is "0123", the opponent's try is "0330", then the answer is 1 bull and 1 cow.

In this problem you are to guess the string s that the system has chosen. You only know that the chosen string consists of 4 distinct digits.

You can make queries to the testing system, each query is the output of a single 4-digit string. The answer to the query is the number of bulls and number of cows. If the system's response equals "4 0", that means the interaction with your problem is over and the program must terminate. That is possible for two reasons — the program either guessed the number x or made an invalid action (for example, printed letters instead of digits).

Your program is allowed to do at most 7 queries.

You can hack solutions of other participants providing a 4-digit string containing distinct digits — the secret string.

Input

To read answers to the queries, the program must use the standard input.

The program will receive pairs of non-negative integers in the input, one pair per line. The first number in a pair is a number of bulls and the second one is a number of cows of the string s and the string xi printed by your program. If the system response equals "4 0", then your solution should terminate.

The testing system will let your program read the i-th pair of integers from the input only after your program displays the corresponding system query in the output: prints value xi in a single line and executes operation flush.

Output

The program must use the standard output to print queries.

Your program must output requests — 4-digit strings x1, x2, ..., one per line. After the output of each line the program must execute flush operation. The program should read the answer to the query from the standard input.

Your program is allowed to do at most 7 queries.

Examples

Input

0 1
2 0
1 1
0 4
2 1
4 0


Output

8000
0179
3159
3210
0112
0123

Note

The secret string s in the example is "0123".

Example input:
0 1
2 0
1 1
0 4
2 1
4 0


Example output:
8000
0179
3159
3210
0112
0123

Tags: brute force
constructive algorithms
interactive
Difficulty rating: 2500

Solution:
#include <bits/stdc++.h>
using namespace std;
struct Q {
  int a, b, c, d;
  Q(int x, int y, int z, int w) : a(x), b(y), c(z), d(w) {}
};
pair<int, int> query(Q z) {
  int u, v;
  printf("%d%d%d%d\n", z.a, z.b, z.c, z.d);
  fflush(stdout);
  scanf("%d%d", &u, &v);
  return make_pair(u, v);
}
pair<int, int> dist(Q e, Q f) {
  int bulls = 0, cows = 0;
  if (e.a == f.a) bulls++;
  if (e.b == f.b) bulls++;
  if (e.c == f.c) bulls++;
  if (e.d == f.d) bulls++;
  if ((e.a == f.a) || (e.a == f.b) || (e.a == f.c) || (e.a == f.d)) cows++;
  if ((e.b == f.a) || (e.b == f.b) || (e.b == f.c) || (e.b == f.d)) cows++;
  if ((e.c == f.a) || (e.c == f.b) || (e.c == f.c) || (e.c == f.d)) cows++;
  if ((e.d == f.a) || (e.d == f.b) || (e.d == f.c) || (e.d == f.d)) cows++;
  return make_pair(bulls, cows - bulls);
}
int main() {
  srand(2333333);
  vector<Q> X;
  for (int i = 0; i < 10; ++i)
    for (int j = 0; j < 10; ++j)
      if (i - j) {
        for (int k = 0; k < 10; ++k)
          if ((i - k) && (j - k)) {
            for (int l = 0; l < 10; ++l)
              if ((i - l) && (j - l) && (k - l)) {
                Q h(i, j, k, l);
                X.push_back(h);
              }
          }
      }
  int bulls = 0, cows = 0, k = 0;
  while (bulls < 4) {
    random_shuffle(X.begin(), X.end());
    Q V = X[0];
    pair<int, int> Z = query(V);
    bulls = Z.first;
    cows = Z.second;
    vector<Q> Y;
    k++;
    for (int i = 0; i < X.size(); ++i) {
      pair<int, int> T = dist(V, X[i]);
      if (T.first == bulls && T.second == cows) Y.push_back(X[i]);
    }
    X = Y;
  }
  return 0;
}