多多君拼团购买了N个骰子,为了方便后面进行活动,多多君需要将这些骰子进行分类。
两个骰子为同类的定义是:
将其中一个骰子通过若干次上下、左右或前后翻转后,其与另一个骰子对应的6面数字均相等。 将其中一个骰子通过若干次上下、左右或前后翻转后,其与另一个骰子对应的6面数字均相等。 第一行1个整数N,表示骰子的数量。(1 <= N <= 1,000)接下来N行,每行6个数字(1~6,且各不相同)其中第i行表示第i个骰子当前上、下、左、右、前、后这6面的数字。
共2行:
第一行1个整数M,表示不同种类的骰子的个数
第二行M个整数,由大到小排序,表示每个种类的骰子的数量
2 1 2 3 4 5 6 1 2 6 5 3 4
1 2
第二个骰子相当于是第一个骰子从左向右旋转了一面得到,属于同类。
3 1 2 3 4 5 6 1 2 6 5 3 4 1 2 3 4 6 5
2 2 1
第三个骰子无法通过任何旋转变换成第一个或第二个骰子。
10 2 5 1 3 4 6 5 4 3 2 1 6 1 4 6 2 3 5 1 5 6 3 4 2 6 4 2 1 5 3 3 6 4 5 2 1 1 6 3 4 2 5 5 1 4 2 6 3 6 2 3 1 5 4 5 3 6 1 4 2
9 2 1 1 1 1 1 1 1 1
只有第4个骰子(1 5 6 3 4 2)与第8个骰子(5 1 4 2 6 3)属于同一类。一种可能的变换方式:1) 首先从右向左翻转1次
(1 5 6 3 4 2) -> (1 5 4 2 3 6)
2) 然后从上向下翻转2次
(1 5 4 2 3 6) -> (6 3 4 2 1 5) -> (5 1 4 2 6 3)
对于50%的数据,有: 1 <= N <= 50对于100%的数据,有:1 <= N <= 1,000
import sys
from collections import defaultdict
def validate_dice(dice):
"""验证骰子的6个面是否包含1到6且不重复"""
return sorted(dice) == [1, 2, 3, 4, 5, 6]
class Dice:
def __init__(self, state):
"""初始化骰子状态"""
if not validate_dice(state):
raise ValueError("Invalid dice state: must contain numbers 1 to 6 without duplicates.")
self.state = list(state)
self.normalize()
def normalize(self):
"""将骰子标准化:1朝上,最小数朝前"""
# 将1旋转到上面
pos_1 = self.state.index(1)
if pos_1 == 1:
self._rotate_vertical()
self._rotate_vertical()
elif pos_1 == 2:
self._rotate_vertical()
elif pos_1 == 3:
self._rotate_vertical()
self._rotate_vertical()
self._rotate_vertical()
elif pos_1 == 4:
self._rotate_horizontal()
self._rotate_vertical()
elif pos_1 == 5:
self._rotate_horizontal()
self._rotate_vertical()
self._rotate_vertical()
self._rotate_vertical()
# 将最小数旋转到前面
min_number = min(self.state[2:])
min_number_index = self.state.index(min_number)
if min_number_index == 3:
self._rotate_horizontal()
self._rotate_horizontal()
elif min_number_index == 4:
self._rotate_horizontal()
elif min_number_index == 5:
self._rotate_horizontal()
self._rotate_horizontal()
self._rotate_horizontal()
def _rotate_vertical(self):
"""绕垂直轴旋转骰子"""
self.state[0], self.state[1], self.state[2], self.state[3] = (
self.state[2], self.state[3], self.state[1], self.state[0]
)
def _rotate_horizontal(self):
"""绕水平轴旋转骰子"""
self.state[2], self.state[3], self.state[4], self.state[5] = (
self.state[4], self.state[5], self.state[3], self.state[2]
)
def main():
# 读取输入
num_dices = int(sys.stdin.readline().strip())
dices = []
for _ in range(num_dices):
dice_state = tuple(map(int, sys.stdin.readline().strip().split()))
if not validate_dice(dice_state):
raise ValueError("Invalid dice state: must contain numbers 1 to 6 without duplicates.")
dices.append(dice_state)
# 统计骰子类别
dice_count = defaultdict(int)
for dice_state in dices:
dice = Dice(dice_state)
bottom, right, front = dice.state[1], dice.state[3], dice.state[4]
dice_count[(bottom, right, front)] += 1
# 输出结果
num_categories = len(dice_count)
counts = sorted(dice_count.values(), reverse=True)
print(num_categories)
print(' '.join(map(str, counts)))
if __name__ == "__main__":
main() class dice():
def __init__(self, a):
self.a=a
def switch_out(self,i,j):
if i!=j:
self.a[2*i], self.a[2*i+1], self.a[2*j], self.a[2*j+1] = \
self.a[2*j], self.a[2*j+1], self.a[2*i+1], self.a[2*i]
def switch_in(self,i):
if self.a[2*i]>self.a[2*i+1]:
self.a[2*i], self.a[2*i+1] = self.a[2*i+1], self.a[2*i]
self.a[2*i+2], self.a[2*i+3] = self.a[2*i+3], self.a[2*i+2]
def tidy(self):
i1=self.a.index(1)//2
self.switch_out(0, i1)
self.switch_in(0)
i2=self.a.index(min(self.a[2:]))//2
self.switch_out(1, i2)
self.switch_in(1)
return tuple(self.a)
n=int(input())
dc={}
for i in range(n):
nd=dice(list(map(int, input().split()))).tidy()
dc[nd]=dc.get(nd, 0)+1
print(len(dc.keys()))
for v in sorted(dc.values(), reverse=True):
print(v, end=" ")