Advertisement
t_a_w

easyctf library

Mar 16th, 2017
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.14 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. mod = (10**9+7)
  4.  
  5. class vec:
  6.   def __init__(self,a1,a2):
  7.     self.a1 = a1 % mod
  8.     self.a2 = a2 % mod
  9.  
  10. class mat:
  11.   def __init__(self,a11,a12,a21,a22):
  12.     self.a11 = a11 % mod
  13.     self.a12 = a12 % mod
  14.     self.a21 = a21 % mod
  15.     self.a22 = a22 % mod
  16.   def __mul__(self, other):
  17.     if type(other) is mat:
  18.       return mat(
  19.         self.a11 * other.a11 + self.a12 * other.a21, # c_11
  20.         self.a11 * other.a12 + self.a12 * other.a22, # c_12
  21.         self.a21 * other.a11 + self.a22 * other.a21, # c_21
  22.         self.a21 * other.a12 + self.a22 * other.a22, # c_22
  23.       )
  24.     else:
  25.       return vec(
  26.         self.a11 * other.a1 + self.a12 * other.a2,
  27.         self.a21 * other.a1 + self.a22 * other.a2,
  28.       )
  29.   def __pow__(self, n):
  30.     if n == 0:
  31.       return mat(1,0,0,1)
  32.     elif n == 1:
  33.       return self
  34.     else:
  35.       a = mat(1,0,0,1)
  36.       b = self
  37.       while n > 1:
  38.         if n%2 == 1:
  39.           a = a * b
  40.         b = b * b
  41.         n //= 2
  42.       return a*b
  43.  
  44. def library(n):
  45.   m = mat(1,1,2,1) ** n
  46.   v = m * vec(1, 0)
  47.   return (v.a1 + v.a2) % mod
  48.  
  49. n = int(input())
  50. print(library(n))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement