Submission #9221

# Submission time Handle Problem Language Result Execution time Memory
9221 2014-09-28T04:49:48 Z ainu7 Phibonacci (kriii2_P) C++
1 / 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;

int inv_mod(int a, int b) {
  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
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
# Verdict Execution time Memory 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 Runtime error 0 ms 1672 KB SIGFPE Floating point exception
20 Halted 0 ms 0 KB -