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 JavaScript (Node.js 12.16.1) (judge verdict: Accepted)

"use strict";

// 最大公約数その2（多入力対応）
const getGcd = array => {
  let len, a, b;
	
  len = array.length;
	a   = array[0];
	
  for (let i=1; i<len; i++) {
		b = array[i];
		a = gcdTwoNumbers(a, b);
	}

	return a;
}

function gcdTwoNumbers(x, y) {
  x = Math.abs(x);
  y = Math.abs(y);
  while(y) {
    let t = y;
    y = x % y;
    x = t;
  }
  return x;
}

//　組み合わせ 被りなし
const combineWithoutRepetitions = (comboOptions, comboLength) => {
  if (comboLength === 1) {
    return comboOptions.map(comboOption => [comboOption]);
  }

  // Init combinations array.
  const combos = [];

  // Eliminate characters one by one and concatenate them to
  // combinations of smaller lengths.
  comboOptions.forEach((currentOption, optionIndex) => {
    const smallerCombos = combineWithoutRepetitions(
      comboOptions.slice(optionIndex + 1),
      comboLength - 1
    );

    smallerCombos.forEach((smallerCombo) => {
      combos.push([currentOption].concat(smallerCombo));
    });
  });

  return combos;
}

const main = arg => {
    arg = arg.trim().split("\n");
    const K = parseInt(arg[0].split(" ")[0]);
    
    let answer = 0;
    let A = [];
    
    for(let i=1; i<=K; i++) {
        A.push(i);
    }
    
    // 1種類の時
    for(let i=1; i<=K; i++) {
        answer += i;
    }
    
    // console.log("answer:" + answer)
    
    // 2種類の時
    const combos2 = combineWithoutRepetitions(A, 2);
    const N2 = combos2.length;
    
    for(let i=0; i<N2; i++) {
        answer += gcdTwoNumbers(combos2[i][0], combos2[i][1]) * 6;
    }
    
    // console.log("answer:" + answer)
    
    // 3種類の時
    const combos3 = combineWithoutRepetitions(A, 3);
    const N3 = combos3.length;
    
    for(let i=0; i<N3; i++) {
        answer += getGcd(combos3[i]) * 6;
    }
    
    // console.log("answer:" + answer)
    
    console.log(answer);
}
main(require('fs').readFileSync('/dev/stdin', 'utf8'));