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 Python (3.8.2) (judge verdict: Time Limit Exceeded)

# abc162_c.py
# https://atcoder.jp/contests/abc162/tasks/abc162_c

# C - Sum of gcd of Tuples (Easy) /
# 実行時間制限: 2 sec / メモリ制限: 1024 MB
# 配点 : 300点

# 問題文
# K∑a=1K∑b=1K∑c=1gcd(a,b,c)を求めてください。
# ただし、gcd(a,b,c)は a,b,cの最大公約数を表します。

# 制約
#     1≤K≤200
#     Kは整数

# 入力
# 入力は以下の形式で標準入力から与えられる。
# K

# 出力
# K∑a=1K∑b=1K∑c=1gcd(a,b,c)の値を出力せよ。

# 入力例 1
# 2

# 出力例 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
# となるため、答えは 9です。

# 入力例 2
# 200

# 出力例 2
# 10813692


global FLAG_LOG
FLAG_LOG = False


def log(value):
    # FLAG_LOG = True
    # FLAG_LOG = False
    if FLAG_LOG:
        print(str(value))


def calculation(lines):
    # S = lines[0]
    K = int(lines[0])
    # N, M = list(map(int, lines[0].split()))
    # values = list(map(int, lines[1].split()))
    # values = list(map(int, lines[2].split()))
    # values = list()
    # for i in range(N):
    #     values.append(int(lines[i]))
    # valueses = list()
    # for i in range(N):
    #     valueses.append(list(map(int, lines[i+1].split())))

    import math

    dic = dict()
    for x in range(1, K):
        for y in range(x+1, K+1):
            dic[(x, y)] = math.gcd(x, y)

    su = 0
    for a in range(1, K+1):
        for b in range(1, K+1):
            for c in range(1, K+1):
                # ☆これだと5秒かかる
                # su += get_gcd(a, b, c)
                tmp = [a, b, c]
                tmp.sort()
                x, y, z = tmp
                # けど...速くしきらんかった...
                su += get_gcd_by_dic(dic, x, y, z)

    return [su]


# def get_gcd(a, b, c):
#     import math
#     return math.gcd(math.gcd(a, b), math.gcd(a, c))


def get_gcd_by_dic(dic, a, b, c):
    if a == b:
        x = a
    else:
        x = dic[(a, b)]
    if a == c:
        y = a
    else:
        y = dic[(a, c)]
    if x == y:
        ret = x
    elif x > y:
        ret = dic[(y, x)]
    else:
        ret = dic[(x, y)]
    return ret


# 引数を取得
def get_input_lines(lines_count):
    lines = list()
    for _ in range(lines_count):
        lines.append(input())
    return lines


# テストデータ
def get_testdata(pattern):
    if pattern == 1:
        lines_input = ['2']
        lines_export = [9]
    if pattern == 2:
        lines_input = ['200']
        lines_export = [10813692]
    return lines_input, lines_export


# 動作モード判別
def get_mode():
    import sys
    args = sys.argv
    global FLAG_LOG
    if len(args) == 1:
        mode = 0
        FLAG_LOG = False
    else:
        mode = int(args[1])
        FLAG_LOG = True
    return mode


# 主処理
def main():
    import time
    started = time.time()
    mode = get_mode()
    if mode == 0:
        lines_input = get_input_lines(1)
    else:
        lines_input, lines_export = get_testdata(mode)

    lines_result = calculation(lines_input)

    for line_result in lines_result:
        print(line_result)

    # if mode > 0:
    #     print(f'lines_input=[{lines_input}]')
    #     print(f'lines_export=[{lines_export}]')
    #     print(f'lines_result=[{lines_result}]')
    #     if lines_result == lines_export:
    #         print('OK')
    #     else:
    #         print('NG')
    # finished = time.time()
    # duration = finished - started
    # print(f'duration=[{duration}]')


# 起動処理
if __name__ == '__main__':
    main()