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



Sample input:
2


Sample output:
9

Submission status: Accepted

Solution code:
import math
K = int(input())

ans = 0
ab = []
for b in range(1, K+1):
    for a in range(1, K+1):
        ab.append(math.gcd(a, b))
        
for c in range(1, K+1):
    for i in ab:
        ans += math.gcd(c, i)
print(ans)

Submission status: Accepted

Solution code:
import math
def gud(a,b):
  if a%b==0:
    return b
  else:
    return gud(b,a%b)
a=int(input())
ans=0
for i in range(1,a+1):
  for j in range(1,a+1):
    n=math.gcd(i,j)
    for k in range(1,a+1):
      ans+=math.gcd(n,k)
print(ans)

Submission status: Accepted

Solution code:
import math

n = int(input())
sum = 0
for i in range(1,n+1):
  for j in range(1,n+1):
    a = math.gcd(i,j)
    for k in range(1,n+1):
      sum += math.gcd(a,k)

print(sum)

Submission status: Accepted

Solution code:

def euclid(a,b):
    if b ==0:
        return a
    else:
        return euclid(b, a%b)


K = int(input())
SUM = 0
for a in range(K):
    a += 1
    for b in range(K):
        b += 1
        for c in range(K):
            c += 1
            NUMS = [a,b,c]
            SUM += euclid(euclid(a,b),c)
            #print("{}, {}".format(NUMS,SUM))
print("{}".format(SUM))

Submission status: Accepted

Solution code:
K = int(input())
import math
sumnation = 0
gcdlist = []
for A in range(1,K+1):
    for B in range(1,K+1):
        gcdlist.append(math.gcd(A,B))
for a in range(1,1+K):
    for b in range(1,1+K):
        gcd_a_b = gcdlist[(a-1)*K+b-1]
        for c in range(1,1+K):
            sumnation += gcdlist[(gcd_a_b-1)*K+c-1]
print(sumnation)

Submission status: Accepted

Solution code:
import math
import itertools
a=int(input())
ans=0
for i,j in itertools.product(range(1,a+1),range(1,a+1)):
  h=math.gcd(i,j)
  for k in range(1,a+1):
    ans=ans+math.gcd(k,h)
print(ans)

Submission status: Accepted

Solution code:
import math
from functools import reduce

def gcd(*numbers):
    return reduce(math.gcd, numbers)

def myfunc(K):
    if K == 1:
        return 1
    elif K == 2:
        return 9

    temp = K
    for a in range(2,K):
        temp += sum([gcd(K,a,b) for b in range(1,a)])*6
    temp += sum([gcd(K,a,a) for a in range(1,K)])*6

    return myfunc(K-1) + temp           

K = int(input())

print(myfunc(K))

Submission status: Accepted

Solution code:
n = int(input())
import numpy as np
total = np.gcd.outer(np.arange(1,n+1), np.gcd.outer(np.arange(1,n+1), np.arange(1, n+1)).flatten())
print(np.sum(total))

Submission status: Accepted

Solution code:
def gcd(a,b):
    if b % a == 0:
        return a
    else:
        return gcd(b % a,a)
        
ans = 0
k = int(input())
for l in range(1,k+1):
    for m in range(l,k+1):
        for n in range(m,k+1):
            if(l == m and m==n):
                ans += gcd(l,gcd(m,n))
            elif(l == m or m ==n):
                ans += gcd(l,gcd(m,n)) * 3
            else:
                ans += gcd(l,gcd(m,n)) * 6

print(ans)

Submission status: Accepted

Solution code:
K = int(input())
from math import gcd
ans = 0
for a in range(1,K+1):
    for b in range(1,K+1):
        for c in range(1,K+1):
            ans = ans + gcd(gcd(a,b),c)
print(ans)

Submission status: Accepted

Solution code:
def gcd2(x, y):
    if x % y == 0:
        return y
    else:
        return gcd2(y, x % y)

def gcd3(x, y, z):
    return gcd2(gcd2(x, y), z)

def pat(x, y, z):
    p = len(set([x, y, z]))
    if p == 1:
        return 1
    elif p == 2:
        return 3
    else:
        return 6

K = int(input())
S = 0
for i in range(1, K+1):
    for j in range(i, K+1):
        for k in range(j, K+1):
            S += gcd3(i, j, k) * pat(i, j, k)
print(S)

Submission status: Accepted

Solution code:
from math import gcd
N=int(input())
total=0

