Problem:


Score : 900 points

Problem Statement

AtCoDeer the deer has N square tiles. The tiles are numbered 1 through N, and the number given to each tile is written on one side of the tile. Also, each corner of each tile is painted in one of the 1000 colors, which are represented by the integers 0 between 999. The top-left, top-right, bottom-right and bottom-left corner of the tile with the number i are painted in color C_{i,0}, C_{i,1}, C_{i,2} and C_{i,3}, respectively, when seen in the direction of the number written on the tile (See Figure 1).

Figure 1: The correspondence between the colors of a tile and the input

AtCoDeer is constructing a cube using six of these tiles, under the following conditions:

For each tile, the side with the number must face outward.

For each vertex of the cube, the three corners of the tiles that forms it must all be painted in the same color.

Help him by finding the number of the different cubes that can be constructed under the conditions.
Since each tile has a number written on it, two cubes are considered different if the set of the used tiles are different, or the tiles are used in different directions, even if the formation of the colors are the same. (Each tile can be used in one of the four directions, obtained by 90° rotations.) Two cubes are considered the same only if rotating one in the three dimensional space can obtain an exact copy of the other, including the directions of the tiles.

Figure 2: The four directions of a tile

Constraints

6≦N≦400

0≦C_{i,j}≦999 (1≦i≦N , 0≦j≦3)

Input

The input is given from Standard Input in the following format:

N
C_{1,0} C_{1,1} C_{1,2} C_{1,3}
C_{2,0} C_{2,1} C_{2,2} C_{2,3}
:
C_{N,0} C_{N,1} C_{N,2} C_{N,3}

Output

Print the number of the different cubes that can be constructed under the conditions.

Sample Input 1

6
0 1 2 3
0 4 6 1
1 6 7 2
2 7 5 3
6 4 5 7
4 0 3 5

Sample Output 1

1

The cube below can be constructed.

Sample Input 2

8
0 0 0 0
0 0 1 1
0 1 0 1
0 1 1 0
1 0 0 1
1 0 1 0
1 1 0 0
1 1 1 1

Sample Output 2

144

Sample Input 3

6
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0

Sample Output 3

122880



Sample input:
6
0 1 2 3
0 4 6 1
1 6 7 2
2 7 5 3
6 4 5 7
4 0 3 5


Sample output:
1

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
N, = map(int, input().split())

def normal(xs):
    mnx = min(xs)
    xi = xs.index(mnx)
    if xs[(xi+3)%4] == mnx:
        if xs[(xi+2)%4] == mnx:
            xi = (xi+2)%4
        else:
            xi = (xi+3)%4
    return (xs[xi%4], xs[(xi+1)%4], xs[(xi+2)%4], xs[(xi+3)%4])

dd = defaultdict(int)
ss = []
for _ in range(N):
    a, b, c, d = map(int, input().split())
    x = normal([a,b,c,d])
    n=1
    if x[0] == x[2] and x[1] == x[3]:
        n *= 2
        if x[0] == x[1]:
            n *= 2
    dd[x] += n
    ss.append(x)

def icr(x):
    n = 1
    if x[0] == x[2] and x[1] == x[3]:
        n *= 2
        if x[0] == x[1]:
            n *= 2
    dd[x] += n

def dcr(x):
    n = 1
    if x[0] == x[2] and x[1] == x[3]:
        n *= 2
        if x[0] == x[1]:
            n *= 2
    dd[x] -= n

def f(ff, gg):
    #print(dd)
    a,b,c,d=ff
    e,h,g,f=gg
    tl = list(map(normal, [(a,e,f,b), (b,f,g,c), (c,g,h,d), (d,h,e,a)]))
    r = 1
#    print(ff, gg, r)
    for cp in tl:
        r *= dd[cp]
        dcr(cp)
#        print(r, cp, dd[cp])
    for cp in tl:
        icr(cp)
    #print(ff,gg,r)
    return r

r = 0
for i in range(N):
    ff = ss[i]
    dcr(ff)
    for j in range(i+1, N):
        if i == j:
            continue
        sl = ss[j]
        dcr(sl)
        #print(dd)
        x, y, z, w = sl
        sls = [(x,y,z,w), (y,z,w,x), (z,w,x,y), (w,x,y,z)]
        for s in sls:
            r += f(ff, s)
        icr(sl)
    icr(ff)
