Submission #9779

#TimeUsernameProblemLanguageResultExecution timeMemory
9779ainu7Phibonacci (kriii2_P)C++98
1 / 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; int inv_mod(int a, int b) { if (a == 0) return 0; if (a == 1) return b; int div = mmod / a + 1; return inv_mod((a * (long long)div) % mmod, (b * (long long)div) % mmod); } pair<int, int> fibo(long long aa) { int a[2][2] = {0}; a[0][0] = a[0][1] = a[1][0] = 1; int res[2][2] = {0}; res[0][0] = res[1][1] = 1; for (int bit=60; bit>=0; bit--) { { int q[2][2] = {0}; for (int i=0; i<2; i++) for (int j=0; j<2; j++) for (int k=0; k<2; k++) q[i][k] = (q[i][k] + res[i][j] * 1LL * res[j][k]) % mmod; for (int i=0; i<2; i++) for (int j=0; j<2; j++) res[i][j] = q[i][j]; } if (aa&(1LL<<bit)) { int q[2][2] = {0}; for (int i=0; i<2; i++) for (int j=0; j<2; j++) for (int k=0; k<2; k++) q[i][k] = (q[i][k] + res[i][j] * 1LL * a[j][k]) % mmod; for (int i=0; i<2; i++) for (int j=0; j<2; j++) res[i][j] = q[i][j]; } } return pair<int, int>(res[1][0], res[1][1]); } pair<int, int> two(pair<int, int> a, pair<int, int> b) { int aa = (1LL*a.first*b.second+1LL*a.second*b.first+1LL*a.first*b.first) % mmod; int bb = (1LL*a.first*b.first+1LL*a.second*b.second) % mmod; return pair<int, int>(aa, bb); } pair<int, int> mult(pair<int, int> fib, long long a) { if (a == 0) return pair<int, int>(0, 1); pair<int, int> m = mult(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<int, int> fib = fibo(n); pair<int, int> fibpow = mult(fib, k); // cout << fib.first << " " << fib.second << " " << fibpow.first << " " << fibpow.second << " " << endl; pair<int, int> fibk = fibo(k); int mm = (fibpow.first * 1LL * inv_mod(fibk.first, 1)) % mmod; int nn = (fibpow.second - 1LL * fibk.second * mm) % mmod; if (nn < 0) nn += mmod; cout << mm << " " << nn << endl; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...