fork download
  1. #include <iostream>
  2.  
  3. using namespace std;
  4. __int128_t n;
  5.  
  6. __int128_t pow(__int128_t a, __int128_t b) {
  7. __int128_t res = 1;
  8. while (b)
  9. if (b % 2) {
  10. res = (res * a) % n;
  11. b--;
  12. } else {
  13. a = (a * a) % n;
  14. b = b / 2;
  15. }
  16. return res;
  17. }
  18.  
  19. __int128_t f(__int128_t x, __int128_t m) {
  20. if (m == 0) return 1;
  21. else if (m % 2) return f(x, m / 2) * (pow(x, (m + 1) / 2) + 1) % n;
  22. else return (f(x, m - 1) + pow(x, m)) % n;
  23. }
  24.  
  25. int main() {
  26. int counter;
  27. cin >> counter;
  28. for (int c = 0; c < counter; ++c) {
  29. size_t x, m, nn;
  30. cin >> x >> m >> nn;
  31. n = __int128_t(nn);
  32. cout << (size_t) f(__int128_t(x), __int128_t(m)) << "\n";
  33. }
  34. }
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
Standard output is empty