結果

問題 No.526 フィボナッチ数列の第N項をMで割った余りを求める
ユーザー TeruMiyakeTeruMiyake
提出日時 2020-07-11 15:37:48
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 7,514 bytes
コンパイル時間 1,743 ms
コンパイル使用メモリ 175,612 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-21 04:44:13
合計ジャッジ時間 2,309 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using ld  = long double;
using pint = pair<int, int>;
using pll = pair<ll, ll>;
using pld = pair<ld, ld>;
const int INF=1e9+7;
const ll LINF=9223372036854775807;
const ll MOD=1e9+7;
const ld PI=acos(-1);
const ld EPS = 1e-10; //微調整用(EPSより小さいと0と判定など)

int ii() { int x; if (scanf("%d", &x)==1) return x; else return 0; }
long long il() { long long x; if (scanf("%lld", &x)==1) return x; else return 0; }
string is() { string x; cin >> x; return x; }
char ic() { char x; cin >> x; return x; }
void oi(int x) { printf("%d ", x); }
void ol(long long x) { printf("%lld ", x); }
void od_nosp(double x) { printf("%.15f", x); } // 古い問題用
void od(double x) { printf("%.15f ", x); }
// long doubleで受け取り、fをLfなどに変えて出力すると、変な数値が出る
// それをなんとかするには独自の出力を作らなければならなそう
void os(const string &s) { printf("%s ", s.c_str()); }
void oc(const char &c) { printf("%c ", c); }
#define o_map(v){cerr << #v << endl; for(const auto& xxx: v){cout << xxx.first << " " << xxx.second << "\n";}} //動作未確認
void br() { putchar('\n'); }

// #define gcd __gcd //llは受け取らない C++17~のgcdと違うので注意
// int lcm(int a, int b){return a / gcd(a, b) * b;}
#define begin_end(a) a.begin(),a.end() //sort(begin_end(vec));
#define REP(i,m,n) for(ll i=(ll)(m) ; i < (ll) (n) ; i++ )
#define rep(i,n) REP(i,0,n)
#define m_p(a,b) make_pair(a,b)
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
#define GET_POS(c,x) (lower_bound(c.begin(),c.end(),x)-c.begin())
#define p_b push_back
#define SZ(x) ((int)(x).size) //size()がunsignedなのでエラー避けに


// coutによるpairの出力(空白区切り)
template<typename T1, typename T2> ostream& operator<<(ostream& s, const pair<T1, T2>& p) {return s << "(" << p.first << " " << p.second << ")";}
// coutによるvectorの出力(空白区切り)
template<typename T> ostream& operator<<(ostream& s, const vector<T>& v) {
  int len = v.size();
  for (int i = 0; i < len; ++i) {
    s << v[i]; if (i < len - 1) s << " "; //"\t"に変えるとTabで見やすく区切る
  }
  return s;
}
// coutによる多次元vectorの出力(空白区切り)
template<typename T> ostream& operator<<(ostream& s, const vector< vector<T> >& vv) {
  int len = vv.size();
  for (int i = 0; i < len; ++i) {
    s << vv[i] << endl;
  }
  return s;
}

//最大値、最小値の更新。更新したor等しければtrueを返す
template<typename T>
bool chmax(T& a, T b){return (a = max(a, b)) == b;}
template<typename T>
bool chmin(T& a, T b){return (a = min(a, b)) == b;}

//4近傍(上下左右) rep(i, 2) にすると右・下だけに進む
vector<int> dx_4 = {1, 0, -1, 0};
vector<int> dy_4 = {0, 1, 0, -1};

// -------- template end - //


// - library ------------- //


// Matrix 構造体の定義
// n行 m列 初期値x の2次元vector<T> として扱える
// Matrix[i] でi行目のベクトルを読み書きできる
template<typename T>
struct Matrix {
  // Matrix.val := n行m列 初期値x の2次元vector<T>
  vector<vector<T> > val;
  // Matrix 及びメンバ変数val : n行 m列 初期値x の2次元vector<T> を生成)
  Matrix(int n, int m, T x = 0) : val(n, vector<T>(m, x)) {}
  // init() をかけることで、再度初期化することができる
  void init(int n, int m, T x = 0) { val.assign(n, vector<T>(m, x)); }
  // size() はvalの行数であって、rank(次元)ではない(0だけになった行も削除しないから)ので注意
  size_t size() const { return val.size(); }
  // Matrix[i] で、Matrix.val[i](i行目の行ベクトル)のポインタを返す
  // ポインタなので、アクセスだけでなく更新もできる
  // inline にすることで高速化
  inline vector<T> operator [] (int i) const { return val[i]; }
  inline vector<T>& operator [] (int i) { return val[i]; }
  // .height, .width := 行数・列数
  int height = val.size();
  int width = val[0].size();
  // 標準出力
  void show(){
    rep(i,height)rep(j,width){
      if(j != 0)cout << " ";
      cout << val[i][j];
      if(j==width-1)cout << endl;
    }
    return ;
  }

