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
#include <iostream>
#include <vector>
#include <cstdlib>
#include "cielib.h"

#define REP(i,n) for(int i=0; i<n; i++)
#define FOR(i,b,e) for(int i=b; i<=e; i++)
#define FORD(i,b,e) for(int i=b; i>=e; i--)
#define PB push_back
using namespace std;

bool tryAgain[600];

int abs(int x){
	if(x < 0) return -x;
	return x;
}

int d, r, p[600], dir[600], eq[600];

void recFind(int x, int a, int b, int e){
	if(b==e) {
		p[x] = b;
		if(a < 0) p[x]--;
		return;
	}

	int m = (b+e)/2;
	p[x] = m;
	czyCieplo(p);
	p[x] = m+a;
	if(czyCieplo(p)){
		if(a==1) recFind(x,a,m+a,e);
		else recFind(x,a,b,m);
	}
	else {
		if(a==1) recFind(x,a,b,m);
		else recFind(x,a,m+1,e);
	}
}

void find(int x){
	p[x] = 0;
	czyCieplo(p);
	p[x] = 1;
	if( czyCieplo(p) ){
		recFind(x, 1, 0, r);
		dir[x] = 1;
	}
	else{
		dir[x] = -1;
		p[x] = r;
		czyCieplo(p);
		p[x] = r-1;
		if(czyCieplo(p)) recFind(x, -1, 0, r);
		else {
			p[x] = r/2;
			tryAgain[x] = true;
		}
	}	
}

void finalBinsearch(int b, int e){
	if(b==e){
		REP(i,d) p[i] = eq[i] + b * dir[i];
		znalazlem(p);
		return;
	}

	int m = (b+e)/2;
	REP(i,d) p[i] = eq[i] + m*dir[i];
	czyCieplo(p);
	REP(i,d) p[i] += dir[i];
	if(czyCieplo(p)) finalBinsearch(m+1,e);
	else finalBinsearch(b,m);
}

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

	REP(i,d) p[i] = r/2;

	REP(i,d) find(i);
	bool again = false;
	REP(i,d){
		if(tryAgain[i]) again = true;
	}
	if(again) REP(i,d) p[i] += dir[i];
	REP(i,d) if(tryAgain[i]) find(i);

	int m = 1000000009;
	REP(i,d){
		eq[i] = p[i];
		if(dir[i] == 1) m = min(m, r-p[i]);
		else m = min(m, p[i]);
	}
	if(m==0) znalazlem(p);
	else{
		finalBinsearch(0,m);
	}
}