結果

問題 No.64 XORフィボナッチ数列
ユーザー nenuonnenuon
提出日時 2017-07-23 16:08:49
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 1,188 bytes
コンパイル時間 723 ms
コンパイル使用メモリ 96,444 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-17 16:50:33
合計ジャッジ時間 1,246 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 1 ms
6,940 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 1 ms
6,944 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 1 ms
6,944 KB
testcase_06 AC 1 ms
6,944 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 1 ms
6,944 KB
testcase_09 AC 2 ms
6,940 KB
testcase_10 AC 2 ms
6,944 KB
testcase_11 AC 1 ms
6,944 KB
testcase_12 AC 1 ms
6,944 KB
testcase_13 AC 2 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <cmath>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#include <stdlib.h>
#include <stdio.h>
#include <bitset>
using namespace std;
#define FOR(I,A,B) for(int I = (A); I < (B); ++I)
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;

// 無理やり行列累乗を使って

mat mul(mat &a, mat &b) {
  mat ret(a.size(), vec(b[0].size()));
  FOR(i,0,a.size()) {
    FOR(j,0,b[0].size()) {
      FOR(k,0,b.size()) {
        ret[i][j] ^= a[i][k] * b[k][j];
      }
    }
  }
  return ret;
}

mat fastpow(mat &a, ll n) {
  mat ret(a.size(), vec(a.size()));
  FOR(i,0,a.size()) FOR(j,0,a.size()) ret[i][i] = 1;
  while(n) {
    if(n & 1) ret = mul(ret, a);
    a = mul(a, a);
    n >>= 1;
  }
  return ret;
}

int main()
{
  ll f1,f2,N;
  cin>>f1>>f2>>N;
  if(N==0) {
    cout << f1 << endl;
    return 0;
  }
  mat m(2, vec(2));
  m[0][0] = 1;m[0][1] = 1;
  m[1][0] = 1;m[1][1] = 0;
  N--;
  mat mm = fastpow(m, N);
  mat f(2,vec(1));
  f[0][0] = f2;f[1][0] = f1;
  mat ans = mul(mm, f);
  cout << ans[0][0] << endl;
  return 0;
}
0