#include using namespace std; using ll = long long; using ld = long double; using pint = pair; using pll = pair; using pld = pair; 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 ostream& operator<<(ostream& s, const pair& p) {return s << "(" << p.first << " " << p.second << ")";} // coutによるvectorの出力(空白区切り) template ostream& operator<<(ostream& s, const vector& 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 ostream& operator<<(ostream& s, const vector< vector >& vv) { int len = vv.size(); for (int i = 0; i < len; ++i) { s << vv[i] << endl; } return s; } //最大値、最小値の更新。更新したor等しければtrueを返す template bool chmax(T& a, T b){return (a = max(a, b)) == b;} template bool chmin(T& a, T b){return (a = min(a, b)) == b;} //4近傍(上下左右) rep(i, 2) にすると右・下だけに進む vector dx_4 = {1, 0, -1, 0}; vector dy_4 = {0, 1, 0, -1}; // -------- template end - // // - library ------------- // // Matrix 構造体の定義 // n行 m列 初期値x の2次元vector として扱える // Matrix[i] でi行目のベクトルを読み書きできる template struct Matrix { // Matrix.val := n行m列 初期値x の2次元vector vector > val; // Matrix 及びメンバ変数val : n行 m列 初期値x の2次元vector を生成) Matrix(int n, int m, T x = 0) : val(n, vector(m, x)) {} // init() をかけることで、再度初期化することができる void init(int n, int m, T x = 0) { val.assign(n, vector(m, x)); } // size() はvalの行数であって、rank(次元)ではない(0だけになった行も削除しないから)ので注意 size_t size() const { return val.size(); } // Matrix[i] で、Matrix.val[i](i行目の行ベクトル)のポインタを返す // ポインタなので、アクセスだけでなく更新もできる // inline にすることで高速化 inline vector operator [] (int i) const { return val[i]; } inline vector& 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 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 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 a(2, 2, 1); a[1][1] = 0; Matrix b(2, 1, 0); b[0][0] = 1; // F1 = 0, F2 = 1; Matrix ans = matpow(a, N-2, M) * b; cout << ans[0][0] << endl; }