字體:小 中 大 |
|
|
|
| 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)
|
|
| ( 知識學習|考試升學 ) |











