結果

問題 No.526 フィボナッチ数列の第N項をMで割った余りを求める
ユーザー やまだやまだ
提出日時 2024-06-21 22:50:52
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,161 bytes
コンパイル時間 847 ms
コンパイル使用メモリ 100,792 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-06-21 22:50:54
合計ジャッジ時間 1,792 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <string>
#include <cmath>
#include <iomanip>
#include <map>
#include <stack>
#include <vector>
#include <queue>
#include <deque>
#include <set>
#include <bitset>
using namespace std;
#define rep(i,a,b) for(ll i=a;i<b;i++)
#define rrep(i,a,b) for(ll i=a;i>=b;i--)
#define fore(i, a) for(auto& i : a)
#define pi 3.141592653589793238
typedef long long ll;
ll mod = 998244353; ll mof = 1000000007;

ll n, m;

tuple<ll, ll, ll, ll>kake(tuple<ll, ll, ll, ll>a, tuple<ll, ll, ll, ll>b) {
	ll a0 = get<0>(a);
	ll a1 = get<1>(a);
	ll a2 = get<2>(a);
	ll a3 = get<3>(a);
	ll b0 = get<0>(b);
	ll b1 = get<1>(b);
	ll b2 = get<2>(b);
	ll b3 = get<3>(b);
	ll c0 = a0 * b0 + a1 * b2;
	ll c1 = a0 * b1 + a1 * b3;
	ll c2 = a2 * b0 + a3 * b2;
	ll c3 = a2 * b1 + a3 * b3;
	return make_tuple(c0 % m, c1 % m, c2 % m, c3 % m);
}

int main()
{
	cin >> n >> m;
	tuple<ll, ll, ll, ll>a[39];
	a[0] = make_tuple(0, 1, 1, 1);
	rep(i, 1, 31) {
		a[i] = kake(a[i - 1], a[i - 1]);
	}
	bitset<30>bs(n-1);
	tuple<ll, ll, ll, ll>ans = make_tuple(1, 0, 0, 1);
	rep(i, 0, 30) {
		if (bs[i])ans = kake(ans, a[i]);
	}
	cout << get<1>(ans) << endl;
}
0