for i in range(1,N+1):
  for j in range(1,N+1):
    for k in range(1,N+1):
      total+=gcd(gcd(i,j),k)
      
print(total)

Submission status: Accepted

Solution code:
from math import gcd

K = int(input())
ans = 0 

for a in range(1, K+1):
    for b in range(1, K+1):
        for c in range(1, K+1):
            ans += gcd(gcd(a,b), c)
print(ans)

Submission status: Accepted

Solution code:
from math import gcd

n = int(input())
ans = 0
for i in range(1, n + 1):
    for j in range(1, n + 1):
        for k in range(1, n + 1):
            ans += gcd(gcd(i, j), k)
            
print(ans)

Submission status: Accepted

Solution code:
def gcd(a, b):
  if a < b:
    a_init = a
    a = b
    b = a_init
  while b != 0:
    r = a % b
    a = b
    b = r
  return a
	
K = int(input())
count = 0
temp = []
for a in range(1, K+1):
	for b in range(1, a):
		temp.append(gcd(a, b))
for c in range(1, K+1):
	for j in range(len(temp)):
		count += gcd(c, temp[j]) * 2
	for k in range(1, K+1):
		count += gcd(c, k)
print(count)

Submission status: Accepted

Solution code:
from math import gcd

def threegcd(a, b, c):
    return gcd(gcd(a,b), c)

k = int(input())

ans = 0

for i in range(1, k+1):
    for j in range(1, k+1):
        for k in range(1, k+1):
            ans += threegcd(i, j, k)

print(ans)

Submission status: Accepted

Solution code:
K = int(input())
def gcd(a, b):
    if a < b:
        a, b = b, a
    while b != 0:
        r = a%b
        a = b
        b = r
    return a
count = 0
temp = list()
for n in range(1, K+1):
    for m in range(1, n):
        temp.append(gcd(n, m))
for l in range(1, K+1):
    for j in range(len(temp)):
        p = gcd(l, temp[j])
        count += 2 * p
    for k in range(1, K+1):
        q = gcd(l, k)
        count += q
print(count)

Submission status: Accepted

Solution code:
import math, string, itertools, fractions, heapq, collections, re, array, bisect, copy, functools, random
import sys
from functools import reduce
from collections import deque, defaultdict, Counter; from heapq import heappush, heappop
from itertools import permutations, combinations, product, accumulate, groupby
from bisect import bisect_left, bisect_right, insort_left, insort_right
from operator import itemgetter as ig
sys.setrecursionlimit(10 ** 7)
inf = 10 ** 20; INF = float("INF"); ans = 0; tmp = 0; cnt = 0; ansli = []; tmpli = []; candili = []; stillset = set()
eps = 1.0 / 10 ** 10; mod = 10 ** 9 + 7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]; ddn = dd + [(-1, 1), (1, 1), (1, -1), (-1, -1)]; ddn9 = ddn + [(0, 0)]
"""for dx, dy in dd:
        nx = j + dx; ny = i + dy
            if 0 <= nx < w and 0 <= ny < h:"""
def wi(): return list(map(int, sys.stdin.readline().split()))
def wip(): return [int(x) - 1 for x in sys.stdin.readline().split()]#WideIntPoint
def ws(): return sys.stdin.readline().split()
def i(): return int(sys.stdin.readline())
def s(): return input()
def s_list(): return list(input())
def hi(n): return [i() for _ in range(n)]
def hs(n): return [s() for _ in range(n)]#HeightString
def mi(n): return [wi() for _ in range(n)]#MatrixInt
def num_grid(n): return [[int(i) for i in sys.stdin.readline().split()[0]] for _ in range(n)]#NumberGrid
def mip(n): return [wip() for _ in range(n)]
def ms(n): return [ws() for _ in range(n)]
def grid(n): return [s_list() for _ in range(n)]

def gcd(a,b):
    if b == 0:
        return a
    return gcd(b, a % b)

def gcd_list(numbers):
    return reduce(gcd, numbers)

K = i()

sigma = 0
nums = [i for i in range(1, K + 1)]
for abc in itertools.combinations_with_replacement(nums, 3):  # 重複組み合わせ，200H3
    tmp = gcd_list(abc)
    abc_set = set(abc)
    sigma += int(tmp * (6 / (math.factorial(-len(abc_set) + 4))))

print(sigma)

"""
[1, 2, 3]
(1, 1)
(1, 2)
(1, 3)
(2, 2)
(2, 3)
(3, 3)
"""

