Problem: icpc2023_asia_jakarta_onsite_regional_c
Difficulty: medium
Categories: bit, data structures
Time limit: 2000 ms, memory limit: 256 MB

You found an antique box in your warehouse and decided to open it. The moment you open the box, it traps you into a cursed game where you are playing against a demon. The game consists of 333 rounds and you have to win all rounds in order to escape. The demon also gives you 999 coins that you can use throughout all rounds.

Note that in this problem, denote cell $( r , c )$ of a grid as the cell in row $r$ and column $c$ of the grid.

Before the start of each round, the demon will prepare a secret paper, which can be represented as a grid with 3 rows and 3 columns, both numbered from 1 to 3. The demon will secretly put a hole in one or more cells, and you are not aware which cells have a hole in it. Then, the round starts with the demon giving you an odd integer $N$ $3 \leq N \leq 3 3$ ).

Within each round, you can ask the demon several queries, each costing you one coin. For each query, you are allowed to give the demon your paper, which can be represented as a grid with $N$ rows and $N$ columns, both numbered from 1 to $N$ . Each cell is coloured either black or white by you.

For each of your query, the demon will calculate a binary result grid with $N - 2$ rows and $N - 2$ columns, both numbered from 1 to $N - 2$ . The value on cell $( r , c )$ of the result grid is filled as follows.

• The demon will put the secret paper on top of your paper such that the cell $( r + i - 1 , c + j - 1 )$ of your paper aligns with the cell $( i , j )$ of the secret paper, for $1 \leq i , j \leq 3$ .   
• The demon can only see the colour of the cell in your paper if the corresponding cell in the secret paper has a hole in it.   
• The value on cell $( r , c )$ of the result grid is 1 if there is an odd number of black cells that it can see through the holes, or 0 otherwise.

You win the round if the result grid consists of only the values 1. Otherwise, the demon will give you the result grid as a feedback, and the round continues.

If you have spent all the coins and still did not win all the rounds, then you will be trapped forever. Escape the cursed game!

# Interaction

Each round is started with an odd integer $N$ $3 \leq N \leq 3 3$ ) which can be read through standard input.

Then, for each query that you give to the demon, you can output $N$ lines to standard output. Each of the $N$ lines consists of $N$ characters. The $c ^ { \mathrm { t h } }$ character of the $r ^ { \mathrm { { t h } } }$ line represents the colour of cell $( r , c )$ in your paper. The character should be 1 if $( r , c )$ is coloured black, or 0 if coloured white.

The demon will reply with one line consisting of a string, which can be read through standard input.

• If the string is CORRECT, then you win the current round and the next round (if exists) will immediately start.   
• If the string is INCORRECT, then the demon will give you $N - 2$ more lines which can be read through standard input. Each of the $N - 2$ lines consists of $N - 2$ characters, representing the binary result grid as explained in the description.

The demon prepares the secret paper before the start of each round. In other words, the grader is NOT adaptive. There will be at least one hole in the secret paper.

The total number of queries across all 333 rounds should not exceed 999. If you exceed the maximum number of queries, you should terminate your program with 0 to receive the verdict Wrong Answer. If you do not terminate, the judging result would be undefined because your program is reading from a closed stream.

Do not forget to flush output buffers after each output. In C you can use fflush(stdout). In $\complement + +$ you can use fflush(stdout) or cout  flush. In Java you can use method flush for output stream, for example, System.out.flush(). In Python you can use stdout.flush().

# Sample Interaction #1

The following interaction only shows 2 rounds. The actual interaction stays until you win all 333 rounds or you run out of coins.

<table><tr><td rowspan=1 colspan=1>Standard Input</td><td rowspan=1 colspan=1>Standard Output</td></tr><tr><td rowspan=1 colspan=1>3</td><td rowspan=1 colspan=1></td></tr><tr><td rowspan=1 colspan=1></td><td rowspan=1 colspan=1>111000111</td></tr><tr><td rowspan=1 colspan=1>INCORRECT0</td><td rowspan=1 colspan=1></td></tr><tr><td rowspan=1 colspan=1></td><td rowspan=1 colspan=1>111111111</td></tr><tr><td rowspan=1 colspan=1>CORRECT</td><td rowspan=1 colspan=1></td></tr><tr><td rowspan=1 colspan=1>5</td><td rowspan=1 colspan=1></td></tr><tr><td rowspan=1 colspan=1></td><td rowspan=1 colspan=1>1001110011000001101011011</td></tr><tr><td rowspan=1 colspan=1>INCORRECT111001111</td><td rowspan=1 colspan=1></td></tr><tr><td rowspan=1 colspan=1></td><td rowspan=1 colspan=1>1101111011001001101111011</td></tr><tr><td rowspan=1 colspan=1>CORRECT</td><td rowspan=1 colspan=1></td></tr><tr><td rowspan=1 colspan=1>(start of round 3, con-tinue until 333 rounds)</td><td rowspan=1 colspan=1></td></tr></table>

