Submission #9791

#TimeUsernameProblemLanguageResultExecution timeMemory
9791ainu7Phibonacci (kriii2_P)C++98
4 / 4
0 ms1676 KiB
#include <math.h> #include <stdio.h> #include <string.h> #include <vector> #include <string> #include <queue> #include <map> #include <algorithm> #include <cmath> #include <iostream> #include <sstream> #include <set> using namespace std; const int mmod_ = 1000000007; const long long mmod = 1LL * mmod_ * mmod_; long long inv_mod(long long a, long long b) { //cout << a << " " << b << endl; if (a == 1) return b; long long div = mmod / a + 1; return inv_mod((a * (__int128_t)div) % mmod, (b * (__int128_t)div) % mmod); } pair<long long, long long> fibo(__int128_t aa) { __int128_t a[2][2] = {0}; a[0][0] = a[0][1] = a[1][0] = 1; __int128_t res[2][2] = {0}; res[0][0] = res[1][1] = 1; for (int bit=60; bit>=0; bit--) { { __int128_t q[2][2] = {0}; for (__int128_t i=0; i<2; i++) for (__int128_t j=0; j<2; j++) for (__int128_t k=0; k<2; k++) q[i][k] = (q[i][k] + res[i][j] * (__int128_t)1 * res[j][k]) % mmod; for (__int128_t i=0; i<2; i++) for (__int128_t j=0; j<2; j++) res[i][j] = q[i][j]; } if (aa&((__int128_t)1<<bit)) { __int128_t q[2][2] = {0}; for (__int128_t i=0; i<2; i++) for (__int128_t j=0; j<2; j++) for (__int128_t k=0; k<2; k++) q[i][k] = (q[i][k] + res[i][j] * (__int128_t)1 * a[j][k]) % mmod; for (__int128_t i=0; i<2; i++) for (__int128_t j=0; j<2; j++) res[i][j] = q[i][j]; } } return pair<long long, long long>(res[1][0], res[1][1]); } pair<long long, long long> two(pair<long long, long long> a, pair<long long, long long> b) { __int128_t aa = ((__int128_t)1*a.first*b.second+(__int128_t)1*a.second*b.first+(__int128_t)1*a.first*b.first) % mmod; __int128_t bb = ((__int128_t)1*a.first*b.first+(__int128_t)1*a.second*b.second) % mmod; return pair<long long, long long>(aa, bb); } pair<long long, long long> mult_two(pair<long long, long long> fib, __int128_t a) { if (a == 0) return pair<long long, long long>(0, 1); pair<long long, long long> m = mult_two(fib, a/2); m = two(m, m); if (a%2) m = two(m, fib); return m; } int main() { long long n, k; cin >> n >> k; pair<long long, long long> fib = fibo(n); pair<long long, long long> fibpow = mult_two(fib, k); // cout << fibpow.first << " " << fibpow.second << " " << endl; pair<long long, long long> fibk = fibo(k); // cout << "! " << fibk.first << " " << fibk.second << endl; // cout << fibpow.first % fibk.first << endl; if (fibpow.first % mmod_ == 0) { fibpow.first /= mmod_; fibk.first /= mmod_; } long long mm = (fibpow.first * (__int128_t)1 * inv_mod(fibk.first, 1)) % mmod; long long nn = (fibpow.second - (__int128_t)1 * fibk.second * mm) % mmod; if (nn < 0) nn += mmod; cout << mm % mmod_ << " " << nn % mmod_ << endl; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...