Submission status: Accepted

Solution code:
from math import gcd
K = int(input())

ans =  0
for i in range(1,K+1):
    for j in range(1,K+1):
        for k in range(1,K+1):
            ans = ans + gcd(gcd(i,j),k)

print(ans)

Submission status: Accepted

Solution code:
import math, string, itertools, fractions, heapq, collections, re, array, bisect, copy, functools, random
import sys
from functools import reduce
from collections import deque, defaultdict, Counter; from heapq import heappush, heappop
from itertools import permutations, combinations, product, accumulate, groupby
from bisect import bisect_left, bisect_right, insort_left, insort_right
from operator import itemgetter as ig
sys.setrecursionlimit(10 ** 7)
inf = 10 ** 20; INF = float("INF"); ans = 0; tmp = 0; cnt = 0; ansli = []; tmpli = []; candili = []; stillset = set()
eps = 1.0 / 10 ** 10; mod = 10 ** 9 + 7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]; ddn = dd + [(-1, 1), (1, 1), (1, -1), (-1, -1)]; ddn9 = ddn + [(0, 0)]
"""for dx, dy in dd:
        nx = j + dx; ny = i + dy
            if 0 <= nx < w and 0 <= ny < h:"""
def wi(): return list(map(int, sys.stdin.readline().split()))
def wip(): return [int(x) - 1 for x in sys.stdin.readline().split()]#WideIntPoint
def ws(): return sys.stdin.readline().split()
def i(): return int(sys.stdin.readline())
def s(): return input()
def s_list(): return list(input())
def hi(n): return [i() for _ in range(n)]
def hs(n): return [s() for _ in range(n)]#HeightString
def mi(n): return [wi() for _ in range(n)]#MatrixInt
def num_grid(n): return [[int(i) for i in sys.stdin.readline().split()[0]] for _ in range(n)]#NumberGrid
def mip(n): return [wip() for _ in range(n)]
def ms(n): return [ws() for _ in range(n)]
def grid(n): return [s_list() for _ in range(n)]

def gcd(a,b):
    if b == 0:
        return a
    return gcd(b, a % b)

def gcd_list(numbers):
    return reduce(gcd, numbers)

K = i()

sigma = 0
for a in range(1, K + 1):
    for b in range(1, K + 1):
        for c in range(1, K + 1):
            sigma += gcd_list([a, b, c])

print(sigma)

Submission status: Accepted

Solution code:
import math
k = int(input())
ans = 0
ij = 0
for i in range(k):
    for j in range(k):
        ij = math.gcd(i+1,j+1)
        for l in range(k):
            ans += math.gcd(ij,l+1)
print(ans)

Submission status: Accepted

Solution code:
import math

k = int(input())
sum = 0
for a in range(1,k+1):
    for b in range(1,k+1):
        ab = math.gcd(a,b)
        for c in range(1,k+1):
            sum += math.gcd(ab,c)
print(sum)

Submission status: Accepted

Solution code:
import math
K = int(input())
#print("K:\t" + str(K))
count = 0
for i in range(1, K+1):
    for j in range(1, K+1):
        tmp = math.gcd(i, j)
        for k in range(1, K+1):
            count += math.gcd(tmp, k)
#            print(count)
print(count)

Submission status: Accepted

Solution code:
import math
n = int(input())
a = 0
for i in range(0,n):
  for j in range(0,n):
    gcd1 = math.gcd(i+1,j+1)
    
    for k in range(0,n):
      gcd2 = math.gcd(gcd1,k+1)
      a = a + gcd2
      
print(a)

Submission status: Accepted

Solution code:
import math
K=int(input())
dp=[[0][0]]*K
sum_gcd=0
c=1
for a in range(1,K+1):
    for b in  range(1,K+1):
            for c in  range(1,K+1):
                sum_gcd+=math.gcd(c,math.gcd(a,b))
print(sum_gcd)

Submission status: Accepted

Solution code:
from collections import deque
from functools import reduce
import math; floor,ceil = math.floor,math.ceil
import sys; input = lambda: sys.stdin.readline().rstrip()
aint = lambda: int(input()); ints = lambda: list(map(int, input().split()))
Yes = lambda boolean: print('Yes') if boolean else print('No')
YES = lambda boolean: print('YES') if boolean else print('NO')
is_even = lambda x: True if x%2==0 else False
s = lambda x: int(x*(x+1)*0.5)

