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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#include <iostream>
#include <vector>
#include "cielib.h"

using std::vector;

int d, k, r;
vector<int> coordinates, mini, maxi;


void print(vector<int> & vec) {
    //printf("(");
    for(int dim = 0; dim < d-1; ++dim) {
        //printf("%d, ", vec[dim]);
    }
    //printf("%d)", vec[d-1]);
}

void addToAll(vector<int> & vec, int val) {
    for(auto &x: vec) {
        x += val;
    }
}

bool czyCieplo(vector<int> & vec) {
    for(auto x : vec) {
        if (x < 0 || x > r) {
            //printf("Wrong question!: ");
            print(vec);
            //printf("\n");
            std::exit(0);
        }
    }
    return (bool)(czyCieplo(&vec[0]));
}

void znalazlem(vector<int> & vec) {
    znalazlem(&vec[0]);
}



bool heatGoingFromDown(int dim, vector<int> & point) {
    point[dim]--;
    czyCieplo(point);
    point[dim]++;
    auto x = czyCieplo(point);

    return x;
}

bool heatGoingFromUp(int dim, vector<int> & point) {
    if(point[dim] == r) {
        return true;
    }
    point[dim]++;
    czyCieplo(point);
    point[dim]--;
    auto x = czyCieplo(point);

    return x;
}

void addWithMask(vector<int>& vec, vector<bool>& mask, int val) {
    for (int i = 0; i < vec.size(); ++i) {
        if (mask[i]) {
            vec[i] += val;
        }
    }
}

void isAnswer(vector<int>& vec) {
    for (int dim = 0; dim < d; ++dim) {
        if ((vec[dim] == r || heatGoingFromUp(dim, vec)) && (vec[dim] == 0 || heatGoingFromDown(dim, vec))) {

        } else {
            return;
        }
    }
//    return true;
    znalazlem(vec);
}

void iterateBox() {
    //printf("BOX! :C\n");
    //printf("from: "); print(mini); //printf(" to: "); print(maxi); //printf("\n");
	auto miniOrig = mini;
	
    while (true) {
        isAnswer(mini);
	
        mini[0]++;
        for(int dim = 0; dim < d; ++dim) {
            if (mini[dim] > maxi[dim]) {
                mini[dim] = miniOrig[dim];
                mini[dim+1]++;
            }
        }
    }
}

int main() {
    d = podajD();
    k = podajK();
    r = podajR();

    coordinates.resize(d, 1); coordinates.assign(d, 1);

    mini.resize(d, 0); mini.assign(d, 0);
    maxi.resize(d, r); maxi.assign(d, r);

    while(true) {
        vector<int> mid;
        for(int dim = 0; dim < d; ++dim) {
            mid.push_back((mini[dim] + maxi[dim])/2);
        }

        //printf("now: "); print(mini); //printf(" | "); print(maxi); //printf(" -> "); print(mid); //printf("\n");


        // 1. check if mid is ansfer
        isAnswer(mid);

        // 2. cut dimensions
        bool anyChange = false;

        for(int dim = 0; dim < d; ++dim) {
            int x;

            // first non-decreasing
            int begin = mini[dim], end = maxi[dim];
            while (begin != end) {
                int midi = (begin + end) / 2;

                mid[dim] = midi;
                czyCieplo(mid);
                mid[dim]++;

                if (czyCieplo(mid)) {
                    begin = midi + 1;
                } else {
                    end = midi;
                }
            }

            if (mini[dim] != begin) {
                anyChange = true;
                mini[dim] = begin;
            }

            // last decreasing
            begin = mini[dim], end = maxi[dim];
            while (begin != end) {
                int midi = (begin + end) / 2;

                mid[dim] = midi + 1;
                czyCieplo(mid);
                mid[dim]--;

                if (czyCieplo(mid)) {
                    end = midi;
                } else {
                    begin = midi + 1;
                }
            }

            if (maxi[dim] != begin) {
                anyChange = true;
                maxi[dim] = begin;
            }

            //printf("dim: %d : (%d...%d)\n", dim, mini[dim], maxi[dim]);
        }

        if (!anyChange) {
            iterateBox();
        }
    }

    std::cout << "Hello, World!" << std::endl;
    return 0;
}