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
#include <algorithm>
#include "cielib.h"

#define NDEBUG
#include <cassert>

using namespace std;
#define span_n(x, n) x, x+n

int R, D;
const int dLim = 501;
int Guess[dLim];
int AddR[dLim];
int FavorableRs[30] = { 2 };

void Init();
void AdjustToFavorableR();
void ReduceR();
void Reduce2();
void ReduceUntil2();
void Finalize();

int main()
{
	Init();
	AdjustToFavorableR();
	ReduceUntil2();
	Reduce2();
	Finalize();
    return 0;
}

void Init()
{
	R = podajR();
	D = podajD();
	
	for(int i = 1; i < 30; ++i)
		FavorableRs[i] = 2*FavorableRs[i-1];
}

void AdjustToFavorableR()
{
	int favorableR = *lower_bound(span_n(FavorableRs, 30), R);
	if(R == favorableR)
		return;
	int previousR = R;
	ReduceR();
	assert(R <= favorableR);
	for(int d = 0; d < D; ++d)
		AddR[d] = min(AddR[d], previousR-favorableR);
	R = favorableR;
}

void ReduceR()
{
	int center = R/2;
	for(int d = 0; d < D; ++d)
		Guess[d] = AddR[d] + center;
	for(int d = 0; d < D; ++d)
	{
		Guess[d] = AddR[d];
		czyCieplo(Guess);
		Guess[d] = AddR[d] + R;
		bool shouldAdd = czyCieplo(Guess);
		Guess[d] = AddR[d] + center;
		if(shouldAdd)
			AddR[d] += center;
	}
	R = center;
}

void Reduce2()
{
	assert(R == 2);
	for(int d = 0; d < D; ++d)
		Guess[d] = AddR[d] + 1;
	for(int d = 0; d < D; ++d)
	{
		Guess[d] = AddR[d];
		czyCieplo(Guess);
		
		Guess[d] = AddR[d] + 2;
		if(czyCieplo(Guess))
			AddR[d] += 2;
		else
		{
			Guess[d] = AddR[d];
			if(!czyCieplo(Guess))
				AddR[d] += 1;
		}
		Guess[d] = AddR[d];
	}
}

void ReduceUntil2()
{
	while(R > 2)
		ReduceR();
}

void Finalize()
{
	znalazlem(Guess);
}