  // 行列同士の演算
//  Matrix& operator=(const Matrix& a){return (*a);} 元ライブラリについていたが、要らないように思える…… 要検証
  Matrix& operator+=(const Matrix& a){assert(width == a.width && height == a.height);rep(i,height)rep(j,width)val[i][j] += a[i][j]; return *this;}
  Matrix& operator-=(const Matrix& a){assert(width == a.width && height == a.height);rep(i,height)rep(j,width)val[i][j] -= a[i][j]; return *this;} 
  Matrix& operator*=(const Matrix& a){assert(width == a.height);Matrix<T> val2(height, a.width, 0);rep(i,height)rep(j,a.width)rep(k,width)val2[i][j] += val[i][k]*a[k][j];width = a.width;rep(i,height)val[i].resize(width);rep(i,height)rep(j,width)val[i][j] = val2[i][j]; return *this;}
  Matrix operator+(const Matrix& a) const { return Matrix(*this) += a;}
  Matrix operator-(const Matrix& a) const { return Matrix(*this) -= a;}
  Matrix operator*(const Matrix& a) const { return Matrix(*this) *= a;}
  bool operator==(const Matrix& a){assert(width == a.width && height == a.height);bool flg = true;rep(i,height)rep(j,width)if(val[i][j] != a[i][j])flg = false; return flg;}
  // 行列とスカラの演算
  Matrix& operator+=(const T& a){rep(i,height)rep(j,width)val[i][j] += a;return *this;}
  Matrix& operator-=(const T& a){rep(i,height)rep(j,width)val[i][j] -= a;return *this;}
  Matrix& operator*=(const T& a){rep(i,height)rep(j,width)val[i][j] *= a;return *this;}
  Matrix& operator/=(const T& a){rep(i,height)rep(j,width)val[i][j] /= a;return *this;}
  Matrix& operator%=(const T& a){rep(i,height)rep(j,width)val[i][j] %= a;return *this;}
  Matrix operator+(const T& a) const { return Matrix(*this) += a;}
  Matrix operator-(const T& a) const { return Matrix(*this) -= a;}
  Matrix operator*(const T& a) const { return Matrix(*this) *= a;}
  Matrix operator/(const T& a) const { return Matrix(*this) /= a;}
  Matrix operator%(const T& a) const { return Matrix(*this) %= a;}

};

// 繰り返し二乗法を用いた行列累乗(MODあるなしを第三引数で選べる)
// 行列の累乗は、正方行列(行数=列数)にしか定義されない
// 行列の0乗は、基本的に考えない(0乗=単位行列と定義することもあるらしい)
template <typename T_Matrix>
T_Matrix matpow(T_Matrix x, ll n, ll mod = -1){
  assert(x.height == x.width);
  assert(n > 0);
  
  // まず ans を単位行列とする( llpow() での ans = 1 の部分)
  T_Matrix ans(x.height, x.width, 0);
  rep(i, x.height) ans[i][i] = 1;

  if (mod == -1){
    // modをとらない場合
    while (n > 0){
      if (n % 2 == 1) ans = ans * x;
      x = x * x;
      n /= 2; // n を右に1つビットシフト
    }
    return ans;
  }
  else{
    // modをとる場合
    while (n > 0){
      if (n % 2 == 1) ans = ans * x % mod;
      x = x * x % mod;
      n /= 2; // n を右に1つビットシフト
    }
    return ans;
  }
}


// --------- library end - //


int main(){

  ll N, M;
  cin >> N >> M;

  Matrix<ll> a(2, 2, 1);
  a[1][1] = 0;

  Matrix<ll> b(2, 1, 0);
  b[0][0] = 1; // F1 = 0, F2 = 1;

  Matrix<ll> ans = matpow(a, N-2, M) * b;
  cout << ans[0][0] << endl;


}
0