Submission #5307232


Source Code Expand

Copy
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; (i) < (int)(n); ++ (i))
#define REP3(i, m, n) for (int i = (m); (i) < (int)(n); ++ (i))
using namespace std;
template <int32_t MOD>
struct mint {
int32_t value;
mint() = default;
mint(int32_t value_) : value(value_) {}
inline mint<MOD> operator + (mint<MOD> other) const { int32_t c = this->value + other.value; return mint<MOD>(c >= MOD ? c - MOD : c); }
inline mint<MOD> operator * (mint<MOD> other) const { int32_t c = (int64_t)this->value * other.value % MOD; return mint<MOD>(c < 0 ? c + MOD : c); }
inline mint<MOD> & operator += (mint<MOD> other) { this->value += other.value; if (this->value >= MOD) this->value -= MOD; return *this; }
inline mint<MOD> & operator *= (mint<MOD> other) { this->value = (int64_t)this->value * other.value % MOD; if (this->value < 0) this->value += MOD; return *this; }
};
constexpr int MOD = 1e9 + 7;
constexpr int SQRT = 500;
class solver {
 
 
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; (i) < (int)(n); ++ (i))
#define REP3(i, m, n) for (int i = (m); (i) < (int)(n); ++ (i))
using namespace std;

template <int32_t MOD>
struct mint {
    int32_t value;
    mint() = default;
    mint(int32_t value_) : value(value_) {}
    inline mint<MOD> operator + (mint<MOD> other) const { int32_t c = this->value + other.value; return mint<MOD>(c >= MOD ? c - MOD : c); }
    inline mint<MOD> operator * (mint<MOD> other) const { int32_t c = (int64_t)this->value * other.value % MOD; return mint<MOD>(c < 0 ? c + MOD : c); }
    inline mint<MOD> & operator += (mint<MOD> other) { this->value += other.value; if (this->value >= MOD) this->value -= MOD; return *this; }
    inline mint<MOD> & operator *= (mint<MOD> other) { this->value = (int64_t)this->value * other.value % MOD; if (this->value < 0) this->value += MOD; return *this; }
};


constexpr int MOD = 1e9 + 7;

constexpr int SQRT = 500;
class solver {
    int n;
    vector<vector<mint<MOD> > > a;
    vector<vector<vector<mint<MOD> > > > b;
    map<pair<int, int>, mint<MOD> > memo;

public:
    solver(int n_, const vector<vector<mint<MOD> > > & a_)
            : n(n_), a(a_) {
        b.resize(n);
        REP (i, n) {
            int k = min<int>(SQRT, a[i].size());
            b[i].resize(k);
            REP3 (k1, 1, k) {
                b[i][k1].resize(k1);
                REP (j, k1) {
                    for (int j1 = j; j1 < a[i].size(); j1 += k1) {
                        b[i][k1][j] += a[i][j1];
                    }
                }
            }
        }
    }

    mint<MOD> operator () (int x, int y) {
        auto key = minmax({ x, y });
        if (memo.count(key)) return memo[key];

        if (a[x].size() < a[y].size()) {
            swap(x, y);
        }
        int kx = a[x].size();
        int ky = a[y].size();
        assert (kx >= ky);

        mint<MOD> acc = 0;
        if (kx >= SQRT and ky < SQRT) {
            REP (j, ky) {
                acc += b[x][ky][j] * a[y][j];
            }
        } else {
            REP (j, kx) {
                acc += a[x][j] * a[y][j % ky];
            }
        }

        return memo[key] = acc;
    }
};

int main() {
    int n; cin >> n;
    vector<vector<mint<MOD> > > a(n);
    REP (i, n) {
        int k; cin >> k;
        a[i].resize(k);
        REP (j, k) {
            int a_i_j; cin >> a_i_j;
            a[i][j] = a_i_j;
        }
    }

    solver f(n, a);

    int q; cin >> q;
    while (q --) {
        int x, y; cin >> x >> y;
        cout << f(x, y).value << endl;
    }
    return 0;
}

