Problem: cf2109_c1
Difficulty: easy
Categories: bit, math
Time limit: 2000 ms, memory limit: 256 MB

This is the easy version of the problem. In this version, you can send at most $\mathbf{7}$ commands.

This is an interactive problem.

Welcome, Duelists! In this interactive challenge, there is an unknown integer $x$ ($1 \le x \le 10^9$). You must make it equal to a given integer in the input $n$. By harnessing the power of "Mathmech" monsters, you can send a command to do one of the following:

|  |  |  |  |  |  |
| --- | --- | --- | --- | --- | --- |
| Command | Constraint | Result | Case | Update | Jury's response |
| "add $y$" | $-10^{18} \le y \le 10^{18}$ | $\mathrm{res} = x + y$ | $\text{if } 1 \le \mathrm{res} \le 10^{18}$ | $x \leftarrow \mathrm{res}$ | "1" |
| $\mathrm{else}$ | $x \leftarrow x$ | "0" |
| "mul $y$" | $1 \le y \le 10^{18}$ | $\mathrm{res} = x \cdot y$ | $\text{if } 1 \le \mathrm{res} \le 10^{18}$ | $x \leftarrow \mathrm{res}$ | "1" |
| $\mathrm{else}$ | $x \leftarrow x$ | "0" |
| "div $y$" | $1 \le y \le 10^{18}$ | $\mathrm{res} = x/y$ | $\text{if } y$ divides $x$ | $x \leftarrow \mathrm{res}$ | "1" |
| $\mathrm{else}$ | $x \leftarrow x$ | "0" |
| "digit" | — | $\mathrm{res} = S(x)$$^{\text{∗}}$ | — | $x \leftarrow \mathrm{res}$ | "1" |

You have to make $x$ equal to $n$ using at most $\mathbf{7}$ commands.

$^{\text{∗}}$$S(n)$ is a function that returns the sum of all the individual digits of a non-negative integer $n$. For example, $S(123) = 1 + 2 + 3 = 6$

Input

Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 5000$). The description of the test cases follows.

The first and only line of each test case contains one integer $n$ ($1 \le n \le 10^9$).

Interaction

The interaction for each test case begins by reading the integer $n$.

To send a command, output a line in the following format:

* "add $y$" Add some integer $y$ ($-10^{18} \le y \le 10^{18}$) to $x$.

  The jury will output "1" if $x + y$ is within $[1, 10^{18}]$ (successful), and "0" otherwise. If successful, update $x \leftarrow x + y$.
* "mul $y$" Multiply $x$ by a positive integer $y$ ($1 \le y \le 10^{18}$).

  The jury will output "1" if $x \cdot y$ is within $[1, 10^{18}]$ (successful), and "0" otherwise. If successful, update $x \leftarrow x \cdot y$.
* "div $y$" Divide $x$ by a positive integer $y$ ($1 \le y \le 10^{18}$).

  The jury will output "1" if $y$ is a divisor of $x$ (successful), and "0" otherwise. If successful, update $x \leftarrow \frac{x}{y}$.
* "digit" Make $x$ equal to the sum of its digits.

  The jury will always output "1" and update $x \leftarrow S(x)$.

Note that commands are case sensitive.

When you have determined that $x$ is equal to $n$, output a line in the following format:

* "!" — where the jury will output a "1" if $n$ is equal to $x$, and "-1" otherwise.

Note that answering does not count toward your limit of $\mathbf{7}$ commands.

If your program makes more than $\mathbf{7}$ commands for one test case, or makes an invalid command, then the response to the command will be "-1". After receiving such a response, your program should immediately terminate to receive the verdict Wrong Answer. Otherwise, it may receive any other verdict.

After printing a command, do not forget to output the end of the line and flush the output. Otherwise, you will get Idleness limit exceeded. To do this, use:

* fflush(stdout) or cout.flush() in C++;
* System.out.flush() in Java;
* sys.stdout.flush() in Python;
* std::io::stdout().flush() in Rust;
* see the documentation for other languages.

The interactor is non-adaptive. The unknown integer $x$ does not change during the interaction.

Example

Input

```
2
100

0

1

1

1

5

1

1

1
```

Output

```
add -10

add 1

mul 10

!

digit

div 2

!
```

Note

