結果

問題 No.1136 Four Points Tour
ユーザー sahiyasahiya
提出日時 2020-12-23 18:45:04
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 5,736 bytes
コンパイル時間 2,905 ms
コンパイル使用メモリ 134,320 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-21 15:15:39
合計ジャッジ時間 4,333 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 2 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 2 ms
4,348 KB
testcase_12 AC 2 ms
4,348 KB
testcase_13 AC 2 ms
4,348 KB
testcase_14 AC 2 ms
4,348 KB
testcase_15 AC 2 ms
4,348 KB
testcase_16 AC 2 ms
4,348 KB
testcase_17 AC 2 ms
4,348 KB
testcase_18 AC 2 ms
4,348 KB
testcase_19 AC 1 ms
4,348 KB
testcase_20 AC 2 ms
4,348 KB
testcase_21 AC 1 ms
4,348 KB
01_Sample03_evil.txt AC 2 ms
4,348 KB
04_Rnd_large_evil1.txt AC 2 ms
4,348 KB
04_Rnd_large_evil2.txt AC 2 ms
4,348 KB
04_Rnd_large_evil3.txt AC 1 ms
4,348 KB
04_Rnd_large_evil4.txt AC 2 ms
4,348 KB
04_Rnd_large_evil5.txt AC 2 ms
4,348 KB
04_Rnd_large_evil6.txt AC 2 ms
4,348 KB
04_Rnd_large_evil7.txt AC 2 ms
4,348 KB
04_Rnd_large_evil8.txt AC 2 ms
4,348 KB
04_Rnd_large_evil9.txt AC 2 ms
4,348 KB
04_Rnd_large_evil10.txt AC 2 ms
4,348 KB
05_Rnd_huge_evil1.txt AC 1 ms
4,348 KB
05_Rnd_huge_evil2.txt AC 2 ms
4,348 KB
05_Rnd_huge_evil3.txt AC 2 ms
4,348 KB
05_Rnd_huge_evil4.txt AC 2 ms
4,348 KB
05_Rnd_huge_evil5.txt AC 2 ms
4,348 KB
05_Rnd_huge_evil6.txt AC 2 ms
4,348 KB
05_Rnd_huge_evil7.txt AC 2 ms
4,348 KB
99_evil_01.txt AC 2 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstring>
#include <deque>
#include <forward_list>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef bitset<16> BS;
struct edge {
    int to, cost, id;
};
const ll MOD = 1E+09 + 7; // =998244353;
const ll INF = 1E18;
const int MAX_N = 1E+05;

template <typename T>
struct mat {
    // 行列m
    vector<vector<T>> m;
    // コンストラクタ:第1引数⇒行数、第2引数⇒列数、第3引数⇒初期値
    mat()
        : m(vector<vector<T>>(static_cast<T>(0)))
    {
    }
    mat(int h, int w)
        : m(vector<vector<T>>(h, vector<T>(w, static_cast<T>(0))))
    {
    }
    mat(int h, int w, T d)
        : m(vector<vector<T>>(h, vector<T>(w, d)))
    {
    }
    //単位行列を返すコンストラクタ:第1引数⇒n*n正方行列のn
    mat(int n)
        : m(vector<vector<T>>(n, vector<T>(n)))
    {
        for (int i = 0; i < n; i++) {

            m[i][i] = static_cast<T>(1);
        }
    }
    // 添字演算子
    vector<T> operator[](const int i) const { return m[i]; } //読み取り
    vector<T>& operator[](const int i) { return m[i]; } //書き込み
    // 行数・列数
    int nrow = m.size(); //行数
    int ncol = m[0].size(); //列数
    // 行列同士の演算
    mat& operator=(const mat& a)
    {
        m.resize(a.nrow);
        for (int i = 0; i < a.nrow; i++) {
            m[i].resize(a.ncol);
        }
        for (int i = 0; i < a.nrow; i++) {
            for (int j = 0; j < a.ncol; j++) {
                m[i][j] = a[i][j];
            }
        }
        nrow = a.nrow;
        ncol = a.ncol;
        return *this;
    }
    mat& operator+=(const mat& a)
    {
        for (int i = 0; i < nrow; i++) {
            for (int j = 0; j < ncol; j++) {
                m[i][j] += a[i][j];
            }
        }
        return *this;
    }
    mat& operator-=(const mat& a)
    {
        for (int i = 0; i < nrow; i++) {
            for (int j = 0; j < ncol; j++) {
                m[i][j] -= a[i][j];
            }
        }
        return *this;
    }

