답안 #9791

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
9791 2014-09-28T08:57:47 Z ainu7 Phibonacci (kriii2_P) C++
4 / 4
0 ms 1676 KB
#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;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 1676 KB Output is correct
2 Correct 0 ms 1676 KB Output is correct
3 Correct 0 ms 1676 KB Output is correct
4 Correct 0 ms 1676 KB Output is correct
5 Correct 0 ms 1676 KB Output is correct
6 Correct 0 ms 1676 KB Output is correct
7 Correct 0 ms 1676 KB Output is correct
8 Correct 0 ms 1676 KB Output is correct
9 Correct 0 ms 1676 KB Output is correct
10 Correct 0 ms 1676 KB Output is correct
11 Correct 0 ms 1676 KB Output is correct
12 Correct 0 ms 1676 KB Output is correct
13 Correct 0 ms 1676 KB Output is correct
14 Correct 0 ms 1676 KB Output is correct
15 Correct 0 ms 1676 KB Output is correct
16 Correct 0 ms 1676 KB Output is correct
17 Correct 0 ms 1676 KB Output is correct
18 Correct 0 ms 1676 KB Output is correct
19 Correct 0 ms 1676 KB Output is correct
20 Correct 0 ms 1676 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 1676 KB Output is correct
2 Correct 0 ms 1676 KB Output is correct
3 Correct 0 ms 1676 KB Output is correct
4 Correct 0 ms 1676 KB Output is correct
5 Correct 0 ms 1676 KB Output is correct
6 Correct 0 ms 1676 KB Output is correct
7 Correct 0 ms 1676 KB Output is correct
8 Correct 0 ms 1676 KB Output is correct
9 Correct 0 ms 1676 KB Output is correct
10 Correct 0 ms 1676 KB Output is correct
11 Correct 0 ms 1676 KB Output is correct
12 Correct 0 ms 1676 KB Output is correct
13 Correct 0 ms 1676 KB Output is correct
14 Correct 0 ms 1676 KB Output is correct
15 Correct 0 ms 1676 KB Output is correct
16 Correct 0 ms 1676 KB Output is correct
17 Correct 0 ms 1676 KB Output is correct
18 Correct 0 ms 1676 KB Output is correct
19 Correct 0 ms 1676 KB Output is correct
20 Correct 0 ms 1676 KB Output is correct