import java.lang.Math.pow import kotlin.math.* fun next() = readLine()!! fun nextInt() = next().toInt() fun nextLong() = next().toLong() fun nextDouble() = next().toDouble() fun nextList() = next().split(" ") fun nextIntList() = next().split(" ").map { it.toInt() } fun nextLongList() = next().split(" ").map { it.toLong() } fun nextDoubleList() = next().split(" ").map { it.toDouble() } val mod = 10.0.pow(8).toLong() fun main(){ val N = nextInt() val A = Array(N){ LongArray(N) } for(i in 0 until N){ val a = nextLongList() for(j in 0..i){ A[i][j] = a[j] } } val state = State(N, A, LongArray(N)) var nowState = state var (bestScore, bestScoreAnswer) = state.getScore() var nowScore = bestScore val startTemp = 50000 val endTemp = 100 for(i in 0 until 20000){ val nextState = nowState.copy() nextState.transition() val (nextScore, nextScoreAnswer) = nextState.getScore() val temp = startTemp + (endTemp - startTemp) * (i / 20000.0) val probability = exp((nextScore - nowScore) / temp) val isForceNext = probability > Math.random() if(nextScore > nowScore || isForceNext){ nowState = nextState nowScore = nextScore println("iteration: $i, score: $nowScore") } if(nextScore > bestScore){ bestScore = nextScore bestScoreAnswer = nextScoreAnswer } } println(bestScoreAnswer.joinToString(" ")) } class State(val N: Int, val A: Array, val b: LongArray){ val delta = 10.0.pow(6).toLong() fun transition(){ val i = (0 until N).random() val d = max(1, 25 - abs(25 - i)) val bb = b[i] b[i] = (bb + (delta / d)) % mod } fun getScore(): Pair { val B = Array(N){ LongArray(N) } B[N - 1] = b for(i in N - 2 downTo 0){ for(j in 0..i){ B[i][j] = (B[i + 1][j] + B[i + 1][j + 1]) % mod } } var maxE = -1L for(i in 0 until N){ for(j in 0..i){ val e = min(abs(B[i][j] - A[i][j]), (10.0.pow(8) - abs(B[i][j] - A[i][j])).toLong()) maxE = max(maxE, e) } } val score = 5 * 10.0.pow(8).toLong() - maxE return Pair(score, b) } fun copy(): State { val b = LongArray(N) for(i in 0 until N){ b[i] = this.b[i] } return State(N, A, b) } }