    mat& operator*=(const mat& a)
    {
        mat<T> m2(nrow, a.ncol, static_cast<T>(0));
        for (int i = 0; i < nrow; i++) {
            for (int j = 0; j < a.ncol; j++) {
                for (int k = 0; k < nrow; k++) {
                    m2[i][j] += m[i][k] * a[k][j];
                    m2[i][j] %= MOD;
                }
            }
        }
        for (int i = 0; i < nrow; i++) {
            for (int j = 0; j < a.ncol; j++) {
                m[i][j] = m2[i][j];
            }
        }
        ncol = a.ncol;
        return *this;
    }

    mat operator+(const mat& a) const { return static_cast<mat>(*this) += a; }
    mat operator-(const mat& a) const { return static_cast<mat>(*this) -= a; }
    mat operator*(const mat& a) const { return static_cast<mat>(*this) *= a; }
    bool operator==(const mat& a)
    {
        for (int i = 0; i < nrow; i++) {
            for (int j = 0; j < ncol; j++) {
                if (m[i][j] != a[i][j])
                    return false;
            }
        }
        return true;
    }

    // 行列とスカラの演算
    mat& operator+=(const T& a)
    {
        for (int i = 0; i < nrow; i++) {
            for (int j = 0; j < ncol; j++) {
                m[i][j] += a;
            }
        }
        return *this;
    }
    mat& operator-=(const T& a)
    {
        for (int i = 0; i < nrow; i++) {
            for (int j = 0; j < ncol; j++) {
                m[i][j] -= a;
            }
        }
        return *this;
    }
    mat& operator*=(const T& a)
    {
        for (int i = 0; i < nrow; i++) {
            for (int j = 0; j < ncol; j++) {
                m[i][j] *= a;
                m[i][j] %= MOD;
            }
        }
        return *this;
    }
    mat& operator/=(const T& a)
    {
        for (int i = 0; i < nrow; i++) {
            for (int j = 0; j < ncol; j++) {
                m[i][j] /= a;
            }
        }
        return *this;
    }
    mat operator+(const T& a) const { return static_cast<mat>(*this) += a; }
    mat operator-(const T& a) const { return static_cast<mat>(*this) -= a; }
    mat operator*(const T& a) const { return static_cast<mat>(*this) *= a; }
    mat operator/(const T& a) const { return static_cast<mat>(*this) /= a; }

    // 標準出力
    void show()
    {
        for (int i = 0; i < nrow; i++) {
            for (int j = 0; j < ncol; j++) {
                cout << m[i][j] << " ";
            }
            cout << "\n";
        }
        return;
    }
};

// x^a を求める
template <typename T>
mat<T> powmat(mat<T> x, ll a)
{
    mat<T> ans(x.nrow);
    while (a > 0) {
        if (a & 1) {
            ans = ans * x;
        }
        x = x * x;
        a >>= 1;
    }
    return ans;
}

ll dx[4] = { -1, 1, 0, 0 }, dy[4] = { 0, 0, -1, 1 };

ll N;

int X[MAX_N];

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    cin >> N;
    auto DP = mat<ll>(4, 1, 0LL);
    DP[0][0] = 1;
    auto A = mat<ll>(4, 4, 1LL);
    A[0][0] = 0, A[1][1] = 0, A[2][2] = 0, A[3][3] = 0;

    A = powmat(A, N);
    DP = A * DP;
    /*
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
            cout << "i = " << i << ", j = " << j << ", dp = " << dp[i][j] << "\n";
        }
    }
    */

    cout << DP[0][0] << "\n";

    return 0;
}
0