print(r//3)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
N, = map(int, input().split())

def normal(xs):
    mnx = min(xs)
    xi = xs.index(mnx)
    if xs[(xi+3)%4] == mnx:
        if xs[(xi+2)%4] == mnx:
            xi = (xi+2)%4
        else:
            xi = (xi+3)%4
    return (xs[xi%4], xs[(xi+1)%4], xs[(xi+2)%4], xs[(xi+3)%4])

dd = defaultdict(int)
ss = []
for _ in range(N):
    a, b, c, d = map(int, input().split())
    x = normal([a,b,c,d])
    n=1
    if x[0] == x[2] and x[1] == x[3]:
        n *= 2
        if x[0] == x[1]:
            n *= 2
    dd[x] += n
    ss.append(x)

def icr(x):
    n = 1
    if x[0] == x[2] and x[1] == x[3]:
        n *= 2
        if x[0] == x[1]:
            n *= 2
    dd[x] += n

def dcr(x):
    n = 1
    if x[0] == x[2] and x[1] == x[3]:
        n *= 2
        if x[0] == x[1]:
            n *= 2
    dd[x] -= n

def f(ff, gg):
    #print(dd)
    a,b,c,d=ff
    e,h,g,f=gg
    tl = list(map(normal, [(a,e,f,b), (b,f,g,c), (c,g,h,d), (d,h,e,a)]))
    r = 1
#    print(ff, gg, r)
    for cp in tl:
        r *= dd[cp]
        dcr(cp)
#        print(r, cp, dd[cp])
    for cp in tl:
        icr(cp)
    #print(ff,gg,r)
    return r

r = 0
for i in range(N):
    ff = ss[i]
    dcr(ff)
    for j in range(N):
        if i == j:
            continue
        sl = ss[j]
        dcr(sl)
        #print(dd)
        x, y, z, w = sl
        sls = [(x,y,z,w), (y,z,w,x), (z,w,x,y), (w,x,y,z)]
        for s in sls:
            r += f(ff, s)
        icr(sl)
    icr(ff)
print(r//6)

Submission status: Wrong Answer

Solution code:
from collections import deque
N = int(input())
C = []
M = {}
for i in range(N):
    *c, = 0, 0, 0, 0 #map(int, input().split())
    c = tuple(min(c[j:] + c[:j] for j in range(1, 5)))
    C.append(c)
    if c not in M:
        M[c] = deque([i])
    else:
        M[c].append(i)
def count(p, q, r, s):
    if p == q == r == s:
        return 4
    if p == r and q == s:
        return 2
    return 1
def solve(ci, cj, k):
    R = {}
    for l in range(4):
        #  [l]   [l-1]
        # [l+k] [l+k+1]
        c = ci[l], ci[l-1], cj[k-l], cj[k-l-1]
        c = tuple(min(c[j:] + c[:j] for j in range(1, 5)))
        if c not in M:
            return 0
        R[c] = R.get(c, 0) + 1
    res = 1
    for c in R:
        m = M[c]
        cnt = len(m)
        if c == cj:
            cnt -= 1
        if cnt < R[c]:
            return 0
        k = count(*c)
        for p in range(cnt-R[c]+1, cnt+1):
            res *= p * k
    return res

ans = 0
for i in range(N):
    ci = C[i]
    q = M[ci]; q.popleft()
    if not q:
        del M[ci]
    for j in range(i+1, N):
        cj = C[j]
        for k in range(4):
            ans += solve(ci, cj, k)
print(ans)

Submission status: Wrong Answer

Solution code:
from bisect import bisect
N = int(input())
C = []
M = {}
for i in range(N):
    *c, = map(int, input().split())
    c = tuple(min(c[j:] + c[:j] for j in range(1, 5)))
    C.append(c)
    M.setdefault(c, []).append(i)
def count(p, q, r, s):
    if p == q == r == s:
        return 4
    if p == r and q == s:
        return 2
    return 1
def solve(i, j, k):
    ci = C[i]; cj = C[j]
    R = {}
    for l in range(4):
        #  [l]   [l-1]
        # [l+k] [l+k+1]
        c = ci[l], ci[l-1], cj[(k-l+1)%4], cj[(k-l)%4]
        c = tuple(min(c[j:] + c[:j] for j in range(1, 5)))
        if c not in M:
            return 0
        R[c] = R.get(c, 0) + 1
    res = 1
    for c in R:
        m = M[c]
        cnt = len(m) - bisect(m, i)
        if c == cj:
            cnt -= 1
        if cnt < R[c]:
            return 0
        k = count(*c)
        r = 1
        for p in range(cnt-R[c]+1, cnt+1):
            r *= p
        r *= k**cnt
        res *= r
    return res

ans = 0
for i in range(N):
    for j in range(i+1, N):
        for k in range(4):
            ans += solve(i, j, k)
print(ans)

Submission status: Wrong Answer

Solution code:
n=input()
Cs=[map(int,raw_input().split()) for i in xrange(n)]
D=[None]*n
rot = [1]*n
dic = {}
r4 = range(4)
for i,C in enumerate(Cs):
    l=list()
    rc = 0
    for j in r4:
        c = tuple(C[j:]+C[:j])
        if c in l:
            rc += 1
        else:
            l.append(c)
            dic.setdefault(c,[]).append(i)
    if rc:
        rot[i] = (rc+1)/2*2
    D[i]=l
ans = 0
for i in xrange(n):
    di=D[i][0]
    for j in xrange(i+1,n):
        P=[None]*4
        for dj in D[j]:
            dd = {}
            dks = []
            for k in r4:
                dk = (di[k-3],di[k-4],dj[1-k],dj[-k])
                P[k] = p = tuple(filter(lambda x: x!=i and x!=j, dic.get(dk, [])))
                dd[p] = dd.get(p,0)+1
                dks.append(dk)
            if any(not p for p in P) or any(min(p) < i for p in P):
                continue
            res = 1
            if dks[0]==dks[1]==dks[2]==dks[3]:
                res = 4
            elif dks[0]==dks[2] and dks[1]==dks[3]:
                res = 2
            for k,v in dd.items():
                if len(k) < v:
                    res = 0
                    break
                for ke in k:
                    res *= rot[ke]
                while v:
                    res *= len(k)-v+1
                    v -= 1
            ans += res
print ans

Submission status: Wrong Answer

Solution code:
n=input()
Cs=[map(int,raw_input().split()) for i in xrange(n)]
D=[None]*n
rot = [1]*n
dic = {}
r4 = range(4)
for i,C in enumerate(Cs):
    l=list()
    rc = 0
    for j in r4:
        c = tuple(C[j:]+C[:j])
        if c in l:
            rc += 1
        else:
            l.append(c)
            dic.setdefault(c,[]).append(i)
    if rc:
        rot[i] = (rc+1)/2*2
    D[i]=l
ans = 0
for i in xrange(n):
    di=D[i][0]
    for j in xrange(i+1,n):
        for dj in D[j]:
            dj=list(dj)
            P=[None]*4
            dd = {}
            dks = []
            for k in r4:
                dk = (di[k-3],di[k-4],dj[1-k],dj[-k])
                P[k] = p = tuple(filter(lambda x: x!=i and x!=j, dic.get(dk, [])))
                dd[p] = dd.get(p,0)+1
                dks.append(dk)
            if any(not p for p in P) or any(min(p) < i for p in P):
                continue
            res = 1
            if dks[0]==dks[1]==dks[2]==dks[3]:
                res = 4
            elif dks[0]==dks[2] and dks[1]==dks[3]:
                res = 2
            for k,v in dd.items():
                if len(k) < v:
                    res = 0
                    break
                for ke in k:
                    res *= rot[ke]
                while v:
                    res *= len(k)-v+1
                    v -= 1
            ans += res
print ans

Submission status: Wrong Answer

Solution code:
n=input()
Cs=[map(int,raw_input().split()) for i in xrange(n)]
D=[None]*n
dic = {}
for i,C in enumerate(Cs):
    l=list()
    for j in xrange(4):
        l.append(tuple(C[j:]+C[:j]))
    D[i]=l
    for e in l:
        dic.setdefault(e,[]).append(i)
ans = 0
for i in xrange(n):
    di=D[i][0]
    for j in xrange(i+1,n):
        for dj in D[j]:
            dj=list(dj)
            P=[None]*4
            dd = {}
            for k in xrange(4):
                dk = (di[k-3],di[k-4],dj[1-k],dj[-k])
                P[k] = tuple(filter(lambda x: x!=i and x!=j, dic.get(dk, [])))
                dd[P[k]] = dd.get(P[k],0)+1
            if any(not p for p in P) or any(min(p) < i for p in P):
                continue
            res = 1
            for k,v in dd.items():
                dc = list(set(k))
                if dc < v:
                    res = 0
                    break
                for k2 in dc:
                    res *= k.count(k2)
                while v:
                    res *= len(dc)-v+1
                    v -= 1
            ans += res
print ans

Submission status: Wrong Answer

Solution code:
#    X2 
# X1 X0 X4 X5 
#    X3
#
# C0    C1
#    X0 
# C3    C2
#
# C1    C0
#    5X 
# C2    C3
#
N = int(raw_input())
# N = 6
# N = 8

C_list = []
for n in range(N):
    C_list.append(map(int, raw_input().split()))


"""
C_list = [
    [0, 1, 2, 3], 
    [0, 4, 6, 1], 
    [1, 6, 7, 2], 
    [2, 7, 5, 3], 
    [6, 4, 5, 7], 
    [4, 0, 3, 5]
]

C_list = [
    [0, 0, 0, 0], 
    [0, 0, 1, 1], 
    [0, 1, 0, 1], 
    [0, 1, 1, 0], 
    [1, 0, 0, 1], 
    [1, 0, 1, 0], 
    [1, 1, 0, 0], 
    [1, 1, 1, 1]
]

C_list = [
    [0, 0, 0, 0], 
    [0, 0, 0, 0], 
    [0, 0, 0, 0], 
    [0, 0, 0, 0], 
    [0, 0, 0, 0], 
    [0, 0, 0, 0]
]
"""
res = 0

C_dict = dict()

def sort4(x):
    return sorted([x, [x[1], x[2], x[3], x[0]], [x[2], x[3], x[0], x[1]], [x[3], x[0], x[1], x[2]]])[0]

def rotate(A, n):
    if n == 0:
        return A
    elif n == 1:
        return [A[1], A[2], A[3], A[0]]
    elif n == 2:
        return [A[2], A[3], A[0], A[1]]
    elif n == 3:
        return [A[3], A[0], A[1], A[2]]
    
def add_node(x):
    s = str(sort4(x))
    if C_dict.has_key(s):
        C_dict[s]['count'] += 1
    else:
        # count duplicate
        if x == rotate(x, 1):
            w = 4
        elif x == rotate(x, 2):
            w = 2
        else:
            w = 1
        C_dict[s] = {'count':1, 'weight':w}

def return_dict(x):
    s = str(sort4(x))
    if C_dict.has_key(s):
        return [C_dict[s]['count'], C_dict[s]['weight']]
    else:
        return [0, 0]
    
for i in range(N):
    add_node(C_list[i])
    
for i in range(N-1):
    X0_ = C_list[i]
    for j in range(i+1, N):
        X5 = [C_list[j][1], C_list[j][0], C_list[j][3], C_list[j][2]]
        for k in range(4):
            X0 = rotate(X0_, k)
            X1 = [X5[0], X5[1], X0[1], X0[0]]
            X2 = [X5[1], X5[2], X0[2], X0[1]]
            X3 = [X5[2], X5[3], X0[3], X0[2]]
            X4 = [X5[3], X5[0], X0[0], X0[3]]

            # drop duplicate
            c1 = 0
            c2 = 0
            c3 = 0
            c4 = 0
            
            if X2 in [rotate(X1, l) for l in range(4)]:
                c2 += 1
            if X3 in [rotate(X1, l) for l in range(4)]:
                c3 += 1
            if X3 in [rotate(X2, l) for l in range(4)]:
                c3 += 1
            if X4 in [rotate(X1, l) for l in range(4)]:
                c4 += 1
            if X4 in [rotate(X2, l) for l in range(4)]:
                c4 += 1
            if X4 in [rotate(X3, l) for l in range(4)]:
                c4 += 1
            
            # count how many fits
            r1, k1 = return_dict(X1)
            r2, k2 = return_dict(X2)
            r3, k3 = return_dict(X3)
            r4, k4 = return_dict(X4)
            if sort4(X0) == sort4(X1):
                r1 -= 1
            if sort4(X0) == sort4(X2):
                r2 -= 1
            if sort4(X0) == sort4(X3):
                r3 -= 1
            if sort4(X0) == sort4(X4):
                r4 -= 1
            if sort4(X5) == sort4(X1):
                r1 -= 1
            if sort4(X5) == sort4(X2):
                r2 -= 1
            if sort4(X5) == sort4(X3):
                r3 -= 1
            if sort4(X5) == sort4(X4):
                r4 -= 1
            r1 = max(r1-c1, 0)
            r2 = max(r2-c2, 0)
            r3 = max(r3-c3, 0)
            r4 = max(r4-c4, 0)
            # if r1*r2*r3*r4 > 0:
            #     print i,j,k,X0, X5, r1, r2, r3, r4, c1, c2, c3, c4, k1, k2, k3, k4
            res += r1*r2*r3*r4*k1*k2*k3*k4
            
print res / 3

Submission status: Wrong Answer

Solution code:
#    X2 
# X1 X0 X4 X5 
#    X3
#
# C0    C1
#    X0 
# C3    C2
#
# C1    C0
#    5X 
# C2    C3
#
N = int(raw_input())
# N = 6
# N = 8

C_list = []
for n in range(N):
    C_list.append(map(int, raw_input().split()))


"""
C_list = [
    [0, 1, 2, 3], 
    [0, 4, 6, 1], 
    [1, 6, 7, 2], 
    [2, 7, 5, 3], 
    [6, 4, 5, 7], 
    [4, 0, 3, 5]
]

C_list = [
    [0, 0, 0, 0], 
    [0, 0, 1, 1], 
    [0, 1, 0, 1], 
    [0, 1, 1, 0], 
    [1, 0, 0, 1], 
    [1, 0, 1, 0], 
    [1, 1, 0, 0], 
    [1, 1, 1, 1]
]

C_list = [
    [0, 0, 0, 0], 
    [0, 0, 0, 0], 
    [0, 0, 0, 0], 
    [0, 0, 0, 0], 
    [0, 0, 0, 0], 
    [0, 0, 0, 0]
]
"""
res = 0

def rotate(A, n):
    if n == 0:
        return A
    elif n == 1:
        return [A[1], A[2], A[3], A[0]]
    elif n == 2:
        return [A[2], A[3], A[0], A[1]]
    elif n == 3:
        return [A[3], A[0], A[1], A[2]]
    

C_list_1 = [rotate(C, 1) for C in C_list]
C_list_2 = [rotate(C, 2) for C in C_list]
C_list_3 = [rotate(C, 3) for C in C_list]


for i in range(N-1):
    X0_ = C_list[i]
    for j in range(i+1, N):
        X5 = [C_list[j][1], C_list[j][0], C_list[j][3], C_list[j][2]]
        for k in range(4):
            X0 = rotate(X0_, k)
            X1 = [X5[0], X5[1], X0[1], X0[0]]
            X2 = [X5[1], X5[2], X0[2], X0[1]]
            X3 = [X5[2], X5[3], X0[3], X0[2]]
            X4 = [X5[3], X5[0], X0[0], X0[3]]

            # drop duplicate
            c1 = 0
            c2 = 0
            c3 = 0
            c4 = 0
            
            if X2 in [rotate(X1, l) for l in range(4)]:
                c2 += 1
            if X3 in [rotate(X1, l) for l in range(4)]:
                c3 += 1
            if X3 in [rotate(X2, l) for l in range(4)]:
                c3 += 1
            if X4 in [rotate(X1, l) for l in range(4)]:
                c4 += 1
            if X4 in [rotate(X2, l) for l in range(4)]:
                c4 += 1
            if X4 in [rotate(X3, l) for l in range(4)]:
                c4 += 1
            
            # count how many fits
            r1 = 0
            r1 += max(sum([C_list[m] == X1 for m in range(i+1, N) if m != j]) - c1, 0)
            r1 += max(sum([C_list_1[m] == X1 for m in range(i+1, N) if m != j]) - c1, 0)
            r1 += max(sum([C_list_2[m] == X1 for m in range(i+1, N) if m != j]) - c1, 0)
            r1 += max(sum([C_list_3[m] == X1 for m in range(i+1, N) if m != j]) - c1, 0)
            r2 = 0
            r2 += max(sum([C_list[m] == X2 for m in range(i+1, N) if m != j]) - c2, 0)
            r2 += max(sum([C_list_1[m] == X2 for m in range(i+1, N) if m != j]) - c2, 0)
            r2 += max(sum([C_list_2[m] == X2 for m in range(i+1, N) if m != j]) - c2, 0)
            r2 += max(sum([C_list_3[m] == X2 for m in range(i+1, N) if m != j]) - c2, 0)
            r3 = 0
            r3 += max(sum([C_list[m] == X3 for m in range(i+1, N) if m != j]) - c3, 0)
            r3 += max(sum([C_list_1[m] == X3 for m in range(i+1, N) if m != j]) - c3, 0)
            r3 += max(sum([C_list_2[m] == X3 for m in range(i+1, N) if m != j]) - c3, 0)
            r3 += max(sum([C_list_3[m] == X3 for m in range(i+1, N) if m != j]) - c3, 0)
            r4 = 0
            r4 += max(sum([C_list[m] == X4 for m in range(i+1, N) if m != j]) - c4, 0)
            r4 += max(sum([C_list_1[m] == X4 for m in range(i+1, N) if m != j]) - c4, 0)
            r4 += max(sum([C_list_2[m] == X4 for m in range(i+1, N) if m != j]) - c4, 0)
            r4 += max(sum([C_list_3[m] == X4 for m in range(i+1, N) if m != j]) - c4, 0)
            res += r1*r2*r3*r4
            
print res

Submission status: Wrong Answer

Solution code:
#from pprint import pprint
import collections
import itertools

def get_ltru(f, b):
    return [(b[2], b[1], f[0], f[3]),
            (b[1], b[0], f[1], f[0]),
            (b[0], b[3], f[2], f[1]),
            (b[3], b[2], f[3], f[2])]

def rotations(c):
    return set(tuple(c[i:] + c[:i]) for i in range(4))

def count_distict(s_list):
    sl = s_list
    if len(s_list) == 0:
        return 1
    if len(s_list) == 1:
        return len(s_list[0])
    if len(s_list) == 2:
        return len(s_list[0])*len(s_list[1]) - len(s_list[0]&s_list[1])
    if len(s_list) == 3:
        return len(s_list[0])*len(s_list[1])*len(s_list[2]) - len(s_list[0]&s_list[1])*len(sl[2]) - len(s_list[0]&s_list[2])*len(sl[1]) -len(s_list[1]&s_list[2])*len(sl[0])\
                + len(s_list[0]&s_list[1]&s_list[2])
    if len(sl) == 4:
        a,b,c,d = sl[0], sl[1], sl[2], sl[3]
        return len(a)*len(b)*len(c)*len(d)\
                - len(a&b)*len(c)*len(d)\
                - len(a&c)*len(b)*len(d)\
                - len(a&d)*len(b)*len(c)\
                - len(b&c)*len(a)*len(d)\
                - len(b&d)*len(a)*len(b)\
                - len(c&d)*len(a)*len(b)\
                + 2*len(a&b&c)*len(d)\
                + 2*len(a&b&d)*len(c)\
                + 2*len(a&c&d)*len(b)\
                + 2*len(b&c&d)*len(a)\
                + len(a&b)*len(c&d)\
                + len(a&c)*len(b&d)\
                + len(b&c)*len(a&d)\
                - 6*len(a&b&c&d)

N = int(raw_input())

C = [[int(s) for s in raw_input().split()] for i in range(N)]

r_to_c = {}

r_to_c = collections.defaultdict(set)


for i in range(N):
    for r in rotations(C[i]):
        r_to_c[r].add(i)
#pprint(dict(r_to_c))

ans = 0
for i,j in itertools.product(range(N), repeat=2):
    if i == j: continue
    f = C[i]
    multiplier = 4//len(rotations(C[j]))
    for b in rotations(C[j]):
        l,t,r,u = get_ltru(f,b)
        sym = {i:list() for i in [1,2,4]}
        for face in (l,t,r,u):
            sym[len(rotations(face))].append(face)
        #print(dict(sym))
        this_num = 1
        for n in [1,2,4]:
            this_num *= (4//n)**(len(sym[n]))*count_distict([r_to_c[face]-{i,j} for face in sym[n]])
        ans += this_num*multiplier
print(ans//6)

Submission status: Time Limit Exceeded

Solution code:
import sys
from collections import Counter
readline = sys.stdin.readline

Od = dict()
def order(k):
    if k in Od:
        return Od[k]
    else:
        res = 1
        a, b, c, d = k
        if a == b == c == d:
            res = 4
        elif a == c and b == d:
            res = 2
        Od[k] = res
        return res

El = Counter()
def ele(k):
    if k in El:
        res = El[k]
    else: 
        a, b, c, d = k
        res = min((a, b, c, d), (b, c, d, a), (c, d, a, b), (d, a, b, c))
        El[k] = res
    return res

limit = 5
calc = [[None]*limit for _ in range(limit*400)]
for i in range(limit*400):
    calc[i][1] = i
    for j in range(2, limit):
        calc[i][j] = calc[i][j-1]*(i-j+1)
pp = [[pow(i, j) for j in range(10)] for i in range(10)]

N = int(readline())
C = [tuple(map(int, readline().split())) for _ in range(N)]
D = Counter()

for i in range(N):
    D[ele(C[i])] += 1

ans = 0
for i in range(N):
    D[ele(C[i])] -= 1
    for j in range(i+1, N):
        D[ele(C[j])] -= 1
        
        a, b, c, d = C[i]
        e, f, g, h = C[j]
        for _ in range(4):
            e, f, g, h = h, e, f, g
            E = Counter()
            
            E[ele((b, e, h, c))] += 1
            E[ele((a, f, e, b))] += 1
            E[ele((d, g, f, a))] += 1
            E[ele((c, h, g, d))] += 1            
            res = 1
            
            for k, n in E.items():
                res *= calc[D[k]][n] * pp[order(k)][n]
            ans += res
        
        D[ele(C[j])] += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
from collections import Counter
readline = sys.stdin.readline

Od = dict()
def order(k):
    if k in Od:
        return Od[k]
    else:
        res = 1
        a, b, c, d = k
        if a == b == c == d:
            res = 4
        elif a == c and b == d:
            res = 2
        Od[k] = res
        return res

Dinv = Counter()
El = Counter()
def ele(k):
    if k in El:
        res = El[k]
    else: 
        a, b, c, d = k
        res = (a<<30) + (b<<20) + (c<<10) + d
        for _ in range(3):
            a, b, c, d = d, a, b, c
            res = min(res, (a<<30) + (b<<20) + (c<<10) + d)
        Dinv[res] = (a, b, c, d)
        El[k] = res
    return res

limit = 5
calc = [[None]*limit for _ in range(limit*400)]
for i in range(limit*400):
    calc[i][1] = i
    for j in range(2, limit):
        calc[i][j] = calc[i][j-1]*(i-j+1)
pp = [[pow(i, j) for j in range(10)] for i in range(10)]

N = int(readline())
C = [tuple(map(int, readline().split())) for _ in range(N)]
D = Counter()

for i in range(N):
    D[ele(C[i])] += 1

ans = 0
for i in range(N):
    D[ele(C[i])] -= 1
    for j in range(i+1, N):
        D[ele(C[j])] -= 1
        
        a, b, c, d = C[i]
        e, f, g, h = C[j]
        for _ in range(4):
            e, f, g, h = h, e, f, g
            E = Counter()
            
            E[ele((b, e, h, c))] += 1
            E[ele((a, f, e, b))] += 1
            E[ele((d, g, f, a))] += 1
            E[ele((c, h, g, d))] += 1            
            res = 1
            
            for k, n in E.items():
                res *= calc[D[k]][n] * pp[order(Dinv[k])][n]
            ans += res
        
        D[ele(C[j])] += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
from collections import Counter
readline = sys.stdin.readline

Od = dict()
def order(k):
    if k in Od:
        return Od[k]
    else:
        res = 1
        a, b, c, d = k
        if a == b == c == d:
            res = 4
        elif a == c and b == d:
            res = 2
        Od[k] = res
        return res

geta = 1000

Dinv = Counter()
El = Counter()
def ele(k):
    if k in El:
        res = El[k]
    else: 
        a, b, c, d = k
        res = a*geta**3 + b*geta**2 + c*geta + d
        for _ in range(3):
            a, b, c, d = d, a, b, c
            res = min(res, a*geta**3 + b*geta**2 + c*geta + d)
            Dinv[res] = (a, b, c, d)
        El[k] = res
    return res

def calc(k, n):
    od = order(Dinv[k])
    val = D[k]//4
    res = 1
    for i in range(n):
        res *= (val-i)
    return res*pow(od, n)

N = int(readline())

C = [tuple(map(int, readline().split())) for _ in range(N)]

D = Counter()

for i in range(N):
    D[ele(C[i])] += 4


ans = 0
for i in range(N):
    D[ele(C[i])] -= 4
    for j in range(i+1, N):
        D[ele(C[j])] -= 4
        
        a, b, c, d = C[i]
        e, f, g, h = C[j]
        for _ in range(4):
            e, f, g, h = h, e, f, g
            E = Counter()
            
            E[ele((b, e, h, c))] += 1
            E[ele((a, f, e, b))] += 1
            E[ele((d, g, f, a))] += 1
            E[ele((c, h, g, d))] += 1            
            res = 1
            for k, n in E.items():
                res *= calc(k, n)
            ans += res
        
        D[ele(C[j])] += 4

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
from collections import Counter
readline = sys.stdin.readline

def order(k):
    a, b, c, d = k
    if a == b == c == d:
        return 4
    if a == c and b == d:
        return 2
    return 1

geta = 1000

Dinv = Counter()
El = Counter()
def ele(k):
    if k in El:
        res = El[k]
    else: 
        a, b, c, d = k
        res = a*geta**3 + b*geta**2 + c*geta + d
        for _ in range(3):
            a, b, c, d = d, a, b, c
            res = min(res, a*geta**3 + b*geta**2 + c*geta + d)
            Dinv[res] = (a, b, c, d)
        El[k] = res
    return res

def calc(k, n):
    od = order(Dinv[k])
    val = D[k]//4
    res = 1
    for i in range(n):
        res *= (val-i)
    return res*pow(od, n)

N = int(readline())

C = [tuple(map(int, readline().split())) for _ in range(N)]

D = Counter()

for i in range(N):
    D[ele(C[i])] += 4


ans = 0
for i in range(N):
    D[ele(C[i])] -= 4
    for j in range(i+1, N):
        D[ele(C[j])] -= 4
        
        a, b, c, d = C[i]
        e, f, g, h = C[j]
        for _ in range(4):
            e, f, g, h = h, e, f, g
            E = Counter()
            
            E[ele((b, e, h, c))] += 1
            E[ele((a, f, e, b))] += 1
            E[ele((d, g, f, a))] += 1
            E[ele((c, h, g, d))] += 1            
            res = 1
            for k, n in E.items():
                res *= calc(k, n)
            ans += res
        
        D[ele(C[j])] += 4

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
from collections import Counter
readline = sys.stdin.readline

def order(k):
    a, b, c, d = k
    if a == b == c == d:
        return 4
    if a == c and b == d:
        return 2
    return 1

INF = 10**18
geta = 1000

Dinv = Counter()
def ele(k):
    a, b, c, d = k
    res = INF
    for _ in range(4):
        a, b, c, d = d, a, b, c
        res = min(res, a*geta**3 + b*geta**2 + c*geta + d)
        Dinv[res] = (a, b, c, d)
    return res

def calc(k, n):
    od = order(Dinv[k])
    val = D[k]//4
    res = 1
    for i in range(n):
        res *= (val-i)
    return res*od**n

N = int(readline())

C = [tuple(map(int, readline().split())) for _ in range(N)]

D = Counter()

for i in range(N):
    D[ele(C[i])] += 4


ans = 0
for i in range(N):
    D[ele(C[i])] -= 4
    for j in range(i+1, N):
        D[ele(C[j])] -= 4
        
        a, b, c, d = C[i]
        e, f, g, h = C[j]
        for _ in range(4):
            e, f, g, h = h, e, f, g
            E = Counter()
            
            E[ele((b, e, h, c))] += 1
            E[ele((a, f, e, b))] += 1
            E[ele((d, g, f, a))] += 1
            E[ele((c, h, g, d))] += 1            
            res = 1
            for k, n in E.items():
                res *= calc(k, n)
            ans += res
        
        D[ele(C[j])] += 4

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict
N, = map(int, input().split())

def normal(xs):
    return tuple(min((xs[j:] + xs[:j] for j in range(1, 5))))

dd = defaultdict(int)
cc = defaultdict(int)
ss = []
for _ in range(N):
    a, b, c, d = map(int, input().split())
    x = normal([a,b,c,d])
    n=1
    if x[0] == x[2] and x[1] == x[3]:
        n *= 2
        if x[0] == x[1]:
            n *= 2
    dd[x] += 1
    cc[x] = n
    ss.append(x)

def icr(x):
    dd[x] += 1

def dcr(x):
    dd[x] -= 1

def f(ff, gg):
    a,b,c,d=ff
    e,h,g,f=gg
    tl = list(map(normal, [(a,e,f,b), (b,f,g,c), (c,g,h,d), (d,h,e,a)]))
    for cp in tl:
        if cp not in dd:
            return 0
    r = 1
    for cp in tl:
        r *= dd[cp]*cc[cp]
        dcr(cp)
    for cp in tl:
        icr(cp)
    return r

r = 0
for i in range(N):
    ff = ss[i]
    dcr(ff)
    for j in range(i+1, N):
        sl = ss[j]
        dcr(sl)
        x, y, z, w = sl
        sls = [(x,y,z,w), (y,z,w,x), (z,w,x,y), (w,x,y,z)]
        for s in sls:
            r += f(ff, s)
        icr(sl)
    icr(ff)
print(r//3)

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict
N, = map(int, input().split())
dd = defaultdict(int)
ss = []
for _ in range(N):
    a, b, c, d = map(int, input().split())
    dd[(a,b,c,d)] += 1
    dd[(b,c,d,a)] += 1
    dd[(c,d,a,b)] += 1
    dd[(d,a,b,c)] += 1
    ss.append((a,b,c,d))
def icr(x):
    a, b, c, d = x
    for faf in [(b,c,d,a), (c,d,a,b), (d,a,b,c), (a,b,c,d)]:
        dd[faf] += 1
def dcr(x):
    a, b, c, d = x
    for faf in [(b,c,d,a), (c,d,a,b), (d,a,b,c), (a,b,c,d)]:
        dd[faf] -= 1

def f(ff, gg):
    a,b,c,d=ff
    e, h, g, f = gg
    tl = [(a,e,f,b), (b,f,g,c), (c,g,h,d), (d,h,e,a)]
    r = 1
    for cp in tl:
        r *= dd[cp]
        dcr(cp)
    for cp in tl:
        icr(cp)
    return r

r = 0
for i in range(N):
    a,b,c,d = ss[i]
    ff = (a,b,c,d)
    dcr(ff)
    for j in range(i+1, N):
        if i == j:
            continue
        x, y, z, w = ss[j]
        sl = [(x,y,z,w), (y,z,w,x), (z,w,x,y), (w,x,y,z)]
        for s in sl:
            dcr(s)
            r += f(ff,s)
            icr(s)
    icr(ff)
print(r//3)