def gcd(*numbers):
    return reduce(math.gcd, numbers)

def main():
    n = aint()
    ans = 0
    for a in range(n):
      for b in range(a,n):
        for c in range(b,n):
          if (a==b and b==c):
            ans += gcd(a+1,b+1,c+1)
          elif (a==b or b==c or c==a):
            ans += gcd(a+1,b+1,c+1)*3
          else:
            ans += gcd(a+1,b+1,c+1)*6
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from math import gcd
K = int(input())
Sum = 0
for i in range(1,K+1):
    for j in range(i,K+1):
        for k in range(j,K+1):
            if i==j and j==k:
                Sum += i
            elif i!=j and j!=k and k!=i:
                Sum += gcd(i,gcd(j,k))*6
            else:
                Sum += gcd(i,gcd(j,k))*3
                
print(Sum)

Submission status: Accepted

Solution code:
import math

k = int(input())
a = []
result = []

for i in range(1,k+1):
    for j in range(1,k+1):
        a.append(math.gcd(i,j))

for i in range(1,k+1):
    result.extend(math.gcd(n,i) for n in a)

print(sum(result))

Submission status: Accepted

Solution code:
import math
 
N=int(input())
 
A = 0
for i in range(1,N+1):
  for j in range(1,N+1):
    for k in range(1,N+1):
      A += math.gcd(math.gcd(i,j),k)

print(A)

Submission status: Accepted

Solution code:
import math

K = int(input())

gcd2 = [[0 for i in range(K)] for i in range(K)]

for i in range(K):
  for j in range(K):
    gcd2[i][j] = math.gcd(i+1, j+1)
    
sum = 0
for i in range(K):
  for j in range(K):
    for k in range(K):
      sum += math.gcd(gcd2[i][j], k+1)
      
print(sum)

Submission status: Accepted

Solution code:
import math
K=int(input())
ans=0
for i in range(1,K+1):
    for j in range(1,K+1):
        a=math.gcd(i,j)
        for k in range(1,K+1):
            ans+=math.gcd(a,k)
print(ans)

Submission status: Accepted

Solution code:
import sys
import math
from functools import reduce


def main():
    input = sys.stdin.readline
    K = int(input())
    assert 1 <= K <= 200
    total = 0
    for i in range(1, K+1):
        for j in range(1, K+1):
            a = euclidean_algorithm(i, j)
            for k in range(1, K+1):
                total += euclidean_algorithm(a, k)
    print(total)


def euclidean_algorithm(a, b):
    r = a % b
    while r != 0:
        a = b
        b = r
        r = a % b
    return b


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys
import math
from functools import reduce


def main():
    input = sys.stdin.readline
    K = int(input())
    assert 1 <= K <= 200
    total = 0
    for i in range(1, K+1):
        for j in range(1, K+1):
            a = euclidean_algorithm(i, j)
            for k in range(1, K+1):
                total += euclidean_algorithm(a, k)
    print(total)


def gcd_list(numbers):
    return reduce(euclidean_algorithm, numbers)


def euclidean_algorithm(a, b):
    r = a % b
    while r != 0:
        a = b
        b = r
        r = a % b
    return b


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
K = int(input())
 
import math

ans = 0
a = 0

for i in range(1,K+1):
  for j in range(1,K+1):
    a = math.gcd(i,j)
    for k in range(1,K+1):
      ans += math.gcd(a,k)
      
print(ans)

Submission status: Accepted

Solution code:
from math import gcd
k=int(input())
ans=0
for i in range(1,k+1):
    for j in range(1,k+1):
        x=gcd(i,j)
        for l in range(1,k+1):
            ans+=gcd(x,l)
print(ans)

Submission status: Accepted

Solution code:
from math import gcd
k = int(input())
ans = 0
for i in range(1, k+1):
    for j in range(1, k+1):
        for y in range(1, k+1):
            ans += gcd(gcd(i,j), y)
print(ans)

Submission status: Accepted

Solution code:
import math
k=int(input())
gcd_xy=0
ans=0
for x in range(1,k+1):
    for y in range(1,k+1):
        gcd_xy=math.gcd(x,y)
        for z in range(1,k+1):
            ans += math.gcd(gcd_xy,z)
print(ans)

Submission status: Accepted

