Problem: 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.

Submission attempts (in order):
def main():
    n = int(input())
    s = input()
    res = 0
    r = []
    g = []
    b = []
    for i, c in enumerate(s):
        if c == 'R':
            r.append(i)
        elif c == 'G':
            g.append(i)
        else:
            b.append(i)
    
    cnt = 0
    for i in r:
        for j in g:
            diff = abs(j-i)
            for k in b:
                if abs(i-k) != diff and abs(j-k) != diff and abs(j-k) != abs(i-k):
                    cnt += 1
    print(cnt)
                    


if __name__ == '__main__':
    main()



import math
from itertools import combinations

def main():
    k = int(input())
    res = 0
    memo = [[0 for _ in range(k+1)] for _ in range(k+1)]
    for i ian range(1, k+1):
        for j in range(i, k+1):
            tmp = math.gcd(i,j)
            memo[i][j] = tmp
            memo[j][i] = tmp

    for i in range(1, k+1):
        res += i 

    for c in combinations(range(1,k+1),2):
        ab = memo[c[0], c[1]]
        res += ab * 6

    for c in combinations(range(1,k+1), 3):
        ab = memo[c[0], c[1]]
        abc = memo[ab, c[2]]
        res += abc * 6
    print(res)


if __name__ == '__main__':
    main()


from math import gcd
from itertools import combinations

def main():
    k = int(input())
    res = 0
    memo = [[0 for _ in range(k)] for _ in range(k)]
    for i in range(1, k+1):
        for j in range(i, k+1):
            tmp = gcd(i,j)
            memo[i][j] = tmp
            memo[j][i] = tmp

    for i in range(1, k+1):
        res += i 

    for c in combinations(range(1,k+1),2):
        ab = memo[c[0], c[1]]
        res += ab * 6

    for c in combinations(range(1,k+1), 3):
        ab = memo[c[0], c[1]]
        abc = memo[ab, c[2]]
        res += abc * 6
    print(res)


if __name__ == '__main__':
    main()



import math
from itertools import combinations

def main():
    k = int(input())
    res = 0
    memo = [[0 for _ in range(k)] for _ in range(k)]
    for i ian range(1, k+1):
        for j in range(i, k+1):
            tmp = math.gcd(i,j)
            memo[i][j] = tmp
            memo[j][i] = tmp

    for i in range(1, k+1):
        res += i 

    for c in combinations(range(1,k+1),2):
        ab = memo[c[0], c[1]]
        res += ab * 6

    for c in combinations(range(1,k+1), 3):
        ab = memo[c[0], c[1]]
        abc = memo[ab, c[2]]
        res += abc * 6
    print(res)


if __name__ == '__main__':
    main()

import math
from itertools import combinations

def main():
    k = int(input())
    res = 0
    memo = [[0 for _ in range(k+1)] for _ in range(k+1)]
    for i in range(1, k+1):
        for j in range(i, k+1):
            tmp = math.gcd(i,j)
            memo[i][j] = tmp
            memo[j][i] = tmp

    for i in range(1, k+1):
        res += i 

    for c in combinations(range(1,k+1),2):
        ab = memo[c[0]][c[1]]
        res += ab * 6

    for c in combinations(range(1,k+1), 3):
        ab = memo[c[0]][c[1]]
        abc = memo[ab][c[2]]
        res += abc * 6
    print(res)


if __name__ == '__main__':
    main()



Verdicts: Runtime Error

Runtime Error

Runtime Error

Runtime Error

Accepted