結果

問題 No.344 ある無理数の累乗
ユーザー uenokuuenoku
提出日時 2017-01-31 00:22:46
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 2,195 bytes
コンパイル時間 840 ms
コンパイル使用メモリ 86,800 KB
実行使用メモリ 4,388 KB
最終ジャッジ日時 2023-08-25 15:27:36
合計ジャッジ時間 2,463 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
using namespace std;
typedef long long int lli;
lli MOD = 1000000007;
struct Matrix {
    vector<lli> a;
    lli n;
    lli mod = 1000;
    Matrix(lli n) : n(n)
    {
        a.assign(n * n, 0);
    };
    Matrix operator*(const Matrix r) const
    {
        Matrix v(n);
        rep(i, n) rep(j, n) rep(k, n)
        {
            v.a[i * n + j] += a[i * n + k] * r.a[k * n + j];
            v.a[i * n + j] %= mod;
        }
        return v;
    }
    Matrix operator+(const Matrix r) const
    {
        Matrix v(n);
        rep(i, n) rep(j, n)
        {
            v.a[i * n + j] = (a[i * n + j] + r.a[i * n + j]) % mod;
        }
        return v;
    }
    void set(int b, int d, int c)
    {
        a[b * n + d] = c;
    }
    void operator*=(const Matrix r)
    {
        Matrix v(n);
        rep(i, n) rep(j, n) rep(k, n)
        {
            v.a[i * n + j] += a[i * n + k] * r.a[k * n + j];
            v.a[i * n + j] %= mod;
        }
        *this = v;
    }
    void show()
    {
        rep(i, n) rep(j, n)
        {
            cout << a[i * n + j] << ' ';
            if (j == n - 1)
                cout << endl;
        }
    }
};
Matrix idn(lli n)
{
    Matrix a(n);
    rep(i, n)
    {
        a.a[i * n + i] = 1;
    }
    return a;
}
Matrix pow_mat(Matrix a, lli n)
{
    Matrix an = idn(a.n);
    while (n > 0) {
        if (n & 1)
            an *= a;
        a *= a;
        n /= 2;
    }
    return an;
}
int main()
{
    Matrix g(2);
    g.set(0, 0, 2);
    g.set(0, 1, 2);
    g.set(1, 0, 1);
    lli n;
    cin >> n;
    g = pow_mat(g, n - 1);
    if (n == 1) {
        cout << 2 << endl;
    } else if (n == 2) {
        cout << 7 << endl;
    } else if (n == 3) {
        cout << 20 << endl;
    } else if (n == 4) {
        cout << 55 << endl;
    } else {
        int a;
        if (n & 1)
            a = 0;
        else
            a = 1;
        cout << (g.a[0] * 2 + g.a[1] * 2 - a + 1000) % 1000 << endl;
    }
}
0