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
import java.util.Arrays;

public class cie {
    int d;
    int r;
    int[] dir;

    cie() {
        compute();
    }

    private void compute() {
        d = cielib.podajD();
        r = cielib.podajR();
        int[] pos = new int[d];
        dir = new int[d];
        Arrays.fill(dir, 1);
        add(pos,1);
        Arrays.fill(dir, -1);
        boolean isZero = isCloser(pos);
        pos = new int[d];
        if(isZero) {
            cielib.znalazlem(pos);
            return;
        }

        boolean[] fixed = new boolean[d];
        int cnt = d;
        boolean found = true;
        Arrays.fill(dir, 0);
        while(found) {
            int[] av = new int[cnt];
            int j = 0;
            for(int i=0;i<d;i++) {
                if(!fixed[i]) {
                    av[j] = i;
                    j++;
                }
            }
            found = false;
            outer: for(int u=1;u<=cnt;u++) {
                int[] y = new int[u];
                for(int c=0;c<u;c++) {
                    y[c] = c;
                }
                boolean kon = false;
                while (!kon) {
                    int[] z = new int[u];
                    boolean k2 = false;
                    while(!k2) {
                        for (int i = 0; i < u; i++) {
                            dir[av[y[i]]] = z[i]*2-1;
                        }
                        if (isCloser(pos)) {
                            for (int i = 0; i < u; i++) {
                                fixed[av[y[i]]] = true;
                                dir[av[y[i]]] = z[i]*2-1;
                            }
                            cnt -= u;
                            found = true;
                            break outer;
                        }
                        int p = 0;
                        z[p]++;
                        while(p < u && z[p] == 2) {
                            z[p] = 0;
                            p++;
                            if(p < u) {
                                z[p]++;
                            } else {
                                k2 = true;
                            }
                        }
                    }

                    for (int i = 0; i < u; i++) {
                        dir[av[y[i]]] = 0;
                    }
                    int p = u-1;
                    if(y[p] < cnt-1) {
                        y[p]++;
                    } else {
                        while (p > 0 && y[p - 1] + 1 == y[p]) {
                            p--;
                        }
                        if(p > 0) {
                            p--;
                        }
                        y[p]++;
                        p++;
                        while (p < u) {
                            y[p] = y[p - 1] + 1;
                            p++;
                        }
                        if (y[u - 1] >= cnt) {
                            kon = true;
                        }
                    }
                }
            }
            binarySearch(pos, 0, r);
        }
        cielib.znalazlem(pos);
    }

    private void binarySearch(int[] pos, int from, int to) {
        int baseFrom = from;
        int[] base = Arrays.copyOf(pos, d);
        while(to - from >= 2) {
            int mid = (from + to) / 2;
            System.arraycopy(base,0,pos,0,d);
            add(pos, (mid-baseFrom));
            if (isCloser(pos)) {
                from = mid+1;
            } else {
                to = mid;
            }
        }
        for(int k=-1;k<=3;k++) {
            int g = from + k;
            if(g >= from && g <= to) {
                System.arraycopy(base,0,pos,0,d);
                add(pos, g-baseFrom);
                if(g < to) {
                    if (!isCloser(pos)) {
                        return;
                    }
                } else {
                    break;
                }
            }
        }
    }

    private boolean isCloser(int[] pos) {
        int[] after = new int[d];
        System.arraycopy(pos,0,after,0,d);
        add(after, 1);
        for(int i=0;i<d;i++) {
            if(after[i] < 0 || after[i] > r) {
                return false;
            }
        }
        cielib.czyCieplo(pos);
        return cielib.czyCieplo(after) == 1;
    }

    private void add(int[] pos, int diff) {
        for(int j=0;j<d;j++) {
            pos[j] += dir[j]*diff;
        }
    }

    public static void main(String[] args) {
        new cie();
    }
}