白羽啊


私信TA

用户名:723822380

访问量:2781

签 名:

等  级
排  名 4393
经  验 1642
参赛次数 0
文章发表 22
年  龄 0
在职情况 学生
学  校 泉师
专  业

  自我简介:

解题思路:

观察A和B的规律:A只能由两个相同字母得到,B只能由两个不同字母得到->等价于异或中的运算规则

1^0=1,0^1=1,0^0=1,1^1=0.即A=0,B=1

再看摆放条件,是下层决定上层,所以先摆放最底层的A和B,再决定上层如何摆放,重复这个过程直至所有的A和B被摆放完,最后看是否所有层都摆满,若摆满了说明最底层的摆放可行,计数+1

注意事项:

参考代码:

import itertools, collections

m, n = map(int, input().strip().split())
sum_up = m + n
level = 0
while sum_up: # 求出层数,构成三角形的总数刚好是等差数列的和
    level += 1
    sum_up -= level

count = 0
for i in itertools.product(range(2), repeat = level):#利用product生成最底层01(AB)的全排列
    temp_i = list(i)
    temp = collections.Counter(temp_i)#统计所需的0和1的个数
    temp_m = m - temp[0] # 剩余A的个数
    temp_n = n - temp[1] # 剩余B的个数
    temp_level = level - 1 # 从第二层开始(第一层摆好了)
    while (temp_m > 0 or temp_n > 0) and temp_level > 0:
        temp_j = []
        j = 0
        while j < temp_level:
            cur = temp_i[j] ^ temp_i[j + 1]
            temp_j.append(cur)
            if int(cur) == 0:
                temp_m -= 1
            else:
                temp_n -= 1
            j += 1
        temp_i = temp_j #更新底层的排列
        temp_level -= 1
    if temp_level == 0 and temp_m == 0 and temp_n == 0:
        count += 1

print(count)


 

0.0分

1 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换

万能编程问答  

代码解释器

代码纠错

SQL生成与解释

  评论区