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: 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(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//6)

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict
N, = map(int, input().split())
dd = defaultdict(int)
cc = 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
    if a == c and b == d:
        if a == b:
            cc[(a,b,c,d)] = 4
        else:
            cc[(a,b,c,d)] = 2
            cc[(b,c,d,a)] = 2
    else:
        cc[(a,b,c,d)] = 1
        cc[(b,c,d,a)] = 1
        cc[(c,d,a,b)] = 1
        cc[(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(a, b, c, d, e, f, g, h):
    e, f, g, h = e, h, g, f
    tl = [(a,e,f,b), (b,f,g,c), (c,g,h,d), (d,h,e,a)]
    r = 1
    coun = 0
    for cp in tl:
        coun += 1
#        if (a,b,c,d) == (1, 1, 1, 1) and (e,f,g,h) == (0,0,0,0):
#        if (a,b,c,d) == (1, 0, 1, 0) and (e,f,g,h) == (1,0,1,0):
#            print(coun, dd, dd[cp])
        #print(coun, dd, dd[cp])
        r *= dd[cp]
        dcr(cp)
    for cp in tl:
        icr(cp)
    #print((a,b,c,d), (e,f,g,h), r)
    return r

r = 0
for i in range(N):
    a,b,c,d = ss[i]
    ff = [a,b,c,d]
    dcr(tuple(ff))
    for j in range(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(tuple(s))
            r += f(*(ff+s))
            icr(tuple(s))
    icr(tuple(ff))
print(r//6)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
from collections import defaultdict, Counter
import itertools

mask = (1 << 10) - 1

symmetry = dict()
decode = dict()
counter = defaultdict(int)

def encode(a,b,c,d):
    t = 1 << 40
    for _ in range(4):
        a,b,c,d = b,c,d,a
        x = a + (b << 10) + (c << 20) + (d << 30)
        if t > x:
            t = x
    return t

N = int(input())
tiles = []
for _ in range(N):
    a,b,c,d = map(int,input().split())
    t = encode(a,b,c,d)
    tiles.append(t)
    counter[t] += 1
    if a == b == c == d:
        symmetry[t] = 4
    elif a == c and b == d:
        symmetry[t] = 2
    else:
        symmetry[t] = 1
    decode[t] = (a,b,c,d)

P = [(1,n,n*(n-1),n*(n-1)*(n-2),n*(n-1)*(n-2)*(n-3)) for n in range(N+1)]

power = [[n ** e for e in range(5)] for n in range(5)]

answer = 0
for bottom, top in itertools.combinations(tiles,2):
    counter[bottom] -= 1
    counter[top] -= 1
    a,b,c,d = decode[bottom]
    e,f,g,h = decode[top]
    for _ in range(4):
        e,f,g,h = f,g,h,e
        # a,b,c,d
        # e,h,g,f 
        tiles = [encode(p,q,r,s) for p,q,r,s in [(b,a,e,h), (c,b,h,g), (d,c,g,f), (a,d,f,e)]]
        need = Counter(tiles)
        x = 1
        for tile, cnt in need.items():
            have = counter[tile]
            if have < cnt:
                x = 0
                break
            x *= P[counter[tile]][cnt] # 残っている個数、必要枚数
            x *= power[symmetry[tile]][cnt]
        answer += x
    counter[bottom] += 1
    counter[top] += 1

# 上下を固定した分
answer //= 3
print(answer)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
from collections import defaultdict, Counter
import itertools

mask = (1 << 10) - 1

symmetry = dict()
decode = dict()
counter = defaultdict(int)

def encode(a,b,c,d):
    t = 1 << 40
    for x,y,z,w in [(a,b,c,d), (b,c,d,a), (c,d,a,b), (d,a,b,c)]:
        u = x + (y << 10) + (z << 20) + (w << 30)
        if t > u:
            t = u
    return t

N = int(input())
tiles = []
for _ in range(N):
    a,b,c,d = map(int,input().split())
    t = encode(a,b,c,d)
    tiles.append(t)
    counter[t] += 1
    if a == b == c == d:
        symmetry[t] = 4
    elif a == c and b == d:
        symmetry[t] = 2
    else:
        symmetry[t] = 1
    decode[t] = (a,b,c,d)

P = [(1,n,n*(n-1),n*(n-1)*(n-2),n*(n-1)*(n-2)*(n-3)) for n in range(N+1)]
P += [(0,0,0,0,0)] * (N+1) # 負数代入対策

power = [[n ** e for e in range(5)] for n in range(5)]

answer = 0
for bottom, top in itertools.combinations(tiles,2):
    counter[bottom] -= 1
    counter[top] -= 1
    a,b,c,d = decode[bottom]
    e,f,g,h = decode[top]
    for x,y,z,w in [(e,f,g,h), (f,g,h,e), (g,h,e,f), (h,e,f,g)]:
        # a,b,c,d
        # x,w,z,y 
        tiles = [encode(p,q,r,s) for p,q,r,s in [(b,a,x,w), (c,b,w,z), (d,c,z,y), (a,d,y,x)]]
        need = Counter(tiles)
        x = 1
        for tile, cnt in need.items():
            have = counter[tile]
            if have < cnt:
                x = 0
                break
            x *= P[counter[tile]][cnt] # 残っている個数、必要枚数
            x *= power[symmetry[tile]][cnt]
        answer += x
    counter[bottom] += 1
    counter[top] += 1

# 上下を固定した分
answer //= 3
print(answer)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
from collections import Counter
import itertools
from functools import lru_cache

mask = (1 << 10) - 1

@lru_cache(None)
def encode(a,b,c,d):
    t = 1 << 40
    for x,y,z,w in [(a,b,c,d), (b,c,d,a), (c,d,a,b), (d,a,b,c)]:
        u = x + (y << 10) + (z << 20) + (w << 30)
        if t > u:
            t = u
    return t

@lru_cache(None)
def decode(x):
    return [(x >> n) &mask for n in [0,10,20,30]]

@lru_cache(None)
def P(n,k):
    x = 1
    for i in range(k):
        x *= (n-i)
    return x

@lru_cache(None)
def symmetry(tile):
    a,b,c,d = decode(tile)
    if a == b == c == d:
        return 4
    if a == c and b == d:
        return 2
    return 1

N = int(input())
tiles = []
for _ in range(N):
    a,b,c,d = map(int,input().split())
    tiles.append(encode(a,b,c,d))
counter = Counter(tiles)

answer = 0
for bottom, top in itertools.combinations(tiles,2):
    counter[bottom] -= 1
    counter[top] -= 1
    a,b,c,d = decode(bottom)
    e,f,g,h = decode(top)
    for x,y,z,w in [(e,f,g,h), (f,g,h,e), (g,h,e,f), (h,e,f,g)]:
        # a,b,c,d
        # x,w,z,y 
        tiles = [encode(p,q,r,s) for p,q,r,s in [(b,a,x,w), (c,b,w,z), (d,c,z,y), (a,d,y,x)]]
        need = Counter(tiles)
        x = 1
        for tile, cnt in need.items():
            x *= P(counter[tile],cnt) # 残っている個数、必要枚数
            x *= pow(symmetry(tile), cnt)
        answer += x
    counter[bottom] += 1
    counter[top] += 1

# 上下を固定した分
answer //= 3
print(answer)

Submission status: Time Limit Exceeded

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

mask = (1 << 10) - 1

def encode(a,b,c,d):
    t = 1 << 40
    for x,y,z,w in [(a,b,c,d), (b,c,d,a), (c,d,a,b), (d,a,b,c)]:
        u = x + (y << 10) + (z << 20) + (w << 30)
        if t > u:
            t = u
    return t

def decode(x):
    for _ in range(4):
        yield x & mask
        x >>= 10

def P(n,k):
    x = 1
    for i in range(k):
        x *= (n-i)
    return x

def symmetry(tile):
    a,b,c,d = decode(tile)
    if a == b == c == d:
        return 4
    if a == c and b == d:
        return 2
    return 1

N = int(input())
tiles = []
for _ in range(N):
    a,b,c,d = map(int,input().split())
    tiles.append(encode(a,b,c,d))
counter = Counter(tiles)

counter

answer = 0
for bottom, top in itertools.combinations(tiles,2):
    counter[bottom] -= 1
    counter[top] -= 1
    a,b,c,d = decode(bottom)
    e,f,g,h = decode(top)
    for x,y,z,w in [(e,f,g,h), (f,g,h,e), (g,h,e,f), (h,e,f,g)]:
        # a,b,c,d
        # x,w,z,y 
        tiles = [encode(p,q,r,s) for p,q,r,s in [(b,a,x,w), (c,b,w,z), (d,c,z,y), (a,d,y,x)]]
        need = Counter(tiles)
        x = 1
        for tile, cnt in need.items():
            x *= P(counter[tile],cnt) # 残っている個数、必要枚数
            x *= pow(symmetry(tile), cnt)
        answer += x
    counter[bottom] += 1
    counter[top] += 1

# 上下を固定した分
answer //= 3
print(answer)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3


P3 = 2 ** 30
P2 = 2 ** 20

def get_min(c):
    m = c
    for _ in range(3):
        c = c // 1024 + (c % 1024) * P3
        m = min(m, c)

    return m


def solve(n, ca, colors):

    ans = 0
    for i in range(n - 5):
        ci = ca[i]
        colors[ci] -= 1
        for j in range(i + 1, n):
            cj = ca[j]
            colors[cj] -= 1
            for _ in range(4):
                nd = {}
                c2 = (cj // P2 % 1024) * P3 + (cj // P3) * P2 + (ci // P2 % 1024) * 1024 + ci // P3
                c2 = get_min(c2)
                nd[c2] = 1
                c3 = (ci % 1024) * P3 + (ci // 1024 % 1024) * P2 + (cj % 1024) * 1024 + cj // 1024 % 1024
                c3 = get_min(c3)
                if c3 == c2:
                    nd[c3] += 1
                else:
                    nd[c3] = 1
                c4 = (cj // P2 % 1024) * P3 + (ci // P3 % 1024) * P2 + (ci % 1024) * 1024 + cj // 1024 % 1024
                c4 = get_min(c4)
                if c4 == c2 or c4 == c3:
                    nd[c4] += 1
                else:
                    nd[c4] = 1
                c5 = (ci // P2 % 1024) * P3 + (cj // P3 % 1024) * P2 + (cj % 1024) * 1024 + ci // 1024 % 1024
                c5 = get_min(c5)
                if c5 == c2 or c5 == c3 or c5 == c4:
                    nd[c5] += 1
                else:
                    nd[c5] = 1
                r = 1
                for c, k in nd.items():
                    if not c in colors:
                        r = 0
                        break
                    h = colors[c]
                    if h < k:
                        r = 0
                        break
                    if c // P2 == c % P2:
                        rot = 2
                        if c // P3 == c // P2 % 1024 and c // 1024 % 1024 == c % 1024:
                            rot = 4
                    else:
                        rot = 1
                    while 0 < k:
                        r *= h * rot
                        h -= 1
                        k -= 1
                ans += r
                cj = cj // 1024 + (cj % 1024) * P3
            colors[cj] += 1

    return ans


def main():
    n = input()
    n = int(n)
    colors = {}
    ca = [-1] * n
    for i in range(n):
        c0, c1, c2, c3 = input().split()
        c0 = int(c0)
        c1 = int(c1)
        c2 = int(c2)
        c3 = int(c3)
        c = c0 * P3 + c1 * P2 + c2 * 1024 + c3
        m = get_min(c)
        ca[i] = m
        if m in colors:
            colors[m] += 1
        else:
            colors[m] = 1


    print(solve(n, ca, colors))


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3


def get_min(c):
    m = c
    for _ in range(3):
        c = c // 1024 + (c % 1024) * 2 ** 30
        m = min(m, c)

    return m


def solve(n, ca, colors):

    ans = 0
    for i in range(n - 5):
        ci = ca[i]
        colors[ci] -= 1
        for j in range(i + 1, n):
            cj = ca[j]
            colors[cj] -= 1
            for _ in range(4):
                nd = {}
                c2 = (cj // 2 ** 20 % 1024) * 2 ** 30 + (cj // 2 ** 30) * 2 ** 20 + (ci // 2 ** 20 % 1024) * 2 ** 10 + ci // 2 ** 30
                c2 = get_min(c2)
                nd[c2] = 1
                c3 = (ci % 1024) * 2 ** 30 + (ci // 1024 % 1024) * 2 ** 20 + (cj % 1024) * 2 ** 10 + cj // 1024 % 1024
                c3 = get_min(c3)
                if c3 in nd:
                    nd[c3] += 1
                else:
                    nd[c3] = 1
                c4 = (cj // 2 ** 20 % 1024) * 2 ** 30 + (ci // 2 ** 30 % 1024) * 2 ** 20 + (ci % 1024) * 2 ** 10 + cj // 1024 % 1024
                c4 = get_min(c4)
                if c4 in nd:
                    nd[c4] += 1
                else:
                    nd[c4] = 1
                c5 = (ci // 2 ** 20 % 1024) * 2 ** 30 + (cj // 2 ** 30 % 1024) * 2 ** 20 + (cj % 1024) * 2 ** 10 + ci // 1024 % 1024
                c5 = get_min(c5)
                if c5 in nd:
                    nd[c5] += 1
                else:
                    nd[c5] = 1
                r = 1
                for c, k in nd.items():
                    if not c in colors:
                        r = 0
                        break
                    h = colors[c]
                    if h < k:
                        r = 0
                        break
                    if c // 2 ** 20 == c % 2 ** 20:
                        rot = 2
                        if c // 2 ** 30 == c // 2 ** 20 % 1024 and c // 2 ** 10 % 1024 == c % 1024:
                            rot = 4
                    else:
                        rot = 1
                    while 0 < k:
                        r *= h * rot
                        h -= 1
                        k -= 1
                ans += r
                cj = cj // 1024 + (cj % 1024) * 2 ** 30
            colors[cj] += 1

    return ans


def main():
    n = input()
    n = int(n)
    colors = {}
    ca = [-1] * n
    for i in range(n):
        c0, c1, c2, c3 = input().split()
        c0 = int(c0)
        c1 = int(c1)
        c2 = int(c2)
        c3 = int(c3)
        c = c0 * 2 ** 30 + c1 * 2 ** 20 + c2 * 2 ** 10 + c3
        m = get_min(c)
        ca[i] = m
        if m in colors:
            colors[m] += 1
        else:
            colors[m] = 1


    print(solve(n, ca, colors))


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
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)
    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: Time Limit Exceeded

Solution code:
from collections import Counter
def cs2int(cs):
    ts = [sum(cs[i] * 1000**((-i+k)%4) for i in range(4)) for k in range(4)]
    return min(ts)
def int2cs(i):
    return (i//1000000000, (i//1000000)%1000, (i//1000)%1000, i%1000)
def dup(cs):
    return 4 if cs[0]==cs[1]==cs[2]==cs[3] else 2 if cs[0]==cs[2] and cs[1]==cs[3] else 1

if __name__=="__main__":
    n = int(input())
    bag = Counter(cs2int(tuple(map(int, input().split()))) for _ in range(n))
    s = 0
    for top, i in bag.items():
        top_c = int2cs(top)
        for bottom, j in bag.items():
            bottom_c = int2cs(bottom)
            for k in range(4):
                front = cs2int((top_c[3], top_c[2], bottom_c[(1+k)%4], bottom_c[k]))
                right = cs2int((top_c[2], top_c[1], bottom_c[(2+k)%4], bottom_c[(1+k)%4]))
                back = cs2int((top_c[1], top_c[0], bottom_c[(3+k)%4], bottom_c[(2+k)%4]))
                left = cs2int((top_c[0], top_c[3], bottom_c[k], bottom_c[(3+k)%4]))
                temp = i*(j-(top==bottom))
                temp *= bag[front]- (front==top) - (front==bottom)
                temp *= bag[right] - (right==top) - (right==bottom) - (right==front)
                temp *= bag[back] - (back==top) - (back==bottom) - (back==front) - (back==right)
                temp *= bag[left] - (left==top) - (left==bottom) - (left==front) - (left==right) - (left==back)
                temp *= dup(int2cs(front)) * dup(int2cs(right)) * dup(int2cs(back)) * dup(int2cs(left))
                s += temp
    print(s//6)

Submission status: Time Limit Exceeded

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]]
    

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 duplicate
            if X1 == rotate(X1, 1):
                k1 = 4
            elif X1 == rotate(X1, 2):
                k1 = 2
            else:
                k1 = 1
            if X2 == rotate(X2, 1):
                k2 = 4
            elif X2 == rotate(X2, 2):
                k2 = 2
            else:
                k2 = 1
            if X3 == rotate(X3, 1):
                k3 = 4
            elif X3 == rotate(X3, 2):
                k3 = 2
            else:
                k3 = 1
            if X4 == rotate(X4, 1):
                k4 = 4
            elif X4 == rotate(X4, 2):
                k4 = 2
            else:
                k4 = 1
            # count how many fits
            r1 = max(sum([C_list[m] in [rotate(X1, l) for l in range(4)] for m in range(i+1, N) if m != j]) - c1, 0)
            r2 = max(sum([C_list[m] in [rotate(X2, l) for l in range(4)] for m in range(i+1, N) if m != j]) - c2, 0)
            r3 = max(sum([C_list[m] in [rotate(X3, l) for l in range(4)] for m in range(i+1, N) if m != j]) - c3, 0)
            r4 = max(sum([C_list[m] in [rotate(X4, l) for l in range(4)] for m in range(i+1, N) if m != j]) - 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

Submission status: Accepted

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

def compress(L):
    L2 = list(set(L))
    L2.sort()
    C = {v : k for k, v in enumerate(L2, 1)}
    return L2, C

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


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()



Rot = []
for i in range(N):
    a, b, c, d = C[i]
    Rot.append((a, b, c, d))
    Rot.append((d, a, b, c))
    Rot.append((c, d, a, b))
    Rot.append((b, c, d, a))
Lc, Cr = compress(Rot)
Lc = [None] + Lc
Cc = []

Od = Counter()
Base = Counter()
D = Counter()
for i in range(N):
    a, b, c, d = C[i]
    a, b, c, d = min((a, b, c, d), (b, c, d, a), (c, d, a, b), (d, a, b, c))
    od = order(a, b, c, d)
    r1 = Cr[(a, b, c, d)]
    r2 = Cr[(b, c, d, a)]
    r3 = Cr[(c, d, a, b)]
    r4 = Cr[(d, a, b, c)]
    Base[r1] = r1
    Base[r2] = r1
    Base[r3] = r1
    Base[r4] = r1
    Od[r1] = od
    Od[r2] = od
    Od[r3] = od
    Od[r4] = od
    Cc.append((r1, r2, r3, r4))
    D[r1] += 1


ans = 0
for i in range(N):
    D[Cc[i][0]] -= 1
    a, b, c, d = Lc[Cc[i][0]]
    for j in range(i+1, N):
        D[Cc[j][0]] -= 1
        for idx in range(4):
            e, f, g, h = Lc[Cc[j][idx]]
            E = Counter()
            r1 = (b, e, h, c)
            if r1 not in Cr:
                continue
            r1 = Base[Cr[r1]]
            r2 = (a, f, e, b)
            if r2 not in Cr:
                continue
            r2 = Base[Cr[r2]]
            r3 = (d, g, f, a)
            if r3 not in Cr:
                continue
            r3 = Base[Cr[r3]]
            r4 = (c, h, g, d)
            if r4 not in Cr:
                continue
            r4 = Base[Cr[r4]]
            
            E[r1] += 1
            E[r2] += 1
            E[r3] += 1
            E[r4] += 1            
            res = 1
            for k, n in E.items():
                res *= calc[D[k]][n] * pp[Od[k]][n]
            ans += res
        
        D[Cc[j][0]] += 1

print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict
N, = map(int, input().split())
dd = defaultdict(int)
cc = dict()
nrm = dict()
ss = []
for _ in range(N):
  xs = list(map(int, input().split()))
  cnd = [tuple(xs[j:]+xs[:j])for j in range(4)]
  x = min(cnd)
  for item in cnd:
    nrm[item] = x
  dd[x] += 1
  cc[x] = (4 if x[0]==x[1]else 2)if x[0]==x[2]and x[1]==x[3]else 1
  ss.append(x)
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)]
  q = defaultdict(int)
  for p in tl:
    if p not in nrm:
      return 0
    q[nrm[p]] += 1
  r= 1
  for p, c in q.items():
    for i in range(c):
      r *= dd[p]-i
    r *= cc[p]**c
  return r
r = 0
for i in range(N):
  ff = ss[i]
  dd[ff]-=1
  for j in range(i+1, N):
    sl = ss[j]
    x, y, z, w = sl
    dd[sl]-=1
    r += sum(f(ff,tuple(sl[j:]+sl[:j]))for j in range(4))
    dd[sl]+=1
print(r)

Submission status: Accepted

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

dd = defaultdict(int)
cc = dict()
norm = dict()
ss = []
for _ in range(N):
    xs = list(map(int, input().split()))
    cnd = [tuple(xs[j:] + xs[:j]) for j in range(1, 5)]
    x = min(cnd)
    for item in cnd:
        norm[item] = x
    dd[x] += 1
    cc[x] = (4 if x[0] == x[1] else 2)if x[0] == x[2] and x[1] == x[3] else 1
    ss.append(x)

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)]
    for i in range(4):
        if tl[i] not in norm:
            return 0
        tl[i] = norm[tl[i]]
    r = 1
    for cp in tl:
        r *= dd[cp]*cc[cp]
        dd[cp] -= 1
    for cp in tl:
        dd[cp] += 1
    return r

r = 0
for i in range(N):
    ff = ss[i]
    dd[ff]-=1
    for j in range(i+1, N):
        sl = ss[j]
        x, y, z, w = sl
        dd[sl]-=1
        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)
        dd[sl]+=1
print(r)

Submission status: Accepted

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

dd = defaultdict(int)
cc = dict()
norm = dict()
ss = []
for _ in range(N):
    xs = list(map(int, input().split()))
    cnd = [tuple(xs[j:] + xs[:j]) for j in range(1, 5)]
    x = min(cnd)
    for item in cnd:
        norm[item] = x
    dd[x] += 1
    cc[x] = (4 if x[0] == x[1] else 2)if x[0] == x[2] and x[1] == x[3] else 1
    ss.append(x)

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)]
    for i in range(4):
        if tl[i] not in norm:
            return 0
        tl[i] = norm[tl[i]]
    r = 1
    for cp in tl:
        r *= dd[cp]*cc[cp]
        dd[cp] -= 1
    for cp in tl:
        dd[cp] += 1
    return r

r = 0
for i in range(N):
    ff = ss[i]
    dd[ff]-=1
    for j in range(i+1, N):
        sl = ss[j]
        x, y, z, w = sl
        dd[sl]-=1
        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)
        dd[sl]+=1
    dd[ff]+=1
print(r//3)

Submission status: Accepted

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 = dict()
norm = dict()
ss = []
for _ in range(N):
    xs = list(map(int, input().split()))
    cnd = [tuple(xs[j:] + xs[:j]) for j in range(1, 5)]
    x = min(cnd)
    for item in cnd:
        norm[item] = x
    dd[x] += 1
    cc[x] = (4 if x[0] == x[1] else 2)if x[0] == x[2] and x[1] == x[3] else 1
    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 = [(a,e,f,b), (b,f,g,c), (c,g,h,d), (d,h,e,a)]
    for cp in tl:
        if cp not in norm:
            return 0
    r = 1
    for cp in tl:
        cp = norm[cp]
        r *= dd[cp]*cc[cp]
        dcr(cp)
    for cp in tl:
        cp = norm[cp]
        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: Accepted

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
    if xs[(xi+1)%4] > xs[(xi+3)%4]:
        xi = (xi+2)%4
    return (xs[xi%4], xs[(xi+1)%4], xs[(xi+2)%4], xs[(xi+3)%4])
#x = (0,2,2,0)
#print(normal(x))

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):
    #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
    for cp in tl:
        if cp not in dd:
            r = 0
            break
        r *= dd[cp]*cc[cp]
        dcr(cp)
    for cp in tl:
        if cp not in dd:
            break
        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)