結果

問題 No.1112 冥界の音楽
ユーザー snrnsidysnrnsidy
提出日時 2021-06-14 02:03:14
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 5,695 bytes
コンパイル時間 3,402 ms
コンパイル使用メモリ 232,096 KB
実行使用メモリ 12,700 KB
最終ジャッジ日時 2024-06-06 05:34:46
合計ジャッジ時間 7,734 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
10,624 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 1,172 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 4 ms
5,376 KB
testcase_09 AC 185 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 124 ms
5,376 KB
testcase_12 AC 2 ms
5,376 KB
testcase_13 TLE -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function 'std::vector<int> berlekamp_massey(std::vector<int>)':
main.cpp:58:36: warning: 'ld' may be used uninitialized [-Wmaybe-uninitialized]
   58 |         lint k = -(x[i] - t) * ipow(ld, mod - 2) % mod;
      |                                ~~~~^~~~~~~~~~~~~
main.cpp:45:13: note: 'ld' was declared here
   45 |     int lf, ld;
      |             ^~
main.cpp:59:25: warning: 'lf' may be used uninitialized [-Wmaybe-uninitialized]
   59 |         vector<int> c(i - lf - 1);
      |                       ~~^~~~
main.cpp:45:9: note: 'lf' was declared here
   45 |     int lf, ld;
      |         ^~

ソースコード

diff #

#include <bits/stdc++.h>
#include <random>

using namespace std;

const long long int MOD = 1e9 + 7;
int p[216], q[216], r[216];
long long int k, m, n;

vector <vector<long long int>> mul(vector <vector<long long int>> a, vector<vector<long long int>> b)
{
	int n = a.size();
	int m = a[0].size();
	int k = b[0].size();
	vector <vector<long long int>>c(n, vector<long long int>(k, 0));

	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < k; j++)
		{
			for (int k = 0; k < m; k++)
			{
				c[i][j] += ((a[i][k]) * (b[k][j]));
				c[i][j] %= MOD;
			}
		}
	}

	return c;
}

