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
#include<cstdio>
#include<algorithm>
#include<utility>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<cmath>
#include<iostream>
#include "cielib.h"
//#include "cielib.c"

// macros

#define FORE(c, a, b) for(int c=a; c<= int(b); ++c)
#define FORD(c, a, b) for(int c=a; c>= int(b); --c)
#define FORIT(it, cont, cont_t) for(cont_t::iterator it = cont.begin(); it != cont.end(); ++it)
#define REP(i, n) for(int i = 0; i < (n); ++i)
#define ALL(a) a.begin(), a.end() 
#define PR(n) printf("%d ", (int) (n)) 
#define PRL(n) printf("%lld ", (ll) (n)) 
#define SC(n) scanf("%d", &n);
#define SC2(n, m) scanf("%d%d", &n, &m);

#define xx first
#define yy second
#define pb push_back
#define mp make_pair
#define itr iterator

#define dbg if(0)
#define prd dbg printf

using namespace std;

typedef long long ll;
typedef long double ld;
typedef unsigned int uint;

typedef vector<int> vi;
typedef set<int> si;
typedef map<int, int> mi;
typedef pair<int, int> pi;
typedef vector<pi> vii;
typedef vector<vi> vvi;

const int NMAX = 1<<20;

int d, r, k;

int check(vi const &vec1, vi const &vec2) {
  int tmp[d];
  copy(vec1.begin(), vec1.end(), tmp);
  dbg {
      prd("check1 ");
      REP(i, d) prd("%d=%d ", vec1[i], tmp[i]);
      prd("\n");
    }
  czyCieplo(tmp);
  copy(vec2.begin(), vec2.end(), tmp);
  dbg {
      prd("check2 ");
      REP(i, d) prd("%d=%d ", vec2[i], tmp[i]);
      prd("\n\n");
    }
  return czyCieplo(tmp);
}

void normalize(vi &vec) {
  REP(i, d) {
    if(vec[i] > r) vec[i] = r;
    if(vec[i] < 0) vec[i] = 0;
  }
}

int getMax(vi const &vec, vi const &grad) {
  int out = r+1;
  REP(i, d) {
    if(grad[i] == -1)
      out = min(out, vec[i]);
    if(grad[i] == 1)
      out = min(out, r - vec[i]);
  }
  return out;
}

vi add(vi const &vec1, vi const &vec2, int x) {
  vi vec3(vec1);
  REP(i, d)
    vec3[i] += vec2[i] * x;
  return vec3;
}

int main() {
  d = podajD(); r = podajR(); k = podajK();
  vi pos(d, r/2);
  
  while(1) {
    dbg {
      REP(i, d) prd("%d ", pos[i]);
      prd("\n");
    }
    vi grad(d, 0);
    
    REP(i, d) {
      vi pos2(pos);
      pos2[i] += 1;
      normalize(pos2);
      if(pos[i] == r || check(pos2, pos)) // czy pogarszamy tym ruchem
        grad[i] = -1;
        
      pos2 = pos;
      pos2[i] -= 1;
      normalize(pos2);
      if(pos[i] == 0 || check(pos2, pos))
        grad[i] += 1;
    }
    int zeros = 0;
    REP(i, d) zeros += (grad[i] == 0);
    if(zeros == d) {
      int tmp[d];
      copy(pos.begin(), pos.end(), tmp);
      znalazlem(tmp);
      //printf("DONE");
      return 0;
    }
    
    int start = 0, stop = getMax(pos, grad);
    while(start < stop) {
      int mid = (start + stop) / 2;
      vi cand = add(pos, grad, mid), cand2 = add(cand, grad, 1);
      if (check(cand, cand2))
        start = mid + 1;
      else
        stop = mid;
    }
    
    pos = add(pos, grad, start);
  }
  return 0;
}