網路城邦
上一篇 回創作列表 下一篇   字體:
[Python] UVa 10229
2025/03/25 15:18:12瀏覽36|回應0|推薦1
#UVA_10229

#UVa 10229

#python

#

class Matrix:

    def __init__(self, a, b, c, d, mask):

        self.a, self.b, self.c, self.d = a, b, c, d

        self.mask = mask


    def __mul__(self, other): # operator *

        return Matrix(

            (self.a * other.a + self.b * other.c) & self.mask,

            (self.a * other.b + self.b * other.d) & self.mask,

            (self.c * other.a + self.d * other.c) & self.mask,

            (self.c * other.b + self.d * other.d) & self.mask,

            self.mask

        )


n1=0

m1=0

while True:

    try:

        LL= list(map(int, input().strip().split()))

        if len(LL)==2:

            n, m = LL

            n1=0

            m1=0

        if len(LL)==4:

            n, m, n1, m1 = LL

             

        #n, m, n1, m1= map(int, input().strip().split())

    except EOFError:

        break

        

    M = Matrix(1, 1, 1, 0, (1 << m )- 1)

    X = Matrix(1, 0, 0, 0, (1 << m )- 1)

    while n: # exponentiation by squaring

        if n & 1:

            X = X * M

        M = M * M

        n >>= 1

    print(X.b)

    if (n1 !=0) and (m1 != 0):

        n=n1

        m=m1

        n1=0

        m1=0

        M = Matrix(1, 1, 1, 0, (1 << m )- 1)

        X = Matrix(1, 0, 0, 0, (1 << m )- 1)

        while n: # exponentiation by squaring

            if n & 1:

                X = X * M

            M = M * M

            n >>= 1

        print(X.b)

   
( 知識學習考試升學 )
回應 推薦文章 列印 加入我的文摘
上一篇 回創作列表 下一篇

引用
引用網址:https://classic-blog.udn.com/article/trackback.jsp?uid=aabbcc1&aid=182088710