![](images/c9281622a05b4a8ffdd68f3cca9b07b5c787e5e65db68caccb8b173d3d2caef9.jpg)

# Explanation for the sample interaction #1

For the first round, the following illustration shows how the demon finds the value in cell $( 1 , 1 )$ of the result grid for the first and second queries. The greycolored square represents the secret paper, and the circles represent the holes. In the first query, there are 4 black cells that can be seen through the holes, therefore the value in cell $( 1 , 1 )$ of the result grid is 0. In the second query, there are 5 black cells that can be seen through the holes, therefore the value in cell $( 1 , 1 )$ of the result grid is 1. As the result grid consists of only 1, the first round ends.

![](images/bbf692d89f507f0f9c44a4e7a04fe0d9b6163d9a8bf7094dfcddc6180217bd52.jpg)

For the second round, the following illustration shows how the demon find the value in cell $( 2 , 1 )$ of the result grid for the first query. Since there are 2 black cells that can be seen through the holes, the value in cell $( 2 , 1 )$ is 0.

Non-adaptive interactor (C++):
#include "testlib.h"
#include <bits/stdc++.h>

using namespace std;


static long long queries = 0;
static long long query_limit = 999;
static const long long HARD_CAP = 20000; 


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


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

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

    
    int t = inf.readInt();

    
    
    int secret[40][40];
    int contestant[40][40];

    for (int round_idx = 0; round_idx < t; round_idx++) {
        
        int n = inf.readInt();

        
        
        for (int i = 0; i < 3; i++) {
            string row_s = inf.readToken();
            if ((int)row_s.size() != 3) {
                finish(_fail, "Invalid secret mask format in input file");
            }
            for (int j = 0; j < 3; j++) {
                if (row_s[j] == '1') secret[i][j] = 1;
                else secret[i][j] = 0;
            }
        }

        
        cout << n << endl;

        
        while (true) {
            queries++;
            
            log_metrics();

            
            if (queries > HARD_CAP) {
                finish(_pe, "Hard query limit exceeded");
            }

            
            
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < n; j++) {
                    char c;
                    
                    if (!(cin >> c)) {
                        finish(_wa, "Unexpected EOF (contestant output too short or stream closed)");
                    }
                    if (c == '0') contestant[i][j] = 0;
                    else if (c == '1') contestant[i][j] = 1;
                    else {
                        string msg = "Invalid character in grid output: '";
                        msg += c;
                        msg += "'";
                        finish(_wa, msg);
                    }
                }
            }

            
            
            bool win = true;
            vector<string> result_rows;
            int res_dim = n - 2;

            for (int r = 0; r < res_dim; r++) {
                string row_out = "";
                for (int c = 0; c < res_dim; c++) {
                    
                    
                    int bits = 0;
                    for (int si = 0; si < 3; si++) {
                        for (int sj = 0; sj < 3; sj++) {
                            
                            if (contestant[r + si][c + sj] == 1 && secret[si][sj] == 1) {
                                bits++;
                            }
                        }
                    }
                    
                    if (bits % 2 != 0) {
                        row_out += '1';
                    } else {
                        row_out += '0';
                        win = false; 
                    }
                }
                result_rows.push_back(row_out);
            }

            
            if (win) {
                cout << "CORRECT" << endl;
                break; 
            } else {
                cout << "INCORRECT" << endl;
                for (const string& s : result_rows) {
                    cout << s << endl;
                }
            }
        }
    }

    finish(_ok, "All rounds solved");
    return 0;
}


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");

    
    
    string seed_str = (argc > 1 ? string(argv[1]) : "0");
    int strategy = 0;
    if (seed_str == "1") strategy = 1;      
    else if (seed_str == "2") strategy = 2; 
    else if (seed_str == "3") strategy = 3; 
    else if (seed_str == "4") strategy = 4; 
    else if (seed_str == "5") strategy = 5; 
    

    int t = 333; 
    cout << t << "\n";

    for (int i = 0; i < t; i++) {
        int n;
        
        
        if (strategy == 1) {
            n = 3;
        } else if (strategy == 2) {
            n = 33;
        } else if (strategy == 3) {
            n = 3 + (i % 16) * 2; 
        } else {
            
            n = 3 + rnd.next(16) * 2;
        }

        if (mode == "non") {
            cout << n << "\n";

            
            
            int mask;
            if (strategy == 4) {
                
                mask = (1 << rnd.next(9));
            } else if (strategy == 5) {
                
                if (rnd.next(5) == 0) mask = 511; 
                else mask = 511 ^ (1 << rnd.next(9));
            } else {
                
                mask = rnd.next(1, 511);
            }

            
            for (int r = 0; r < 3; r++) {
                for (int c = 0; c < 3; c++) {
                    if ((mask >> (r * 3 + c)) & 1) cout << '1';
                    else cout << '0';
                }
                cout << "\n";
            }
        } else {
            
            cout << n << "\n";
        }
    }

    return 0;
}