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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#include "cielib.h"
#include<cstdio>
#include<cstdlib>
int d = 0;
int r = 0;
int k = 0;


void print(int t[]) {
	for (int i = 0; i < d; i++) {
		printf("%d ", t[i]);
	}
	printf("\n");
}

int sng(int n) {
	return n >= 0 ? 1 : -1;
}

bool inRange(int n) {
	return n >= 0 && n <= r;
}

int minStep() {
	int step = 1 << 30;
	while (step > r) {
		step >>= 1;
	}
	return step;
}

void solve1() {
	int step = minStep();
	int t[1];
	t[0] = step;
	while (step > 0) {
		//printf("%d %d\n", step, t[0]);
		czyCieplo(t);
		int prev = t[0];
		int next = (prev + step);
		if (inRange(next)) {
			t[0] = next;
			if (czyCieplo(t)) {
				step /= 2;
				continue;
			}
			t[0] = prev;
		}
		next = (prev - step);
		if (inRange(next)) {
			t[0] = next;
			if (czyCieplo(t)) {
				step /= 2;
				continue;
			}
			t[0] = prev;
		}
		step /= 2;
	}
	znalazlem(t);
}

int absDiff(int a, int b) {
	return abs(a-b);
}

void findCorner(int t[], int better, int worse, int idx, int dir[]) {
	//printf("find corner at index %d better=%d worse=%d\n", idx, better, worse);
	//print(dir);
	int current = t[idx];//last value with 0
	int step = minStep();
	while(step > 0) {
		int next = current - dir[idx] * step;
		if (inRange(next)) {
			t[idx] = next;
			czyCieplo(t);
			t[idx] = current;
			if(!czyCieplo(t)) {
				current = next;
			}
		}
		step /= 2;
	}
	//print(t);
	t[idx] = current;
}

void findCorner(int t[], int dir[], int idx) {
	int low = 0;
	int high = r;
	int prev = t[idx];
	t[idx] = low;
	czyCieplo(t);
	t[idx] = prev;
	if (czyCieplo(t)) {// corner in (0, prev]
		dir[idx] = 1;
		t[idx] = prev;
		findCorner(t, prev, 0, idx, dir);
	} else {
		t[idx] = high;
		czyCieplo(t);
		t[idx] = prev;
		if (czyCieplo(t)) {//corner in [prev, r)
			dir[idx] = -1;
			t[idx] = prev;
			findCorner(t, prev, r, idx, dir);
		} else {// corner in 0 or in r
			if (r % 2 == 0) {//corner in 0 and in r
				t[idx] = 0;
				dir[idx] = 1;
			} else {//r is further from r/2 than 0 so r is corner
				t[idx] = r;
				dir[idx] = -1;
			}
			return;
		}
	}
}

void findCorner(int t[], int dir[]) {
	int start = r/2;
	for (int i = 0; i < d; i++) {
		t[i] = start;
	}
	
	for (int i = 0; i < d; i++) {
		findCorner(t, dir, i);
	}
	//printf("BEFORE\n");
//	print(t);
//	for (int i = 1; i < d; i++) {
//		t[i] += dir[i];//dirty hack
//	}
//	printf("AFTER\n");
	//print(t);
	
//	print(t);
//	print(dir);
}

void linearCombination(int result[], int A[], int B[], int x) { //result[i] = A[i] + x*B[i]
	for (int i = 0; i < d; i++) {
		result[i] = A[i] + x * B[i];
	}
}

bool correct(int t[]) {
	for (int i = 0; i < d; i++) {
		if (!inRange(t[i])) {
			return false;
		}
	}
	return true;
}

void findCenter(int t[], int dir[]) {
	int step = minStep();
	int prev[d];
	
	linearCombination(prev, t, t, 0);
	
	while (step > 0) {
		czyCieplo(t);
		linearCombination(prev, t, t, 0);
		linearCombination(t, prev, dir, step);
		if (correct(t)) {
			if (czyCieplo(t)) {
				step /= 2;
				continue;
			}
		}
		linearCombination(t, prev, dir, -step);
		if (correct(t)) {
			if (czyCieplo(t)) {
				step /= 2;
				continue;
			}
		}
		linearCombination(t, prev, prev, 0);
		step /= 2;
	}
}

int main() {
	d = podajD();
	r = podajR();
	k = podajK();
	
	if (d == 1) {
		solve1();
		return 0;
	}
	
	int t[d];
	int dir[d];
	
	findCorner(t, dir);
	findCenter(t, dir);

	znalazlem(t);
}