Advertisement
Malinovsky239

Untitled

Sep 25th, 2012
1,787
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.73 KB | None | 0 0
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <cstdlib>
  4. #include <cstring>
  5. #include <cassert>
  6. #include <cstdio>
  7. #include <vector>
  8. #include <cctype>
  9. #include <string>
  10. #include <ctime>
  11. #include <cmath>
  12. #include <set>
  13. #include <map>
  14.  
  15. typedef long double LD;
  16. typedef long long LL;
  17.  
  18. using namespace std;
  19.  
  20. #define sz(A) (A).size()
  21. #define mp make_pair
  22. #define pb push_back
  23.  
  24. int n, m;
  25.  
  26. LL power(int num, int deg) {
  27.     if (!deg)
  28.         return 1;
  29.  
  30.     if (deg % 2) {
  31.         return (power(num, deg - 1) * num) % m;    
  32.     }
  33.     else {
  34.         LL sqrt_res = power(num, deg / 2);
  35.         return (sqrt_res * sqrt_res) % m;
  36.     }
  37. }
  38.  
  39. int main() {
  40.     cin >> n >> m;
  41.     LL res = power(3, n);
  42.     res--;
  43.     if (res < 0) res += m;
  44.     cout << res << endl;   
  45.     return 0;
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement