danielvitor23

Ordinais ordinários

Jul 29th, 2023
977
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.84 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define fi first
  3. #define se second
  4. using namespace std;
  5.  
  6. using i64 = long long;
  7.  
  8. i64 binpow(i64 b, i64 e, i64 MOD) {
  9.   i64 res = 1;
  10.   while (e > 0) {
  11.     if (e & 1) res = (__int128_t)(res * b) % MOD;
  12.     b = (__int128_t)(b * b) % MOD;
  13.     e >>= 1;
  14.   }
  15.   return res;
  16. }
  17.  
  18. int main() {
  19.   cin.tie(0)->sync_with_stdio(0);
  20.  
  21.   i64 n, m; cin >> n >> m;
  22.  
  23.   if (n == 0) {
  24.     cout << 2 % m << '\n';
  25.     return 0;
  26.   }
  27.  
  28.   if (n == 1) {
  29.     cout << 4 % m << '\n';
  30.     return 0;
  31.   }
  32.  
  33.   cout << (i64)((__int128_t)5LL * binpow(2, n + 1 - 2, m) % m - 1 + m) % m << '\n';
  34. }
  35.  
  36. /*
  37.   a(n) = 5*2^(n-2) - 1
  38.  
  39.   f[0] = 2;
  40.   f[1] = 4;
  41.   f[2] = f[0] + f[1] + (2-1) + 2 = 2 + 4 + 1 + 2 = 9;
  42.   f[3] = f[0] + f[1] + f[2] + (3-1) + 2 = 2 + 4 + 9 + 2 + 2 = 19;
  43.   f[4] = f[0] + f[1] + f[2] + f[3] + (4-1) + 2 = 34 + 5 = 39;
  44. */
Advertisement
Add Comment
Please, Sign In to add comment