|  |  |  |
| --- | --- | --- |
| Solution | Jury | Explanation |
|  | $\texttt{2}$ | There are 2 test cases. |
|  | $\texttt{100}$ | In the first test case, the unknown integer $x = 9$ and we have to make it equal to $n = 100$. |
| $\texttt{add -10}$ | $\texttt{0}$ | The answer to "add -10" is "0". This means that the addition command was not successful as $x + y = 9 + (-10) \le 0$, and $x$ remains $9$ after the command |
| $\texttt{add 1}$ | $\texttt{1}$ | The answer to "add 1" is "1". This means that the addition command was successful as $x + y = 9 + 1 = 10$, and $x$ changes to $10$ after the command. |
| $\texttt{mul 10}$ | $\texttt{1}$ | The answer to "mul 10" is "1". This means that the multiplication command was successful as $x \cdot y = 10 \cdot 10 = 100$, and $x$ changes to $100$ after the command. |
| $\texttt{!}$ | $\texttt{1}$ | The answer to "!" is "1". This means you have determined that $x$ equals $n$. |
|  | $\texttt{5}$ | In the second test case, the unknown integer $x = 1234$ and we have to make it equal to $n = 5$. |
| $\texttt{digit}$ | $\texttt{1}$ | The answer to "digit" is "1". This means that $x$ turned into the sum of its digits $1 + 2 + 3 + 4 = 10$, and $x$ changes to $10$ after the command. |
| $\texttt{div 2}$ | $\texttt{1}$ | The answer to "div 2" is "1". This means that the division command was successful as $y = 2$ is a divisor of $x = 10$, and $x$ changes to $\frac{x}{y} = \frac{10}{2} = 5$ after the command. |
| $\texttt{!}$ | $\texttt{1}$ | The answer to "!" is "1". This means you have determined that $x$ equals $n$. |

Note that the empty lines in the example input and output are for the sake of clarity, and do not occur in the real interaction.

Non-adaptive interactor (C++):









#include "testlib.h"
#include <bits/stdc++.h>

using namespace std;



long long max_queries_used = 0;

const int QUERY_LIMIT = 7;




void log_metrics() {
    try {
        tout << "queries=" << max_queries_used << endl;
        tout << "query_limit=" << QUERY_LIMIT << endl;
    } catch (...) {
        
    }
}



void finish(TResult verdict, const string& msg) {
    log_metrics();
    quitf(verdict, "%s", msg.c_str());
}


long long sum_digits(long long val) {
    long long s = 0;
    while (val > 0) {
        s += val % 10;
        val /= 10;
    }
    return s;
}

int main(int argc, char* argv[]) {
    
    
    registerInteraction(argc, argv);
    
    
    atexit(log_metrics);

    
    
    int t = inf.readInt();
    
    
    cout << t << endl;

    
    log_metrics();

    for (int i = 0; i < t; i++) {
        long long n = inf.readLong(); 
        long long x = inf.readLong(); 
        
        
        cout << n << endl;
        
        int current_queries = 0;
        bool case_active = true;

        while (case_active) {
            string token;
            
            
            if (!(cin >> token)) {
                finish(_wa, "Unexpected EOF reading command");
            }

            
            if (token == "?") {
                if (!(cin >> token)) {
                    finish(_wa, "Unexpected EOF reading command after '?'");
                }
            }

            
            if (token == "!") {
                
                if (x == n) {
                    cout << "1" << endl;
                    case_active = false; 
                } else {
                    cout << "-1" << endl;
                    finish(_wa, format("Wrong guess: x (%lld) != n (%lld)", x, n));
                }
                continue; 
            }

            
            current_queries++;
            
            
            if (current_queries > max_queries_used) {
                max_queries_used = current_queries;
                
                log_metrics();
            }

            
            if (current_queries > QUERY_LIMIT) {
                cout << "-1" << endl;
                finish(_wa, "Query limit exceeded (> 7)");
            }

            
            bool success = false;
            bool error = false;

            if (token == "add") {
                long long y;
                if (!(cin >> y)) error = true;
                else {
                    
                    if (y < -1000000000000000000LL || y > 1000000000000000000LL) {
                        error = true;
                    } else {
                        
                        long long res = x + y;
                        if (res >= 1 && res <= 1000000000000000000LL) {
                            x = res;
                            success = true;
                        }
                    }
                }
            } 
            else if (token == "mul") {
                long long y;
                if (!(cin >> y)) error = true;
                else {
                    
                    if (y < 1 || y > 1000000000000000000LL) {
                        error = true;
                    } else {
                        
                        __int128 res = (__int128)x * y;
                        if (res >= 1 && res <= 1000000000000000000LL) {
                            x = (long long)res;
                            success = true;
                        }
                    }
                }
            } 
            else if (token == "div") {
                long long y;
                if (!(cin >> y)) error = true;
                else {
                    
                    if (y < 1 || y > 1000000000000000000LL) {
                        error = true;
                    } else {
                        
                        if (x % y == 0) {
                            x /= y;
                            success = true;
                        }
                    }
                }
            } 
            else if (token == "digit") {
                
                x = sum_digits(x);
                success = true; 
            } 
            else {
                
                error = true;
            }

            
            if (error) {
                cout << "-1" << endl;
                
                finish(_wa, "Invalid command or argument: " + token);
            } else {
                cout << (success ? "1" : "0") << endl;
            }
        }
    }

    finish(_ok, "All test cases passed");
}


Adaptive interactor (C++):

Test generator (C++):
#include "testlib.h"
#include <bits/stdc++.h>

using namespace std;

int main(int argc, char* argv[]) {
    registerGen(argc, argv, 1);

    
    string mode = opt<string>("mode", "non");
    int t = opt<int>("t", 100);

    
    long long seed_val = 0;
    if (argc > 1) {
        try {
            seed_val = stoll(string(argv[1]));
        } catch (...) {
            seed_val = 0;
        }
    }

    struct Case {
        long long n, x;
    };
    vector<Case> cases;

    auto add = [&](long long n, long long x) {
        cases.push_back({n, x});
    };

    
    if (seed_val == 1) {
        
        add(1, 1);
        add(1000000000, 1000000000);
        add(1, 1000000000);
        add(1000000000, 1);
        add(500, 500);
        add(12345, 12345);
    } else if (seed_val == 2) {
        
        add(1, 999999999);
        add(100, 999999999); 
        add(1000000000, 999999999);
        add(999999999, 1);
        add(10, 199999999);
    } else if (seed_val == 3) {
        
        add(100, 10);
        add(10, 100);
        add(1024, 2);
        add(2, 1024);
        for(long long p = 10; p <= 1000000000; p *= 10) {
            add(p, 1);
            add(1, p);
        }
    }

    
    if ((int)cases.size() > t) {
        shuffle(cases.begin(), cases.end());
        cases.resize(t);
    }

    
    while ((int)cases.size() < t) {
        int type = rnd.next(1, 6);
        long long n = 1, x = 1;

        if (type == 1) { 
            n = rnd.next(1LL, 1000000000LL);
            x = rnd.next(1LL, 1000000000LL);
        } else if (type == 2) { 
            n = rnd.next(1LL, 1000LL);
            x = rnd.next(1LL, 1000000000LL);
        } else if (type == 3) { 
            n = rnd.next(1LL, 1000000000LL);
            x = rnd.next(1LL, 1000LL);
        } else if (type == 4) { 
            n = rnd.next(1LL, 1000000000LL);
            long long delta = rnd.next(-5000LL, 5000LL);
            x = n + delta;
            if (x < 1) x = 1;
            if (x > 1000000000LL) x = 1000000000LL;
        } else if (type == 5) { 
            n = rnd.next(1LL, 1000000000LL);
            int len = rnd.next(1, 9);
            x = 0;
            for(int k=0; k<len; ++k) x = x * 10 + 9;
            if (x < 1) x = 1;
        } else { 
            long long base = rnd.next(1LL, 10000LL);
            long long k = rnd.next(1LL, 10000LL);
            if (rnd.next(0, 1)) {
                n = base;
                x = min(1000000000LL, base * k);
            } else {
                x = base;
                n = min(1000000000LL, base * k);
            }
        }
        add(n, x);
    }

    
    shuffle(cases.begin(), cases.end());

    
    println(t);
    for (const auto& c : cases) {
        if (mode == "adp") {
            
            println(c.n);
        } else {
            
            println(c.n, c.x);
        }
    }

    return 0;
}