結果

問題 No.526 フィボナッチ数列の第N項をMで割った余りを求める
ユーザー ris osiris osi
提出日時 2021-07-27 00:48:04
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 17 ms / 2,000 ms
コード長 4,020 bytes
コンパイル時間 1,257 ms
コンパイル使用メモリ 168,816 KB
実行使用メモリ 42,368 KB
最終ジャッジ日時 2023-09-29 21:43:14
合計ジャッジ時間 2,287 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 6 ms
10,864 KB
testcase_11 AC 17 ms
42,252 KB
testcase_12 AC 15 ms
42,368 KB
testcase_13 AC 14 ms
42,096 KB
testcase_14 AC 16 ms
42,232 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;
typedef pair<long, long> pll;
typedef vector<int> vi;
typedef vector<long> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;

#define all(v) v.begin(), v.end()
#define pr print

/* REP macro */
#define repe(i, a, n) for (long i = (a); i <= (long)(n); ++i)
#define rep(i, n) repe(i, 0, n-1)
#define repd(i, n) for (int i = (long)(n-1); i >= 0; i--)


/* func */
template <typename T> bool chmin(T &a, const T &b) {bool compare = a > b; if (compare) a = b; return compare;}
template <typename T> bool chmax(T &a, const T &b) {bool compare = a < b; if (compare) a = b; return compare;}
template <typename T> inline void print(const T &a) {cout << a << endl;}
template <typename Head, typename ...Tail> void print(Head head, Tail... tail) {
  cout << head << ' ';
  print(tail...);
}
template <typename T> ostream& operator << (ostream &ostr, const vector<T> &v){
  cout << '{';
  for(auto it = v.begin(); it < v.end(); it++){
    if (it == v.end()-1) cout << *it;
    else cout << *it << ", ";
  }
  cout << '}';
  return ostr;
}

template <typename T1, typename T2> ostream& operator << (ostream &ostr, const pair<T1, T2> &p){
  cout << '(' << p.first << ", " << p.second << ')';
  return ostr;
}

template <typename T> T min(const vector<T> &v){
  return accumulate(all(v), v.front(), [](T acc, T i){ return min(acc, i);} );
}

template <typename T> T umin(const vector<T> &v){
  T top = (v.front() < 0 ? -1 : v.front());
  return accumulate(all(v), top, [](T acc, T i){ 
    if (i < 0) return acc;
    return min(acc, i);} );
}

int MOD = 1000000007;

struct mint{

  long val;
   
  mint(long val=0) : val((val % MOD + MOD) % MOD) {}
  
  mint operator-() { return mint(-val); }
  
  mint& operator+=(const mint& a) {
    if ((val += a.val) >= MOD) val -= MOD;
    return *this;
  }
  mint& operator-=(const mint& a) {
    if ((val += MOD-a.val) >= MOD) val -= MOD;
    return *this;
  }
  mint& operator*=(const  mint& a) {
     (val *= a.val) %= MOD;
     return *this;
  }
  
  mint operator+(const mint &a){
    mint b = *this;
    return b += a;
  }
  
  mint operator-(const mint &a){
    mint b = *this;
    return b -= a;
  }
  
  mint operator*(const mint &a){
    mint b = *this;
    return b *= a;
  }
  
  mint& operator++(){
    return *this += mint(1);
  }
  
  mint& operator--(){
    return *this -= mint(1);
  }
  
  mint operator++(int){
    mint b = *this;
    *this += mint(1);
    return b;
  }
  
  mint operator--(int){
    mint b = *this;
    *this -= mint(1);
    return b;
  }
  

  mint pow(long n) const {
    if (n == 0) return 1;
    mint t = pow(n>>1);
  	t *= t;
    if (n % 2 == 1) t *= *this;
  	return t;
  }

  mint inv() const {
    return pow(MOD-2);
  }
    
  mint& operator/=(const mint& a) {
    return (*this) *= a.inv();
  }
    
  mint operator/(const mint& a){
    mint b = *this;
    return b/=a;
  }
  
  mint operator==(const mint& a){
    return this->val == a.val;
  }
  
  mint operator!=(const mint& a){
    return this->val != a.val;
  }

};

ostream& operator<<(ostream& os, const mint& m){
  os << m.val;
  return os;
}

mint pow(const mint &a, long n){
  return a.pow(n);
}

struct UnionFind {
    vl par;
    vl si;
 
    UnionFind(long N) : par(N), si(N) { 
        rep(i, N) {
          par[i] = i;
          si[i] = 1;
        }
    }
 
    long root(long x) { 
        if (par[x] == x) return x;
        return par[x] = root(par[x]);
    }
 
    void unite(long x, long y) {
        int rx = root(x);
        int ry = root(y);
        if (rx == ry) return;
        par[rx] = ry;
        si[ry] += si[rx];
    }
 
    bool same(int x, int y) {
        int rx = root(x);
        int ry = root(y);
        return rx == ry;
    }
  
    long size(int x){
       return si[root(x)];
    }
};


int main(){
  long N, M;
  cin >> N >> M;
  
  MOD = M;
  
  vector<mint> dp(N+1);
  dp[0] = 0; dp[1] = 0; dp[2] = 1;
  
  repe(i, 3, N){
     dp[i] = dp[i-1] + dp[i-2];
  }
  
  pr(dp[N]);

}
0