Problem:
Score : 300 points

Problem Statement

Find \displaystyle{\sum_{a=1}^{K}\sum_{b=1}^{K}\sum_{c=1}^{K} \gcd(a,b,c)}.

Here \gcd(a,b,c) denotes the greatest common divisor of a, b, and c.

Constraints

1 \leq K \leq 200

K is an integer.

Input

Input is given from Standard Input in the following format:

K

Output

Print the value of \displaystyle{\sum_{a=1}^{K}\sum_{b=1}^{K}\sum_{c=1}^{K} \gcd(a,b,c)}.

Sample Input 1

2

Sample Output 1

9

\gcd(1,1,1)+\gcd(1,1,2)+\gcd(1,2,1)+\gcd(1,2,2)
+\gcd(2,1,1)+\gcd(2,1,2)+\gcd(2,2,1)+\gcd(2,2,2)
=1+1+1+1+1+1+1+2=9

Thus, the answer is 9.

Sample Input 2

200

Sample Output 2

10813692

Solution in Kotlin (1.3.71) (judge verdict: Accepted)

fun main() = ABC162C.main()

object ABC162C {
    fun log(message: Any?) {
        //println(message)// TODO: ←消す
    }

    fun main() {
        val K = readInt()
        var ans = 0
        (1..K).forEach { a ->
            (1..K).forEach { b ->
                (1..K).forEach { c ->
                    ans += Gcd.get(a, Gcd.get(b, c))
                }
            }
        }

        println(ans)
        log("calcCount=${Gcd.calcCount}")
    }


    /*####################################################################################*/
    object Gcd {
        var calcCount = 0
            private set
        private val cache = mutableMapOf<Pair<Int, Int>, Int>()

        fun get(a: Int, b: Int): Int {
            val (m, n) = if (a > b) listOf(a, b) else listOf(b, a)
            val cached = cache[m to n]
            return if (cached == null) {
                val gcd = calc(m, n)
                cache[m to n] = gcd
                gcd
            } else cached
        }

        private fun calc(m: Int, n: Int): Int { // m > n
            assert(m > n)
            var m = m
            var n = n
            while (n != 0) {
                val r = m % n
                m = n
                n = r
            }
            calcCount++
            return m
        }
    }

    fun readInt() = readLine()!!.toInt()
    fun readInts() = readLine()!!.split(" ").map { it.toInt() }
}