結果

問題 No.344 ある無理数の累乗
ユーザー 🍡yurahuna🍡yurahuna
提出日時 2016-04-08 06:09:53
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 3 ms / 2,000 ms
コード長 1,619 bytes
コンパイル時間 1,889 ms
コンパイル使用メモリ 171,520 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-19 07:15:17
合計ジャッジ時間 2,918 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 3 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 1 ms
5,376 KB
testcase_12 AC 2 ms
5,376 KB
testcase_13 AC 2 ms
5,376 KB
testcase_14 AC 1 ms
5,376 KB
testcase_15 AC 2 ms
5,376 KB
testcase_16 AC 1 ms
5,376 KB
testcase_17 AC 2 ms
5,376 KB
testcase_18 AC 1 ms
5,376 KB
testcase_19 AC 2 ms
5,376 KB
testcase_20 AC 2 ms
5,376 KB
testcase_21 AC 2 ms
5,376 KB
testcase_22 AC 2 ms
5,376 KB
testcase_23 AC 2 ms
5,376 KB
testcase_24 AC 2 ms
5,376 KB
testcase_25 AC 2 ms
5,376 KB
testcase_26 AC 2 ms
5,376 KB
testcase_27 AC 2 ms
5,376 KB
testcase_28 AC 2 ms
5,376 KB
testcase_29 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

#define rep(i,n) for (int i=0;i<(n);i++)
#define rep2(i,a,b) for (int i=(a);i<(b);i++)
#define rrep(i,n) for (int i=(n)-1;i>=0;i--)
#define rrep2(i,a,b) for (int i=(b)-1;i>=(a);i--)
#define all(a) (a).begin(),(a).end()

typedef long long ll;
typedef pair<int, int> P;
typedef vector<int> vi;
typedef vector<P> vp;
typedef vector<ll> vll;

typedef vector<ll> Vec;
typedef vector<Vec> Mat;

const ll mod = 1000;

Mat mulMatMat(Mat A, Mat B) {
    int n = A.size();
    Mat C(n, Vec(n, 0));

    rep(i, n) {
        rep(j, n) {
            rep(k, n) {
                C[i][j] += A[i][k] * B[k][j];
                C[i][j] %= mod;
            }
        }
    }

    return C;
}

Vec mulMatVec(Mat A, Vec vec) {
    int n = A.size();
    Vec res(n, 0);
    // reverse(all(vec));  // 添え字の大きい要素を上に持ってくるため

    rep(i, n) {
        rep(j, n) {
            res[i] += A[i][j] * vec[j];
            res[i] %= mod;
        }
    }

    return res;
}

Mat powMat(Mat A, ll p) {
    int n = A.size();
    Mat R(n, Vec(n, 0));
    rep(i, n) R[i][i] = 1;  // 単位行列

    for (; p >= 1; p /= 2) {
        if (p & 1) {
            R = mulMatMat(A, R);
        }
        A = mulMatMat(A, A);
    }

    return R;
}

signed main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);

    ll n;
    cin >> n;

    if (n == 0) {
        cout << 1 << endl;
        return 0;
    }

    Mat A(2, Vec(2, 1));
    A[0][1] = 3;

    A = powMat(A, n);
    auto vec = mulMatVec(A, {1, 0});

    cout << (2 * vec[0] - (n % 2 == 0)) % mod << endl;

}
0