Submission Info

Submission Time
Task H - Doki Doki Programming Clubs!
User kimiyuki
Language C++14 (GCC 5.4.1)
Score 200
Code Size 2602 Byte
Status AC
Exec Time 1292 ms
Memory 163968 KB

Judge Result

Set Name All
Score / Max Score 200 / 200
Status
AC × 42
Set Name Test Cases
All 0-sample-1, 0-sample-2, 0-sample1, 0-sample2, 1-random-large-0, 1-random-large-1, 1-random-large-2, 1-random-large-3, 1-random-large-4, 1-random-large-5, 1-random-large-6, 1-random-large-7, 1-random-large-8, 1-random-large-9, 2-challenge-0, 2-challenge-1, 2-challenge-2, 2-challenge-3, 2-challenge-4, 2-challenge-5, 2-challenge-6, 2-challenge-7, 2-challenge-8, 2-challenge-9, 3-challenge2-0, 3-challenge2-1, 3-challenge2-2, 3-challenge2-3, 3-challenge2-4, 3-challenge2-5, 3-challenge2-6, 3-challenge2-7, 3-challenge2-8, 3-challenge2-9, 4-maximum-0, 4-maximum-1, 5-increase-0, 5-increase-1, 6-increase2-0, 6-increase2-1, 7-nomemo-0, 7-nomemo-1
Case Name Status Exec Time Memory
0-sample-1 AC 5 ms 256 KB
0-sample-2 AC 1 ms 256 KB
0-sample1 AC 1 ms 256 KB
0-sample2 AC 1 ms 256 KB
1-random-large-0 AC 1016 ms 31872 KB
1-random-large-1 AC 1117 ms 23936 KB
1-random-large-2 AC 783 ms 47872 KB
1-random-large-3 AC 1093 ms 26880 KB
1-random-large-4 AC 1108 ms 24320 KB
1-random-large-5 AC 915 ms 40320 KB
1-random-large-6 AC 1102 ms 27008 KB
1-random-large-7 AC 1038 ms 28416 KB
1-random-large-8 AC 1071 ms 27520 KB
1-random-large-9 AC 976 ms 35840 KB
2-challenge-0 AC 861 ms 26112 KB
2-challenge-1 AC 807 ms 14720 KB
2-challenge-2 AC 791 ms 11776 KB
2-challenge-3 AC 782 ms 11648 KB
2-challenge-4 AC 785 ms 11904 KB
2-challenge-5 AC 790 ms 12416 KB
2-challenge-6 AC 784 ms 13568 KB
2-challenge-7 AC 790 ms 14464 KB
2-challenge-8 AC 790 ms 14720 KB
2-challenge-9 AC 790 ms 15104 KB
3-challenge2-0 AC 867 ms 26112 KB
3-challenge2-1 AC 854 ms 14464 KB
3-challenge2-2 AC 806 ms 11904 KB
3-challenge2-3 AC 803 ms 11776 KB
3-challenge2-4 AC 796 ms 12160 KB
3-challenge2-5 AC 797 ms 12672 KB
3-challenge2-6 AC 792 ms 13824 KB
3-challenge2-7 AC 804 ms 14720 KB
3-challenge2-8 AC 793 ms 14976 KB
3-challenge2-9 AC 801 ms 15360 KB
4-maximum-0 AC 760 ms 47488 KB
4-maximum-1 AC 756 ms 47488 KB
5-increase-0 AC 1292 ms 163968 KB
5-increase-1 AC 1281 ms 163968 KB
6-increase2-0 AC 1168 ms 71168 KB
6-increase2-1 AC 1167 ms 71168 KB
7-nomemo-0 AC 973 ms 4736 KB
7-nomemo-1 AC 989 ms 4736 KB


2025-04-05 (Sat)
23:33:59 +00:00