const long long int mod = 1e9 + 7;
using lint = long long;
lint ipow(lint x, lint p) {
    lint ret = 1, piv = x;
    while (p) {
        if (p & 1) ret = ret * piv % mod;
        piv = piv * piv % mod;
        p >>= 1;
    }
    return ret;
}
vector<int> berlekamp_massey(vector<int> x) {
    vector<int> ls, cur;
    int lf, ld;
    for (int i = 0; i < x.size(); i++) {
        lint t = 0;
        for (int j = 0; j < cur.size(); j++) {
            t = (t + 1ll * x[i - j - 1] * cur[j]) % mod;
        }
        if ((t - x[i]) % mod == 0) continue;
        if (cur.empty()) {
            cur.resize(i + 1);
            lf = i;
            ld = (t - x[i]) % mod;
            continue;
        }
        lint k = -(x[i] - t) * ipow(ld, mod - 2) % mod;
        vector<int> c(i - lf - 1);
        c.push_back(k);
        for (auto& j : ls) c.push_back(-j * k % mod);
        if (c.size() < cur.size()) c.resize(cur.size());
        for (int j = 0; j < cur.size(); j++) {
            c[j] = (c[j] + cur[j]) % mod;
        }
        if (i - lf + (int)ls.size() >= (int)cur.size()) {
            tie(ls, lf, ld) = make_tuple(cur, i, (t - x[i]) % mod);
        }
        cur = c;
    }
    for (auto& i : cur) i = (i % mod + mod) % mod;
    return cur;
}
int get_nth(vector<int> rec, vector<int> dp, lint n) {
    int m = rec.size();
    vector<int> s(m), t(m);
    s[0] = 1;
    if (m != 1) t[1] = 1;
    else t[0] = rec[0];
    auto mul = [&rec](vector<int> v, vector<int> w) {
        int m = v.size();
        vector<int> t(2 * m);
        for (int j = 0; j < m; j++) {
            for (int k = 0; k < m; k++) {
                t[j + k] += 1ll * v[j] * w[k] % mod;
                if (t[j + k] >= mod) t[j + k] -= mod;
            }
        }
        for (int j = 2 * m - 1; j >= m; j--) {
            for (int k = 1; k <= m; k++) {
                t[j - k] += 1ll * t[j] * rec[k - 1] % mod;
                if (t[j - k] >= mod) t[j - k] -= mod;
            }
        }
        t.resize(m);
        return t;
    };
    while (n) {
        if (n & 1) s = mul(s, t);
        t = mul(t, t);
        n >>= 1;
    }
    lint ret = 0;
    for (int i = 0; i < m; i++) ret += 1ll * s[i] * dp[i] % mod;
    return ret % mod;
}
int guess_nth_term(vector<int> x, lint n) {
    if (n < x.size()) return x[n];
    vector<int> v = berlekamp_massey(x);
    if (v.empty()) return 0;
    return get_nth(v, x, n);
}
struct elem { int x, y, v; }; // A_(x, y) <- v, 0-based. no duplicate please..
vector<int> get_min_poly(int n, vector<elem> M) {
    // smallest poly P such that A^i = sum_{j < i} {A^j \times P_j}
    vector<int> rnd1, rnd2;
    mt19937 rng(0x14004);
    auto randint = [&rng](int lb, int ub) {
        return uniform_int_distribution<int>(lb, ub)(rng);
    };
    for (int i = 0; i < n; i++) {
        rnd1.push_back(randint(1, mod - 1));
        rnd2.push_back(randint(1, mod - 1));
    }
    vector<int> gobs;
    for (int i = 0; i < 2 * n + 2; i++) {
        int tmp = 0;
        for (int j = 0; j < n; j++) {
            tmp += 1ll * rnd2[j] * rnd1[j] % mod;
            if (tmp >= mod) tmp -= mod;
        }
        gobs.push_back(tmp);
        vector<int> nxt(n);
        for (auto& i : M) {
            nxt[i.x] += 1ll * i.v * rnd1[i.y] % mod;
            if (nxt[i.x] >= mod) nxt[i.x] -= mod;
        }
        rnd1 = nxt;
    }
    auto sol = berlekamp_massey(gobs);
    reverse(sol.begin(), sol.end());
    return sol;
}
lint det(int n, vector<elem> M) {
    vector<int> rnd;
    mt19937 rng(0x14004);
    auto randint = [&rng](int lb, int ub) {
        return uniform_int_distribution<int>(lb, ub)(rng);
    };
    for (int i = 0; i < n; i++) rnd.push_back(randint(1, mod - 1));
    for (auto& i : M) {
        i.v = 1ll * i.v * rnd[i.y] % mod;
    }
    auto sol = get_min_poly(n, M)[0];
    if (n % 2 == 0) sol = mod - sol;
    for (auto& i : rnd) sol = 1ll * sol * ipow(i, mod - 2) % mod;
    return sol;
}


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

	cin >> k >> m >> n;
	vector <vector<long long int>> a(m, vector<long long int>(m, 0));
	vector <vector<long long int>> b(m, vector<long long int>(m, 0));
	for (int i = 0; i < m; i++)
	{
		cin >> p[i] >> q[i] >> r[i];
	}

	for (int i = 0; i < m; i++)
	{
		for (int j = 0; j <= m; j++)
		{
			if (q[i] == p[j] && r[i] == q[j])
			{
				//cout << i << ' ' << j << '\n';
				a[i][j] += 1;
			}
		}
		b[i][i] = 1;
	}

    vector <vector<long long int>> A, B;
    vector <int> v;
    v.push_back(0);
    v.push_back(0);
    v.push_back(0);
    A = a;
    B = b;
    for (int t = 0; t < 30; t++)
    {
        a = A;
        b = B;
        int N = t;
        while (N > 0)
        {
            if (N % 2 == 1)
            {
                b = mul(a, b);
            }
            a = mul(a, a);
            N /= 2;
        }

        long long int res = 0;

        for (int i = 0; i < m; i++)
        {
            for (int j = 0; j < m; j++)
            {
                if (p[i] == 1 && r[j] == 1) res += b[i][j]; res %= MOD;
            }
        }
        v.push_back(res);
    }

    cout << guess_nth_term(v, n) << '\n';
	return 0;
}
0