結果
問題 | No.526 フィボナッチ数列の第N項をMで割った余りを求める |
ユーザー | nope0124 |
提出日時 | 2021-01-18 05:37:38 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 2 ms / 2,000 ms |
コード長 | 2,953 bytes |
コンパイル時間 | 1,654 ms |
コンパイル使用メモリ | 175,448 KB |
実行使用メモリ | 5,376 KB |
最終ジャッジ日時 | 2024-05-07 19:11:40 |
合計ジャッジ時間 | 2,327 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 1 ms
5,376 KB |
testcase_02 | AC | 2 ms
5,376 KB |
testcase_03 | AC | 2 ms
5,376 KB |
testcase_04 | AC | 1 ms
5,376 KB |
testcase_05 | AC | 1 ms
5,376 KB |
testcase_06 | AC | 2 ms
5,376 KB |
testcase_07 | AC | 2 ms
5,376 KB |
testcase_08 | AC | 2 ms
5,376 KB |
testcase_09 | AC | 2 ms
5,376 KB |
testcase_10 | AC | 2 ms
5,376 KB |
testcase_11 | AC | 2 ms
5,376 KB |
testcase_12 | AC | 2 ms
5,376 KB |
testcase_13 | AC | 2 ms
5,376 KB |
testcase_14 | AC | 2 ms
5,376 KB |
ソースコード
/* C++ */ #include<bits/stdc++.h> using namespace std; typedef long long int ll; typedef long double ld; typedef vector<ll> vl; typedef vector<vector<ll>> vvl; typedef vector<vector<vector<ll>>> vvvl; typedef pair<ll, ll> pint; const ll MOD = 1000000007; const ll INF = 922337203685477580; #define rep(i, n) for(ll i = (ll)0; i < (ll)(n); i++) #define Rep(i, s, t) for(ll i = (ll)(s); i < (ll)(t); i++) #define ALL(a) (a).begin(),(a).end() #define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); #define PI 3.14159265358979323846 #define ifYN(x) cout << (x ? "Yes" : "No") << "\n" ll dx[4] = {-1, 1, 0, 0}; ll dy[4] = {0, 0, -1, 1}; template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; } template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; } ll Len(ll n) { if (n == 0) return 1; ll s = 0; while (n != 0) s++, n /= 10; return s; } ll Sint(ll n) { ll ans = 0; while (n != 0) ans += n % 10, n /= 10; return ans; } ll Svec(vector<ll> v){ ll n = 0; rep(i, (ll)v.size()) n += v[i]; return n; } ll GCD(ll a, ll b) { return b ? GCD(b, a % b) : a; } ll LCM(ll a, ll b){ return a / GCD(a, b) * b; } ll POW(ll a, ll n, ll mod = INF) { ll res = 1; while (n > 0) { if (n & 1) res = res * a % mod; a = a * a % mod; n >>= 1; } return res; } void dis(vector<ll> v){ rep(i, v.size()) cout << v[i] << endl; } void dis2(vector<vector<ll> > v) { rep (i, v.size()) { rep (j, v[0].size()) cout << v[i][j] << ' '; cout << endl; } } bool cmp(pint a, pint b) { return a.second < b.second; } vector<vector<ll>> MATMUL(vector<vector<ll>> &A, vector<vector<ll>> &B, ll MOD) { vector<vector<ll>> ret(A.size(), vector<ll>(B[0].size(), 0)); rep (i, A.size()) { rep (j, B[0].size()) { rep (k, B.size()) { ret[i][j] = (ret[i][j] + A[i][k] * B[k][j]) % MOD; } } } return ret; } vector<vector<ll>> MATPOW(vector<vector<ll>> A, ll K, ll MOD) { ll N = A.size(); vector<vector<ll>> ret(N, vector<ll>(N, 0)); rep (i, N) ret[i][i] = 1; while (K) { if (K & 1) ret = MATMUL(ret, A, MOD); A = MATMUL(A, A, MOD); K >>= 1; } return ret; } /* void MATPOW(vector<ll> &dp, vector<vector<ll>> &mat, ll k, ll MOD) { ll N = mat.size(); while (k > 0) { if (k & 1) { vector<ll> ret(N, 0); rep (i, N) rep (j, N) ret[i] += mat[i][j] * dp[j], ret[i] %= MOD; dp = ret; } vector<vector<ll>> ret(N, vector<ll>(N, 0)); rep (i, N) rep (j, N) rep (k, N) ret[i][j] += mat[i][k] * mat[k][j], ret[i][j] %= MOD; mat = ret; k >>= 1; } return; }*/ /*↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓*/ int main() { IOS; ll N, M; cin >> N >> M; vector<vector<ll>> dp = {{1}, {0}}; vector<vector<ll>> mat = {{1, 1}, {1, 0}}; mat = MATPOW(mat, N - 1, M); dp = MATMUL(mat, dp, M); cout << dp[1][0] << endl; }