結果

問題 No.541 3 x N グリッド上のサイクルの個数
ユーザー 👑 はまやんはまやんはまやんはまやん
提出日時 2017-07-01 14:12:22
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 4 ms / 2,000 ms
コード長 3,059 bytes
コンパイル時間 2,119 ms
コンパイル使用メモリ 177,132 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-15 09:58:53
合計ジャッジ時間 3,810 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 2 ms
6,944 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 2 ms
6,944 KB
testcase_08 AC 2 ms
6,940 KB
testcase_09 AC 2 ms
6,940 KB
testcase_10 AC 3 ms
6,944 KB
testcase_11 AC 2 ms
6,940 KB
testcase_12 AC 3 ms
6,940 KB
testcase_13 AC 3 ms
6,944 KB
testcase_14 AC 3 ms
6,944 KB
testcase_15 AC 3 ms
6,944 KB
testcase_16 AC 2 ms
6,940 KB
testcase_17 AC 3 ms
6,940 KB
testcase_18 AC 3 ms
6,940 KB
testcase_19 AC 3 ms
6,940 KB
testcase_20 AC 3 ms
6,944 KB
testcase_21 AC 3 ms
6,940 KB
testcase_22 AC 3 ms
6,940 KB
testcase_23 AC 4 ms
6,940 KB
testcase_24 AC 3 ms
6,944 KB
testcase_25 AC 3 ms
6,940 KB
testcase_26 AC 3 ms
6,940 KB
testcase_27 AC 3 ms
6,944 KB
testcase_28 AC 3 ms
6,940 KB
testcase_29 AC 2 ms
6,940 KB
testcase_30 AC 2 ms
6,940 KB
testcase_31 AC 2 ms
6,944 KB
testcase_32 AC 2 ms
6,940 KB
testcase_33 AC 2 ms
6,940 KB
testcase_34 AC 3 ms
6,940 KB
testcase_35 AC 3 ms
6,940 KB
testcase_36 AC 2 ms
6,940 KB
testcase_37 AC 2 ms
6,940 KB
testcase_38 AC 2 ms
6,944 KB
testcase_39 AC 2 ms
6,944 KB
testcase_40 AC 3 ms
6,940 KB
testcase_41 AC 3 ms
6,940 KB
testcase_42 AC 2 ms
6,944 KB
testcase_43 AC 2 ms
6,940 KB
testcase_44 AC 2 ms
6,944 KB
testcase_45 AC 2 ms
6,944 KB
testcase_46 AC 2 ms
6,944 KB
testcase_47 AC 2 ms
6,940 KB
testcase_48 AC 3 ms
6,940 KB
testcase_49 AC 2 ms
6,940 KB
testcase_50 AC 3 ms
6,940 KB
testcase_51 AC 3 ms
6,940 KB
testcase_52 AC 3 ms
6,940 KB
testcase_53 AC 4 ms
6,940 KB
testcase_54 AC 3 ms
6,940 KB
testcase_55 AC 3 ms
6,940 KB
testcase_56 AC 3 ms
6,940 KB
testcase_57 AC 3 ms
6,940 KB
testcase_58 AC 3 ms
6,940 KB
testcase_59 AC 3 ms
6,940 KB
testcase_60 AC 3 ms
6,940 KB
testcase_61 AC 3 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<b;i++)
#pragma GCC optimize ("-O3")
using namespace std; void _main(); int main() { cin.tie(0); ios::sync_with_stdio(false); _main(); }
//---------------------------------------------------------------------------------------------------
typedef long long ll;
int mod = 1000000007;
int add(int x, int y) { return (x += y) >= mod ? x - mod : x; }
template<class... T> int add(int x, T... y) { return add(x, add(y...)); }
int mul(int x, int y) { return 1LL * x * y % mod; }
template<class... T> int mul(int x, T... y) { return mul(x, mul(y...)); }
int sub(int x, int y) { return add(x, mod - y); }
int modpow(int a, long long b) {
    int ret = 1; while (b > 0) {
        if (b & 1) ret = 1LL * ret * a % mod; a = 1LL * a * a % mod; b >>= 1;
    } return ret;
}
int modinv(int a) { return modpow(a, mod - 2); }
typedef vector<int> Vec;
typedef vector<Vec> Mat;
Vec mulMatVec(Mat a, Vec b)
{
    int n = b.size(); Vec ret(n, 0);
    rep(i, 0, n) rep(j, 0, n) ret[i] = add(ret[i], mul(a[i][j], b[j]));
    return ret;
}
Mat mulMatMat(Mat a, Mat b)
{
    int n = a.size(); Mat ret(n, Vec(n, 0));
    rep(i, 0, n) rep(j, 0, n) rep(k, 0, n) ret[i][j] = add(ret[i][j], mul(a[i][k], b[k][j]));
    return ret;
}
Mat fastpow(Mat x, ll n)
{
    Mat ret(x.size(), Vec(x.size(), 0));
    rep(i, 0, x.size()) ret[i][i] = 1;
    while (0 < n) { if ((n % 2) == 0) { x = mulMatMat(x, x); n >>= 1; } else { ret = mulMatMat(ret, x); --n; } }
    return ret;
}
void printVec(Vec a) { cout << "[\t"; rep(i, 0, a.size()) cout << a[i] << "\t"; cout << "]" << endl; }
void printMat(Mat a) { rep(i, 0, a.size()) printVec(a[i]); }
/*---------------------------------------------------------------------------------------------------
            ∧_∧  
      ∧_∧  (´<_` )  Welcome to My Coding Space!
     ( ´_ゝ`) /  ⌒i     
    /   \     | |     
    /   / ̄ ̄ ̄ ̄/  |  
  __(__ニつ/     _/ .| .|____  
     \/____/ (u ⊃  
---------------------------------------------------------------------------------------------------*/



string M[10] = {
    "1000000000", // 0. 初期状態(どこもつながってない)
    "1111110000", // 1. (1,2)
    "1111010000", // 2. (1,3)
    "1111011000", // 3. (1,4)
    "1100110010", // 4. (2,3)
    "1111111100", // 5. (2,4)
    "1001011010", // 6. (3,4)
    "1000101100", // 7. (1,2)(3,4)
    "0000010010", // 8. (1,4)(2,3)
    "0111111011"  // 9. 受理状態(単純閉路ができてる)
};

string Vinit = "1000000000";

ll N;
//---------------------------------------------------------------------------------------------------
void _main() {
    while (cin >> N) {
        Mat m(10, Vec(10, 0));
        rep(y, 0, 10) rep(x, 0, 10) m[y][x] = M[y][x] - '0';
        Vec v(10, 0);
        rep(x, 0, 10) v[x] = Vinit[x] - '0';

        m = fastpow(m, N + 1);
        v = mulMatVec(m, v);
        cout << v[9] << endl;
    }
}
0