結果

問題 No.541 3 x N グリッド上のサイクルの個数
ユーザー uenokuuenoku
提出日時 2017-08-24 03:31:40
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 5 ms / 2,000 ms
コード長 2,259 bytes
コンパイル時間 943 ms
コンパイル使用メモリ 93,232 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-23 13:45:05
合計ジャッジ時間 2,842 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 3 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 3 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 3 ms
5,376 KB
testcase_10 AC 3 ms
5,376 KB
testcase_11 AC 3 ms
5,376 KB
testcase_12 AC 3 ms
5,376 KB
testcase_13 AC 3 ms
5,376 KB
testcase_14 AC 3 ms
5,376 KB
testcase_15 AC 3 ms
5,376 KB
testcase_16 AC 3 ms
5,376 KB
testcase_17 AC 3 ms
5,376 KB
testcase_18 AC 3 ms
5,376 KB
testcase_19 AC 4 ms
5,376 KB
testcase_20 AC 3 ms
5,376 KB
testcase_21 AC 4 ms
5,376 KB
testcase_22 AC 5 ms
5,376 KB
testcase_23 AC 4 ms
5,376 KB
testcase_24 AC 4 ms
5,376 KB
testcase_25 AC 4 ms
5,376 KB
testcase_26 AC 4 ms
5,376 KB
testcase_27 AC 4 ms
5,376 KB
testcase_28 AC 4 ms
5,376 KB
testcase_29 AC 2 ms
5,376 KB
testcase_30 AC 2 ms
5,376 KB
testcase_31 AC 3 ms
5,376 KB
testcase_32 AC 3 ms
5,376 KB
testcase_33 AC 2 ms
5,376 KB
testcase_34 AC 2 ms
5,376 KB
testcase_35 AC 2 ms
5,376 KB
testcase_36 AC 3 ms
5,376 KB
testcase_37 AC 2 ms
5,376 KB
testcase_38 AC 2 ms
5,376 KB
testcase_39 AC 3 ms
5,376 KB
testcase_40 AC 3 ms
5,376 KB
testcase_41 AC 3 ms
5,376 KB
testcase_42 AC 3 ms
5,376 KB
testcase_43 AC 3 ms
5,376 KB
testcase_44 AC 3 ms
5,376 KB
testcase_45 AC 3 ms
5,376 KB
testcase_46 AC 3 ms
5,376 KB
testcase_47 AC 3 ms
5,376 KB
testcase_48 AC 3 ms
5,376 KB
testcase_49 AC 3 ms
5,376 KB
testcase_50 AC 3 ms
5,376 KB
testcase_51 AC 4 ms
5,376 KB
testcase_52 AC 4 ms
5,376 KB
testcase_53 AC 4 ms
5,376 KB
testcase_54 AC 4 ms
5,376 KB
testcase_55 AC 4 ms
5,376 KB
testcase_56 AC 4 ms
5,376 KB
testcase_57 AC 3 ms
5,376 KB
testcase_58 AC 4 ms
5,376 KB
testcase_59 AC 4 ms
5,376 KB
testcase_60 AC 4 ms
5,376 KB
testcase_61 AC 4 ms
5,376 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 = 1e9 + 7;
    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)
        {
            cerr << a[i * n + j] << ' ';
            if (j == n - 1)
                cerr << 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;
}
Matrix parseStr2Matrix(int n, vector<string> s)
{
    Matrix g(10);
    rep(i, n)
    {
        rep(j, n)
            g.set(i, j, s[i][j] - '0');
    }
    return g;
}
int main()
{
    vector<string> s = {
        "1000000000",
        "1111000010",
        "1111110000",
        "1111111100",
        "1011110000",
        "1011111000",
        "1001011010",
        "1100001100",
        "0001000010",
        "0111111011",
    };

    auto m = parseStr2Matrix(10, s);
    lli n;
    cin >> n;
    auto g = pow_mat(m, n + 1);
    g.show();
    cout << g.a[9 * 10 + 0] << endl;
}
0