fork download
  1. #include <iostream>
  2. #include <array>
  3.  
  4. unsigned int fib_mod(unsigned int n, unsigned int m) {using M = std::array<unsigned int, 3>;
  5. auto mul = [m] (M & me, const M & other) {
  6. me[2] = ((me[1] * other[1]) % m + (me[2] * other[2]) % m) % m;
  7. unsigned int me_1 = me[1];
  8. me[1] = ((me[0] * other[1]) % m + (me[1] * other[2]) % m) % m;
  9. me[0] = ((me[0] * other[0]) % m + (me_1 * other[1]) % m) % m;
  10. };
  11. M r{{1, 1, 0}}, e{{1, 0, 1}}, ans{e};
  12. while (n) {
  13. if (n & 1) {
  14. mul(ans, r);
  15. }
  16. mul(r, M(r));
  17. n /= 2;
  18. }
  19. return ans[1];
  20. }
  21.  
  22. int main() {
  23. std::cout << fib_mod(1<<31, 1<<31) << std::endl;
  24. return 0;
  25. }
Success #stdin #stdout 0s 3460KB
stdin
Standard input is empty
stdout
2140540357