#include <bits/stdc++.h> #pragma GCC optimize("Ofast","unroll-loops") using namespace std; #ifdef LOCAL #include "debug.h" #else #define debug(...) #endif #define x first #define y second #define ir(a, x, b) ((a) <= (x) && (x) <= (b)) #define vec vector #define rep(i, a, b) for (int i = a; i < (b); ++i) #define all(x) (x).begin(), (x).end() using ll = long long; const ll MOD = ll(1e9)+7; struct mint { int value; mint(long long v = 0) { value = v % MOD; if (value < 0) value += MOD;} mint(long long a, long long b) : value(0){ *this += a; *this /= b;} mint& operator+=(mint const& b) {value += b.value; if (value >= MOD) value -= MOD; return *this;} mint& operator-=(mint const& b) {value -= b.value; if (value < 0) value += MOD;return *this;} mint& operator*=(mint const& b) {value = (long long)value * b.value % MOD;return *this;} friend mint mexp(mint a, long long e) { mint res = 1; while (e) { if (e&1) res *= a; a *= a; e >>= 1; } return res; } friend mint inverse(mint a) { return mexp(a, MOD - 2); } mint& operator/=(mint const& b) { return *this *= inverse(b); } friend mint operator+(mint a, mint const b) { return a += b; } friend mint operator-(mint a, mint const b) { return a -= b; } friend mint operator-(mint const a) { return 0 - a; } friend mint operator*(mint a, mint const b) { return a *= b; } friend mint operator/(mint a, mint const b) { return a /= b; } friend std::ostream& operator<<(std::ostream& os, mint const& a) {return os << a.value;} friend bool operator==(mint const& a, mint const& b) {return a.value == b.value;} friend bool operator!=(mint const& a, mint const& b) {return a.value != b.value;} }; // mint modpow(mint a, mint b) { // mint res = 1; // while (b) { // if (b % 2 == 1) { // res *= a; // } // b /= 2, a *= a; // } // return res; // } // int n, k; int e(int i, int j) { return n*i + j; } const int N = 3000; int over[N*N]; int tag[N*N]; int find(int x) { if (over[x] < 0) { return x; } return over[x] = find(over[x]); } void unify(int x, int y) { // if (over[x] == over[y] && over[x] > 0) return; if (over[x] > 0 && over[x] == over[y]) return; if (over[x] == y || over[y] == x) return; // debug(x, y); if ((x = find(x)) == (y = find(y))) return; if (over[x] > over[y]) swap(x, y); over[x] += over[y]; over[y] = x; tag[x] += tag[y]; } // int read() { // char c; // bool b = 0; // int n = 0; // for (; !ir('0', c, '9'); c = getchar()) {} // for (; !ir('0', c, '9'); c = getchar()) { // n = 10*n + (c-'0'); // } // return n; // } // int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> k; rep (i, 0, n*n) over[i] = -1; rep (j, 0, n) { rep (i, 0, j) { tag[e(i, j)] = 1; } } // printf("type "); rep (_, 0, k) { // debug(_); vec<int> perm(n); rep (i, 0, n) { cin >> perm[i]; --perm[i]; } rep (i, 0, n) { int v = n*i-1; rep (j, 0, n) { ++v; if (i == j) continue; unify(v, e(perm[i], perm[j])); } } } vec<bool> vis(n*n); mint res = n*(n-1)/2; rep (i, 0, n) { rep (j, 0, n) { if (i == j) continue; int x = find(e(i, j)); if (vis[x]) continue; vis[x] = true; mint tag_ = tag[x], sz_ = -over[x]; res -= tag_ * tag_ / sz_; } } cout << res << "\n"; return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 | #include <bits/stdc++.h> #pragma GCC optimize("Ofast","unroll-loops") using namespace std; #ifdef LOCAL #include "debug.h" #else #define debug(...) #endif #define x first #define y second #define ir(a, x, b) ((a) <= (x) && (x) <= (b)) #define vec vector #define rep(i, a, b) for (int i = a; i < (b); ++i) #define all(x) (x).begin(), (x).end() using ll = long long; const ll MOD = ll(1e9)+7; struct mint { int value; mint(long long v = 0) { value = v % MOD; if (value < 0) value += MOD;} mint(long long a, long long b) : value(0){ *this += a; *this /= b;} mint& operator+=(mint const& b) {value += b.value; if (value >= MOD) value -= MOD; return *this;} mint& operator-=(mint const& b) {value -= b.value; if (value < 0) value += MOD;return *this;} mint& operator*=(mint const& b) {value = (long long)value * b.value % MOD;return *this;} friend mint mexp(mint a, long long e) { mint res = 1; while (e) { if (e&1) res *= a; a *= a; e >>= 1; } return res; } friend mint inverse(mint a) { return mexp(a, MOD - 2); } mint& operator/=(mint const& b) { return *this *= inverse(b); } friend mint operator+(mint a, mint const b) { return a += b; } friend mint operator-(mint a, mint const b) { return a -= b; } friend mint operator-(mint const a) { return 0 - a; } friend mint operator*(mint a, mint const b) { return a *= b; } friend mint operator/(mint a, mint const b) { return a /= b; } friend std::ostream& operator<<(std::ostream& os, mint const& a) {return os << a.value;} friend bool operator==(mint const& a, mint const& b) {return a.value == b.value;} friend bool operator!=(mint const& a, mint const& b) {return a.value != b.value;} }; // mint modpow(mint a, mint b) { // mint res = 1; // while (b) { // if (b % 2 == 1) { // res *= a; // } // b /= 2, a *= a; // } // return res; // } // int n, k; int e(int i, int j) { return n*i + j; } const int N = 3000; int over[N*N]; int tag[N*N]; int find(int x) { if (over[x] < 0) { return x; } return over[x] = find(over[x]); } void unify(int x, int y) { // if (over[x] == over[y] && over[x] > 0) return; if (over[x] > 0 && over[x] == over[y]) return; if (over[x] == y || over[y] == x) return; // debug(x, y); if ((x = find(x)) == (y = find(y))) return; if (over[x] > over[y]) swap(x, y); over[x] += over[y]; over[y] = x; tag[x] += tag[y]; } // int read() { // char c; // bool b = 0; // int n = 0; // for (; !ir('0', c, '9'); c = getchar()) {} // for (; !ir('0', c, '9'); c = getchar()) { // n = 10*n + (c-'0'); // } // return n; // } // int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> k; rep (i, 0, n*n) over[i] = -1; rep (j, 0, n) { rep (i, 0, j) { tag[e(i, j)] = 1; } } // printf("type "); rep (_, 0, k) { // debug(_); vec<int> perm(n); rep (i, 0, n) { cin >> perm[i]; --perm[i]; } rep (i, 0, n) { int v = n*i-1; rep (j, 0, n) { ++v; if (i == j) continue; unify(v, e(perm[i], perm[j])); } } } vec<bool> vis(n*n); mint res = n*(n-1)/2; rep (i, 0, n) { rep (j, 0, n) { if (i == j) continue; int x = find(e(i, j)); if (vis[x]) continue; vis[x] = true; mint tag_ = tag[x], sz_ = -over[x]; res -= tag_ * tag_ / sz_; } } cout << res << "\n"; return 0; } |