1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include <iostream>
#include<sstream>
#include<fstream>
#include<algorithm>
#include <vector>
#include <stdexcept>
#include <queue>
#include <utility>
#include "cielib.h"
#define ll long long
using namespace std;

using namespace std;


int search1D(int a, int b, int p[]) {
    if(a==b)return a;
    p[0] = a;
    czyCieplo(p);
    p[0] = b;
    int cc = czyCieplo(p);
    if(cc) {
        a = (a + b)/2 + 1;
    } else {
        p[0]=a;
        cc = czyCieplo(p);
        if(cc) {
            bool ev = (a+b)%2==0;
            b = (a+b)/2;
            if(ev)b--;
        } else {
            a = b = (a+b)/2;
        }
    }
    return search1D(a,b,p);
}

int * buff;

int* middle(vector<pair<int,int>> &G) {
    for(int i=0;i<G.size();i++){
        int x = (G[i].first + G[i].second)/2;
        buff[i]=x;
    }
    return buff;
}

int k=0;

int myCieplo(int p[]) {
    k++;
    return czyCieplo(&p[0]);
}


void doit() {
    k =0;
    int d = podajD();
    int r = podajR();
    vector<pair<int,int>> R(d, {0,r});
    bool done = false;
    buff = new int[d];
    while(!done) {
        done = true;
        bool moved = false;
        for(int di=0;di<d;di++) {
            int a = R[di].first; int b = R[di].second;
            if(a==b)continue;
            int* p = middle(R);
            p[di]=a;
            myCieplo(p);
            p[di]=b;
            int cc = myCieplo(p);
            if(cc) {
                a = (a+b)/2 +1;
                moved = true;
            } else {
                p[di]=a;
                cc = myCieplo(p);
                if(cc){
                    bool ev = (a+b)%2==0;
                    b = (a+b)/2;
                    if(ev)b--;
                    moved = true;
                } else {
                    if((b-a)%2==0) {
                        int c = (a+b)/2;
                        p[di]=c;
                        if(myCieplo(p)){
                            a= b= c;
                            moved = true;
                        }
                    }
                    if(b==a+1 && a>0) {

                            p[di]=a-1;
                            czyCieplo(p);
                            p[di]=a;
                            cc = czyCieplo(p);
                            if(cc) {
                                a=b;
                                moved = true;
                            }
                    }
                    if(b==a+1 && b<r) {
                            p[di]=b+1;
                            czyCieplo(p);
                            p[di]=b;
                            cc = czyCieplo(p);
                            if(cc){
                                b = a;
                                moved = true;
                            }

                    }
                }
            }
            R[di].first = a;R[di].second = b;
            if(a!=b)done = false;
        }

        if(!moved &&!done) {
            //cout << "STUCK\n";
           // for(int i=0;i<d;i++)cout <<"["<<R[i].first<<","<<R[i].second<<"]\n";
            break;
        }

    }
    if(done) {
          //  cout <<"finished after "<<k<<" moves\n";
            znalazlem(middle(R));
    }
}

int main()
{
    /*srand(0);
    libd = 100;
    libr = 1000000000;
    for(int t=0;t<10000;t++) {
        init_lib(500,1000000000);
        doit();
    }*/
    doit();
    return 0;
}