이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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 time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |