#include<cstdio> #include<algorithm> #include<utility> #include<string> #include<vector> #include<map> #include<set> #include<queue> #include<stack> #include<cmath> #include<iostream> // macros #define FORE(c, a, b) for(int c=a; c<= int(b); ++c) #define FORD(c, a, b) for(int c=a; c>= int(b); --c) #define FORIT(it, cont, cont_t) for(cont_t::iterator it = cont.begin(); it != cont.end(); ++it) #define REP(i, n) for(int i = 0; i < (n); ++i) #define ALL(a) a.begin(), a.end() #define PR(n) printf("%d ", (int) (n)) #define PRL(n) printf("%lld ", (ll) (n)) #define SC(n) scanf("%d", &n); #define SC2(n, m) scanf("%d%d", &n, &m); #define xx first #define yy second #define pb push_back #define mp make_pair #define itr iterator #define dbg if(0) #define prd dbg printf using namespace std; typedef long long ll; typedef long double ld; typedef unsigned int uint; typedef vector<int> vi; typedef set<int> si; typedef map<int, int> mi; typedef pair<int, int> pi; typedef vector<pi> vii; typedef vector<vi> vvi; typedef vector<vii> vvii; typedef set<pi> spi; const int MOD = 1000000007, NMAX = 3005; int n, k, vis_count; ll ans; int perm[NMAX][NMAX]; vvii cc; int visited[NMAX][NMAX]; ll power(ll x, int m) { prd("power %lld %d\n", x, m); if (m==0) return 1; if (m==1) return x; if (m % 2) return (x * power(x, m-1)) % MOD; ll y = power(x, m/2); return (y * y) % MOD; } int main() { SC2(n,k); REP(i,k) { FORE(j,1,n) { SC(perm[i][j]); } } //printf("read %lu %d\n", cc.max_size(), 7); FORE(x,1,n) { FORE(y,1,n) if(x != y) if (!visited[x][y]) { int plus_count = 0, minus_count = 0, empty_run = 0; //vii comp; queue<pi> bfs; bfs.push(mp(x,y)); visited[x][y] = 1; vis_count++; while(!bfs.empty()) { pi uv = bfs.front(); bfs.pop(); //comp.pb(uv); if (uv.xx < uv.yy) plus_count++; else minus_count++; if (vis_count == n * (n-1)) { continue; dbg if (bfs.size() % 1000 == 0) printf("%d / %d, queue: %d, cc: %d\n", vis_count, n * (n-1), (int) bfs.size(), (int) cc.size()); } dbg if (empty_run) continue; empty_run = 1; REP(i,k) { pi next_uv = mp(perm[i][uv.xx], perm[i][uv.yy]); if (!visited[next_uv.xx][next_uv.yy]) { visited[next_uv.xx][next_uv.yy] = 1; bfs.push(next_uv); empty_run = 0; vis_count++; dbg if (vis_count % 1000 == 0 || vis_count > 8996000) printf("%d / %d, queue: %d, cc: %d\n", vis_count, n * (n-1), (int) bfs.size(), (int) cc.size()); } } } //cc.pb(comp); ll q = power((ll) plus_count + minus_count, MOD - 2); ll p = (ll) plus_count * minus_count; p = p % MOD; ans += (ll) p * q; ans = ans % MOD; //printf("p %lld q %lld ans %lld\n", p, q, ans); } } //printf("bfs done\n"); dbg REP(i, cc.size()) { REP(j, cc[i].size()) { printf("(%d %d) ", cc[i][j].xx, cc[i][j].yy); } printf("\n"); } /* REP(i, cc.size()) { int cc_plus = 0; REP(j, cc[i].size()) if (cc[i][j].xx < cc[i][j].yy) cc_plus++; ll q = power((ll) cc[i].size(), MOD - 2); ll p = (ll) cc_plus * (cc[i].size() - cc_plus); p = p % MOD; ans += (ll) p * q; ans = ans % MOD; //printf("p %lld q %lld ans %lld\n", p, q, ans); }*/ PRL(ans); 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 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 | #include<cstdio> #include<algorithm> #include<utility> #include<string> #include<vector> #include<map> #include<set> #include<queue> #include<stack> #include<cmath> #include<iostream> // macros #define FORE(c, a, b) for(int c=a; c<= int(b); ++c) #define FORD(c, a, b) for(int c=a; c>= int(b); --c) #define FORIT(it, cont, cont_t) for(cont_t::iterator it = cont.begin(); it != cont.end(); ++it) #define REP(i, n) for(int i = 0; i < (n); ++i) #define ALL(a) a.begin(), a.end() #define PR(n) printf("%d ", (int) (n)) #define PRL(n) printf("%lld ", (ll) (n)) #define SC(n) scanf("%d", &n); #define SC2(n, m) scanf("%d%d", &n, &m); #define xx first #define yy second #define pb push_back #define mp make_pair #define itr iterator #define dbg if(0) #define prd dbg printf using namespace std; typedef long long ll; typedef long double ld; typedef unsigned int uint; typedef vector<int> vi; typedef set<int> si; typedef map<int, int> mi; typedef pair<int, int> pi; typedef vector<pi> vii; typedef vector<vi> vvi; typedef vector<vii> vvii; typedef set<pi> spi; const int MOD = 1000000007, NMAX = 3005; int n, k, vis_count; ll ans; int perm[NMAX][NMAX]; vvii cc; int visited[NMAX][NMAX]; ll power(ll x, int m) { prd("power %lld %d\n", x, m); if (m==0) return 1; if (m==1) return x; if (m % 2) return (x * power(x, m-1)) % MOD; ll y = power(x, m/2); return (y * y) % MOD; } int main() { SC2(n,k); REP(i,k) { FORE(j,1,n) { SC(perm[i][j]); } } //printf("read %lu %d\n", cc.max_size(), 7); FORE(x,1,n) { FORE(y,1,n) if(x != y) if (!visited[x][y]) { int plus_count = 0, minus_count = 0, empty_run = 0; //vii comp; queue<pi> bfs; bfs.push(mp(x,y)); visited[x][y] = 1; vis_count++; while(!bfs.empty()) { pi uv = bfs.front(); bfs.pop(); //comp.pb(uv); if (uv.xx < uv.yy) plus_count++; else minus_count++; if (vis_count == n * (n-1)) { continue; dbg if (bfs.size() % 1000 == 0) printf("%d / %d, queue: %d, cc: %d\n", vis_count, n * (n-1), (int) bfs.size(), (int) cc.size()); } dbg if (empty_run) continue; empty_run = 1; REP(i,k) { pi next_uv = mp(perm[i][uv.xx], perm[i][uv.yy]); if (!visited[next_uv.xx][next_uv.yy]) { visited[next_uv.xx][next_uv.yy] = 1; bfs.push(next_uv); empty_run = 0; vis_count++; dbg if (vis_count % 1000 == 0 || vis_count > 8996000) printf("%d / %d, queue: %d, cc: %d\n", vis_count, n * (n-1), (int) bfs.size(), (int) cc.size()); } } } //cc.pb(comp); ll q = power((ll) plus_count + minus_count, MOD - 2); ll p = (ll) plus_count * minus_count; p = p % MOD; ans += (ll) p * q; ans = ans % MOD; //printf("p %lld q %lld ans %lld\n", p, q, ans); } } //printf("bfs done\n"); dbg REP(i, cc.size()) { REP(j, cc[i].size()) { printf("(%d %d) ", cc[i][j].xx, cc[i][j].yy); } printf("\n"); } /* REP(i, cc.size()) { int cc_plus = 0; REP(j, cc[i].size()) if (cc[i][j].xx < cc[i][j].yy) cc_plus++; ll q = power((ll) cc[i].size(), MOD - 2); ll p = (ll) cc_plus * (cc[i].size() - cc_plus); p = p % MOD; ans += (ll) p * q; ans = ans % MOD; //printf("p %lld q %lld ans %lld\n", p, q, ans); }*/ PRL(ans); return 0; } |