結果
| 問題 |
No.613 Solitude by the window
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2017-12-14 01:34:00 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 3 ms / 2,000 ms |
| コード長 | 1,689 bytes |
| コンパイル時間 | 2,430 ms |
| コンパイル使用メモリ | 194,180 KB |
| 最終ジャッジ日時 | 2025-01-05 05:18:46 |
|
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 21 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
ll N, M;
struct Matrix {
Matrix()
{
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
table[i][j] = 0;
}
}
}
static Matrix Unit()
{
Matrix ans;
ans.table[0][0] = 1;
ans.table[1][1] = 1;
return ans;
}
Matrix operator*(const Matrix& mat) const
{
Matrix ans;
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
for (int k = 0; k < 2; k++) {
ans.table[i][j] += (table[i][k] * mat.table[k][j]) % M;
ans.table[i][j] %= M;
}
}
}
return ans;
}
array<array<ll, 2>, 2> table;
};
ll power(const ll n, const ll mod)
{
if (n == 0) {
return 1;
}
if (n % 2 == 1) {
return (power(n - 1, mod) * 2) % mod;
} else {
const __int128_t pp = (__int128_t)power(n / 2, mod);
return (pp * pp) % mod;
}
}
Matrix power(const Matrix& mat, const ll n)
{
if (n == 0) {
return Matrix::Unit();
}
if (n % 2 == 1) {
return power(mat, n - 1) * mat;
} else {
const Matrix pp = power(mat, n / 2);
return pp * pp;
}
}
int main()
{
cin.tie(0);
ios::sync_with_stdio(false);
cin >> N >> M;
Matrix mat;
mat.table[0][0] = 2;
mat.table[0][1] = 3;
mat.table[1][0] = 1;
mat.table[1][1] = 2;
const ll mod = (M - 1) * (M + 1);
const Matrix ans = power(mat, power(N, mod));
cout << (2 * ans.table[0][0] + M - 2) % M << endl;
return 0;
}