Solution code:
# https://atcoder.jp/contests/abc162/tasks/abc162_c
import math
K=int(input())
# K=200
tot=0
# for i in range(1,K+1):
#     for j in range(1,K+1):
#         tmp=math.gcd(i,j)
#         for k in range(1,K+1):
#             tot+=math.gcd(tmp,k)
ls=[0]*202
for i in range(1,K+1):
    for j in range(1,K+1):
        tmp=math.gcd(i,j)
        ls[tmp+1]+=1
tot=0
for i in range(1,K+1):
    for j in range(1,201):
        tot+=ls[j+1]*math.gcd(j,i)

print(tot)

Submission status: Accepted

Solution code:
import math
from functools import reduce

def gcd(*numbers):
    return reduce(math.gcd, numbers)

count = 0
n = int(input())
for i in range(1, n+1):
    for j in range(i, n+1):
        for k in range(j, n+1):
            if i==j==k:
                count += gcd(i, j, k)
            elif i==j or j==k:
                count += 3*gcd(i, j, k)
            else:
                count += 6*gcd(i, j, k)

print(count)

Submission status: Accepted

Solution code:
import math
from functools import reduce

def gcd(*numbers):
    return reduce(math.gcd, numbers)


K=int(input())
ans=0
for i in range(1,K+1):
    for j in range(i,K+1):
        for k in range(j,K+1):
            if i==j and j==k:
                ans=ans+gcd(i,j,k)
            elif i==j or j==k:
                ans=ans+3*gcd(i,j,k)
            else:
                ans=ans+6*gcd(i,j,k)
            
print(ans)

Submission status: Accepted

Solution code:
import math
K=int(input())
tot=0
for i in range(1,K+1):
    for j in range(1,K+1):
        tmp=math.gcd(i,j)
        for k in range(1,K+1):
            tot+=math.gcd(tmp,k)
print(tot)

Submission status: Accepted

Solution code:
import math
from functools import reduce

K = int(input())

def gcd(*numbers):
    return reduce(math.gcd, numbers)

ans = 0
for i in range(1,K+1):
    for j in range(i,K+1):
        for k in range(j,K+1):
            if i==j==k:
                ans += gcd(i,j,k)
            elif j==k or i==j: 
                ans += 3*gcd(i,j,k)
            else: ans += 6*gcd(i,j,k)
print(ans)

Submission status: Accepted

Solution code:
# In peace, in love, in war, everything is the same

import math

ar = [[0]*201 for i in range(201)]
for i in range(1, 201, 1):
	for j in range(1, 201, 1):
		ar[i][j] = math.gcd(i, j)
		
n, ans = int(input()), 0
for i in range(1, n+1, 1):
	for j in range(1, n+1, 1):
		for k in range(1, n+1, 1):
			ans += ar[ar[i][j]][k]
print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

#############
# Libraries #
#############

import sys
input = sys.stdin.readline

import math
from collections import deque
from fractions import gcd
from functools import lru_cache


#############
# Constants #
#############

MOD = 10**9+7
INF = float('inf')

#############
# Functions #
#############

######INPUT######
def inputI(): return int(input().strip())
def inputS(): return input().strip()
def inputIL(): return list(map(int,input().split()))
def inputSL(): return list(map(str,input().split()))
def inputILs(n): return list(int(input()) for _ in range(n))
def inputSLs(n): return list(input().strip() for _ in range(n))
def inputILL(n): return [list(map(int, input().split())) for _ in range(n)]
def inputSLL(n): return [list(map(str, input().split())) for _ in range(n)]

#####Inverse#####
def inv(n): return pow(n, MOD-2, MOD)

######Combination######
kaijo_memo = []
def kaijo(n):
  if(len(kaijo_memo) > n):
    return kaijo_memo[n]
  if(len(kaijo_memo) == 0):
    kaijo_memo.append(1)
  while(len(kaijo_memo) <= n):
    kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
  return kaijo_memo[n]

gyaku_kaijo_memo = []
def gyaku_kaijo(n):
  if(len(gyaku_kaijo_memo) > n):
    return gyaku_kaijo_memo[n]
  if(len(gyaku_kaijo_memo) == 0):
    gyaku_kaijo_memo.append(1)
  while(len(gyaku_kaijo_memo) <= n):
    gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
  return gyaku_kaijo_memo[n]

def nCr(n,r):
  if(n == r):
    return 1
  if(n < r or r < 0):
    return 0
  ret = 1
  ret = ret * kaijo(n) % MOD
  ret = ret * gyaku_kaijo(r) % MOD
  ret = ret * gyaku_kaijo(n-r) % MOD
  return ret

