結果

問題 No.526 フィボナッチ数列の第N項をMで割った余りを求める
ユーザー 阿部光希阿部光希
提出日時 2022-11-23 12:41:00
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 2,621 bytes
コンパイル時間 2,180 ms
コンパイル使用メモリ 202,220 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-24 22:28:56
合計ジャッジ時間 14,507 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include<bits/stdc++.h>
#define int ll
#define rep(i, N) for(int i = 0; i < (int)(N); ++i)
#define per(i, N) for(int i = (int)(N)-1; i >= 0; --i)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define bit(n, k) (((n) >> (k)) & 1)
#define pcnt(n) (bitset<64>(n).count())
#define endl '\n'
#define fi first
#define se second
#define mkpr make_pair
#define mktpl make_tuple
#define eb emplace_back

#ifdef LOCAL
#define debug(x) cerr << "line:"<< __LINE__ << " " << #x << " = " << x << '\n'
#define dbgln() cerr << "line:" << __LINE__ << " passed.\n"
#else
#define debug(x) void(0)
#define dbgln() void(0)
#endif

using namespace std;
using ll = int64_t;
using ull = uint64_t;
using ld = long double;
using point = struct{ ld x, y; };
using State = string::iterator;
template<class T> using max_heap = priority_queue<T>;
template<class T> using min_heap = priority_queue<T, vector<T>, greater<T>>;
template<class T> using vec = vector<T>;
template<class T> using vvec = vec<vec<T>>;
template<class T> using vvvec = vec<vvec<T>>;
template<class T> using vvvvec = vvec<vvec<T>>;

constexpr ld EPS = 1e-10;
ld Pi = acos(-1);
constexpr int INF = 1001001001;
constexpr ll LINF = 1001001001001001001ll;
constexpr ll MOD = (1) ? 998244353 : 1e9+7;
constexpr int NIL = -1;
constexpr int pm[2] = {1, -1};
constexpr int dy[4] = {0, 1, 0, -1};
constexpr int dx[4] = {1, 0, -1, 0};
constexpr int dy8[8] = {0, 1, 1, 1, 0, -1, -1, -1};
constexpr int dx8[8] = {1, 1, 0, -1, -1, -1, 0, 1};

ll cel(ll a, ll b){ return (a + b - 1) / b;}
ll Gcd(ll a, ll b){ return b ? Gcd(b, a % b) : abs(a);}
ll Lcm(ll a, ll b){ return a / Gcd(a, b) * b;}
template<class T> T powi(T x, ll exp){
    return exp > 0 ? (exp & 1 ? x : 1) * powi(x * x, exp >> 1) : x / x;
}
ll modpow(ll x, ll exp, ll mod){
    x %= mod;
    return exp > 0 ? (exp & 1 ? x : 1) * modpow(x * x, exp >> 1, mod) % mod : 1;
}
template<class T> bool chmin(T &a, T b){ return a > b ? (a = b, true) : 0;}
template<class T> bool chmax(T &a, T b){ return a < b ? (a = b, true) : 0;}
void yesno(bool b){ cout << (b ? "Yes" : "No"); }

using Pair = pair<ll, ll>;
using Tpl = tuple<int, int, int>;

ll A, B;

ll fibo(ll N){
    if(N <= 1) return 1;
    int p = (N + 1) >> 1;
    ll val1 = fibo(p);
    ll val2 = fibo(p - 1);
    if(!(N & 1)) return (val1 * val1 + val2 * val2) % B;
    ll val3 = fibo(p - 2);
    return val2 * (val1 + val3) % B;
}

void Main(){
    cin >> A >> B;
    cout << fibo(A - 2) << endl;
} 

signed main(){
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);
    cout << fixed << setprecision(15);
    Main();
    return 0;
}
0