######Factorization######
def factorization(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp%i==0:
            cnt=0
            while temp%i==0:
                cnt+=1
                temp //= i
            arr.append([i, cnt])
    if temp!=1:
        arr.append([temp, 1])

    if arr==[]:
        arr.append([n, 1])
    return arr

#####LCM#####
def lcm(a, b):
    return a * b // gcd (a, b)

#############
# Main Code #
#############

K = inputI()

l = [[gcd(i,j) for i in range(1,K+1)] for j in range(1,K+1)]

dic = [0 for i in range(1,K+2)]

for item in l:
  for i in item:
    dic[i] += 1
    
#print(dic)

res = 0
for a in range(1,K+1):
  for d in range(1,K+1):
    res += dic[a] * l[a-1][d-1]
      
print(res)

Submission status: Accepted

Solution code:
import math
import itertools
k = int(input())
a = [i for i in range(1,k+1)]

two = list(itertools.product(a,a))

ans = 0
for x,y in two:
    gcd1 = math.gcd(x,y)
    for i in range(1,k+1):
        gcd2 = math.gcd(gcd1,i)
        ans += gcd2
        
print(ans)

Submission status: Accepted

Solution code:
import math
from functools import reduce
import itertools

k = int(input())
total = 0

def gcd(*numbers):
    return reduce(math.gcd, numbers)


for i in range(1,k+1,1):
    for j in range(i,k+1,1):
        for l in range(j,k+1,1):
            GCD = gcd(i, j, l)
            l = [i,j,l]
            cnt = 0
            for num in l:
                cnt = max(cnt, l.count(num))
            if cnt == 1:
                sum = 6
            elif cnt == 2:
                sum = 3
            elif cnt == 3:
                sum = 1

            total += GCD*sum

print(total)

Submission status: Accepted

Solution code:
import sys
import math
from collections import deque
from decimal import Decimal, ROUND_HALF_UP, ROUND_HALF_EVEN
def I(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LI2(N): return [list(map(int, sys.stdin.readline().split())) for i in range(N)]
def S(): return sys.stdin.readline()
def LS(): return sys.stdin.readline().split()
def LS2(N): return [sys.stdin.readline().split() for i in range(N)]
def FILL(i,h,w): return [[i for j in range(w)] for k in range(h)]
def sisha(num,digit): return Decimal(str(num)).quantize(Decimal(digit),rounding=ROUND_HALF_UP)
#'0.01'や'1E1'などで指定、整数に戻すならintをかます
MOD = 1000000007
INF = float("inf")
sys.setrecursionlimit(10**6+10)

input = sys.stdin.readline


K = I()
def gcd(a,b):
    while b!=0:
        c = a%b
        a=b
        b=c
    return a
ans = 0
for i in range(K):
    for j in range(K):
        for k in range(K):
            ans += gcd(gcd(i+1,j+1),k+1)

print(ans)

Submission status: Accepted

Solution code:
import math
from functools import reduce
import itertools

K = int(input())

gcd_sum = 0
target_range = range(1, K + 1)
for comb in itertools.combinations_with_replacement(target_range, 3):
  a, b, c = comb[0], comb[1], comb[2]
  gcd = reduce(math.gcd, comb)
  
  if a == b == c:
    gcd_sum += gcd
  elif a != b != c:
    gcd_sum += gcd * 6
  else:
    gcd_sum += gcd * 3


print(gcd_sum)

Submission status: Accepted

Solution code:
import collections
import math
# math.gcd(a,b)
k = int(input())
cnts = collections.defaultdict(int)
for i in range(1, k+1):
    for j in range(1, k+1):
        cnts[math.gcd(i,j)] += 1
ans = 0
for i in range(1, k+1):
    for key in cnts.keys():
        ans += math.gcd(i, key)*cnts[key]
print(ans)

Submission status: Accepted

Solution code:
K = int(input())

import math
ans = 0

for a in range(1,K+1):
  for b in range(1,K+1):
    for c in range(1,K+1):
      ans += math.gcd(math.gcd(a,b),c)

print(ans)

Submission status: Accepted

Solution code:
import math
total = 0
K = int(input())
for i in range(1, K+1):
    for j in range(1, K+1):
        ans2 = math.gcd(i, j)
        for k in range(1, K+1):
            ans3 = math.gcd(ans2, k)
            #print(i, j, k, ans3)
            total += ans3
print(total)

Submission status: Accepted

Solution code:
import math

K = int(input())

gcd2 = [[0 for i in range(K)] for i in range(K)]

for i in range(K):
    for j in range(K):
        gcd2[i][j] = math.gcd(i+1,j+1)

sum = 0
for i in range(K):
    for j in range(K):
        for k in range(K):
            sum += math.gcd(gcd2[i][j],k+1)

print(sum)

Submission status: Accepted

Solution code:
import math

K = int(input())

amount = 0
for a in range(1, K+1):
    for b in range(1, K+1):
        gcd_ab = math.gcd(a, b)
        for c in range(1, K+1):
            amount += math.gcd(gcd_ab, c)

print(amount)

Submission status: Accepted

Solution code:
import math
n = int(input())
ans = 0
for i in range(1, n+1):
    for j in range(1, n+1):
        t = math.gcd(i, j)
        for k in range(1, n+1):
            ans += math.gcd(t, k)
print(ans)

Submission status: Accepted

Solution code:
import math
  
k=int(input())
r=0
ls=[]

for i in range(k):
  for j in range(k):
    a=math.gcd(i+1,j+1)
    for l in range(k):
      r+=math.gcd(a,l+1)
print(r)

Submission status: Accepted

Solution code:
import math
k = int(input())
ans = 0
for i in range(1, k+1):
  for j in range(1, k+1):
    a = math.gcd(i, j)
    for l in range(1, k+1):
      ans += math.gcd(l, a)
print(ans)

Submission status: Accepted

Solution code:
from math import gcd
c = 0
k = int(input())
for i in range(1, k + 1):
  for j in range(1, k + 1):
    for n in range(1, k + 1):
      c += gcd(gcd(i, j), n)
print(c)

Submission status: Accepted

Solution code:
from math import gcd

K = int(input())

result = 0
for a in range(1, K + 1):
    for b in range(1, K + 1):
        t = gcd(a, b)
        for c in range(1, K + 1):
            result += gcd(t, c)
print(result)

Submission status: Accepted

Solution code:
import numpy as np
def C():
    n = int(input())
    dp_gcd2d = [[0 for i in range(n+1)] for i in range(n+1)]
    dp = [[[0 for i in range(n+1)] for j in range(n+1)] for k in range(n+1)]
    def gcd(a, b):
        if b == 0:
            dp_gcd2d[a][b] = a
            return a
        elif dp_gcd2d[a][b] != 0:
            return dp_gcd2d[a][b]
        else:
            dp_gcd2d[a][b] = dp_gcd2d[b][a%b]
            return gcd(b, a%b)
    for i in range(1, n+1):
        for j in range(1, n+1):
            dp_gcd2d[i][j] = gcd(i,j)
    for i in range(1, n+1):
        for j in range(i, n+1):
            for k in range(j, n+1):
                dp[i][j][k] = dp_gcd2d[dp_gcd2d[i][j]][k]
                dp[j][i][k] = dp[i][j][k]
                dp[i][k][j] = dp[i][j][k]
                dp[k][i][j] = dp[i][j][k]
                dp[k][j][i] = dp[i][j][k]
                dp[j][k][i] = dp[i][j][k]
    print(np.sum(np.array(dp)))
    

C()

Submission status: Accepted

Solution code:
from math import gcd
K = int(input())

memo =[[0 for i in range(K+1)] for j in range(K+1)]
for i in range(K+1):
    memo[1][i] = 1
    memo[i][1] = 1

cnt =0

for i in range(1,K+1):
        for j in range(1,K+1):
                t = gcd(i,j)
                for k in range(1,K+1):
                    cnt += gcd(t,k)


print(cnt)

Submission status: Accepted

Solution code:
from math import gcd
k = int(input())

res = 0
for a in range(1, k+1):
    for b in range(1, k+1):
        t = gcd(a, b)
        for c in range(1, k+1):
            res += gcd(t, c)

print(res)

Submission status: Accepted

Solution code:
import math
from functools import reduce

def gcd(*numbers):
    return reduce(math.gcd, numbers)
 
'''
l = []
for a in range(1,int(input())+1):
    for b in range(1, int(input())+1):
        for c in range(1,int(input())+1):
            l.append(gcd(a,b,c))
'''
N = input() 
N = int(N)+1
l = (gcd(a,b,c) for a in range(1,N) for b in range(1,N) for c in range(1,N))
print(sum(l))

Submission status: Accepted

Solution code:
import sys
from fractions import gcd

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

K = int(read())
ans = 0

gcd_list = [[0]*210 for i in range(210)]
num_ct = [0]*210

for a in range(K):
    for b in range(K):
        tmp = gcd(a+1,b+1)
        gcd_list[a+1][b+1] = tmp
        num_ct[tmp] += 1

for c in range(K):
    for tmp in range(K):
        ans += gcd_list[c+1][tmp+1] * num_ct[tmp+1]

print(ans)

Submission status: Accepted

Solution code:
from math import gcd
K = int(input())
print(sum(gcd(gcd(a, b), c) for c in range(1, K + 1) for b in range(1, K + 1) for a in range(1, K + 1)))

Submission status: Accepted

Solution code:
import math
n = int(input())
ans = 0
for i in range (1, n + 1) :
   for j in range(1, n + 1) :
       for k in range(1, n + 1) :
            ans += math.gcd(i, math.gcd(j,k))
print(ans)

Submission status: Accepted

Solution code:
def gcd(x, y):
    while y:
        x, y = y, x % y
    return x

k = int(input())
res = [0] * (k + 1)

ans = 0

for i in range(1, k + 1):
    for j in range(1, k + 1):
        res[gcd(i, j)] += 1

for n in range(1, k + 1):
    for r in range(1, k + 1):
        ans += gcd(r, n) * res[r]

print(ans)

Submission status: Accepted

Solution code:
import math

k=int(input())

ans=0

for l in range(1,k+1):
  for m in range(1,k+1):
    tmp=math.gcd(l,m)
    for n in range(1,k+1):
      gcd=math.gcd(tmp,n)
      ans+=gcd
      
print(ans)

Submission status: Accepted

Solution code:
import math

n = int(input())

g = 0
sum = 0


for i in range(1,n+1):
  for j in range(1,n+1):
    g = math.gcd(i,j)
    for k in range(1,n+1):
      sum += math.gcd(g,k)
      
print(sum)

Submission status: Accepted

Solution code:
from functools import lru_cache
@lru_cache()
def gcd(x,y):
    r = x%y
    if r == 0:
        return y
    return gcd(y,r)

K = int(input())
Sum = 0

for i in range(1,K+1):
    Sum += i
    for j in range(i+1,K+1):
        Sum += 6*gcd(i,j)
        for k in range(j+1,K+1):
            Sum += 6*gcd(gcd(i,j),k)
print(Sum)

Submission status: Accepted

Solution code:
import math

k=int(input())

sum=0
for l in range(k):
    for m in range(k):
        tmp=math.gcd(l+1,m+1)
        for n in range(k):
            gcd=math.gcd(tmp,n+1)
            sum+=gcd

print(sum)

Submission status: Accepted

Solution code:
import numpy as np

k = int(input()) + 1
x = np.arange(1, k)
s = np.gcd.outer(np.gcd.outer(x, x), x)
print(np.sum(s))

Submission status: Accepted

Solution code:
import math
from functools import reduce


def gcd_list(numbers):
    return reduce(math.gcd, numbers)

n=int(input())
l1=range(1,n+1)
l2=range(1,n+1)
l3=range(1,n+1)

count=0

for i in l1:
    for j in l2:
        te=math.gcd(i,j)
        for k in l3:
            count+=math.gcd(te,k)

print(count)

Submission status: Accepted

Solution code:
def sum_of_gcd_tuples():
    from math import gcd
    K = int(input())
    total = 0

    for a in range(1, K + 1):
        for b in range(1, K + 1):
            for c in range(1, K + 1):
                total += gcd(gcd(a, b), c)

    print(total)


sum_of_gcd_tuples()

Submission status: Accepted

Solution code:
import math

K = int(input())

A = 0

#3つとも同じ数字
for i in range(K):
    A += i+1
    
#2つ同じ数字
for i in range(K):
    for j in range(K):
        if i > j:
            A += 6*math.gcd(i+1, j+1)

#全部違う数字
for i in range(K):
    for j in range(K):
        for k in range(K):
            if i > j and j > k:
                A += 6*math.gcd(math.gcd(i+1, j+1), k+1)

print(A)

Submission status: Accepted

Solution code:
from math import gcd 
n = int(input())
gcdsum = 0

for i in range(1,n+1):
    for j in range(1,n+1):
        for k in range(1,n+1):
            gcdsum += gcd(i, gcd(j,k))

print(gcdsum)