// #ifndef LOCAL #pragma GCC optimize("O3") // #endif #include "bits/stdc++.h" using namespace std; #define all(x) x.begin(),x.end() template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << p.first << " " << p.second; } template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { string sep; for (const T &x : v) os << sep << x, sep = " "; return os; } #ifdef LOCAL #include "debug.h" #else #define debug(...) 42 #define ASSERT(...) 42 #endif typedef long long ll; typedef vector<int> vi; typedef vector<vi> vvi; typedef pair<int,int> pi; const int oo = 1e9; struct DSU{ vi par; int comps; DSU(int n) : par(n,-1),comps(n) { } void link(int a, int b) { if(par[a]>par[b]) { swap(a,b); } comps--; par[a]+=par[b]; par[b] = a; } bool unite(int a, int b) { int pa = find(a),pb = find(b); if(pa!=pb) link(pa,pb); return pa!=pb; } inline int find(int a) { while(true) { if(par[a]<0) return a; if(par[par[a]]<0) return par[a]; a = par[a] = par[par[a]]; } } }; const long long MD = 1e9+7; template<long long MOD=MD> struct mdint { int d; mdint () {d=0;} mdint (long long _d) : d(_d%MOD){ if(d<0) d+=MOD; }; friend mdint& operator+=(mdint& a, const mdint& o) { a.d+=o.d; if(a.d>=MOD) a.d-=MOD; return a; } friend mdint& operator-=(mdint& a, const mdint& o) { a.d-=o.d; if(a.d<0) a.d+=MOD; return a; } friend mdint& operator*=(mdint& a, const mdint& o) { return a = mdint((ll)a.d*o.d); } mdint operator*(const mdint& o) const { mdint res = *this; res*=o; return res; } mdint operator+(const mdint& o) const { mdint res = *this; res+=o; return res; } mdint operator-(const mdint& o) const { mdint res = *this; res-=o; return res; } mdint operator^(long long b) const { mdint tmp = 1; mdint power = *this; while(b) { if(b&1) { tmp = tmp*power; } power = power*power; b/=2; } return tmp; } friend mdint operator/=(mdint& a, const mdint& o) { a *= (o^(MOD-2)); return a; } mdint operator/(const mdint& o) { mdint res = *this; res/=o; return res; } bool operator==(const mdint& o) { return d==o.d;} bool operator!=(const mdint& o) { return d!=o.d;} friend istream& operator>>(istream& c, mdint& a) {return c >> a.d;} friend ostream& operator<<(ostream& c, const mdint& a) {return c << a.d;} }; using mint = mdint<MD>; /* Current complexity = O(n^2 k ) */ std::mt19937 rng(std::chrono::steady_clock::now().time_since_epoch().count()); template<class I> I rnd(I l,I r){return std::uniform_int_distribution<I>(l,r)(rng);} typedef array<short,3000> P; P operator*(const P& a, const P& b) { int n = a.size(); P res; for(int i=0;i<n;++i) { res[i] = a[b[i]]; } return res; } P operator/(const P& a, const P& b) { int n = a.size(); P res; for(int i=0;i<n;++i) { res[b[i]] = a[i]; } return res; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n,k; cin >> n >> k; auto t0 = chrono::steady_clock::now(); vector<P> a(k); DSU dsu(n*n); auto id = [&](int i, int j) { return i*n+j; }; /* given some cycles connect i,j to p[i],p[j] what does it mean? only i in C_1 get possibly connected with j in C_2 you know exactly who gets connected to where via: all nodes where i is fixed: receive i, ppppp gcd(C_i, C_j) times [j]. i for example gets connected */ // int iter=0; auto t1 = chrono::steady_clock::now(); cerr << (t1-t0).count()*1e-9 << " Before generating Input" << endl; for(auto& p : a) { iota(all(p),0); // for(int i=1;i<n;++i) { // swap(p[rnd(0,i)],p[i]); // } for(int i=0;i<n;++i) { cin >> p[i]; p[i]--; } // loop over gcd of some two cycles? // given gcd = g! // all i,j go to next step? // i,j --> p[i],p[j] } t1 = chrono::steady_clock::now(); cerr << (t1-t0).count()*1e-9 << " After generating Input" << endl; const int IT = min(k, int(3e8)/int(n*n)); for(int rep=0;rep<IT;++rep) { auto& p = a[rep]; for(int i=0;i<n;++i) for(int j=0;j<n;++j) { dsu.unite(id(i,j),id(p[i],p[j])); } } if(IT!=k) { t1 = chrono::steady_clock::now(); cerr << (t1-t0).count()*1e-9 << " After N^3" << endl; auto acc = a[0]; const int LIM = 12321; a.reserve(LIM); while(a.size()<LIM) { a.push_back(a[a.size()-k]); } t1 = chrono::steady_clock::now(); auto next = [&]() { int i= rnd(0,LIM-1); int j = rnd(0,LIM-1); while(i==j) j = rnd(0,LIM-1); if(rnd(0,1)==1) { a[i] = a[i]*a[j]; acc = acc*a[i]; } else { a[i] = a[i]/a[j]; acc = a[i]*acc; } }; for(int iter=0;iter<LIM*5;++iter) { next(); } t1 = chrono::steady_clock::now(); cerr << (t1-t0).count()*1e-9 << " After Generating Random elements" << endl; ll reps=0; while(true) { const int LG = 1; bool done=0; for(int i=0;i<n;++i) { t1 = chrono::steady_clock::now(); if((t1-t0).count()*1e-9>11.4) { done=1; break; } const int B = rnd(0,LIM-1); const int A = rnd(1,LIM-1); for(int j=0;j<n;++j) { reps++; for(int o=0;o<LG;++o) { int v = ((ll)A*(i*n+j) +B)%LIM; dsu.unite(id(i,j), id(a[v][acc[i]],a[v][acc[j]])); } if((reps&((1<<12)-1))==0) { next(); } } } if(done) break; } } vi inv(n*n); for(int i=0;i<n;++i) for(int j=0;j<i;++j) { inv[dsu.find(id(i,j))]++; } mint ans=0; for(int i=0;i<n;++i) for(int j=i+1;j<n;++j) { int who = dsu.find(id(i,j)); int sz = (-dsu.par[who]);; auto myinv = mint(inv[who]); ans+=myinv/sz; } cout << ans << '\n'; t1 = chrono::steady_clock::now(); cerr << (t1-t0).count()*1e-9 << " After Outputting Answer" << endl; }
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 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 | // #ifndef LOCAL #pragma GCC optimize("O3") // #endif #include "bits/stdc++.h" using namespace std; #define all(x) x.begin(),x.end() template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << p.first << " " << p.second; } template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { string sep; for (const T &x : v) os << sep << x, sep = " "; return os; } #ifdef LOCAL #include "debug.h" #else #define debug(...) 42 #define ASSERT(...) 42 #endif typedef long long ll; typedef vector<int> vi; typedef vector<vi> vvi; typedef pair<int,int> pi; const int oo = 1e9; struct DSU{ vi par; int comps; DSU(int n) : par(n,-1),comps(n) { } void link(int a, int b) { if(par[a]>par[b]) { swap(a,b); } comps--; par[a]+=par[b]; par[b] = a; } bool unite(int a, int b) { int pa = find(a),pb = find(b); if(pa!=pb) link(pa,pb); return pa!=pb; } inline int find(int a) { while(true) { if(par[a]<0) return a; if(par[par[a]]<0) return par[a]; a = par[a] = par[par[a]]; } } }; const long long MD = 1e9+7; template<long long MOD=MD> struct mdint { int d; mdint () {d=0;} mdint (long long _d) : d(_d%MOD){ if(d<0) d+=MOD; }; friend mdint& operator+=(mdint& a, const mdint& o) { a.d+=o.d; if(a.d>=MOD) a.d-=MOD; return a; } friend mdint& operator-=(mdint& a, const mdint& o) { a.d-=o.d; if(a.d<0) a.d+=MOD; return a; } friend mdint& operator*=(mdint& a, const mdint& o) { return a = mdint((ll)a.d*o.d); } mdint operator*(const mdint& o) const { mdint res = *this; res*=o; return res; } mdint operator+(const mdint& o) const { mdint res = *this; res+=o; return res; } mdint operator-(const mdint& o) const { mdint res = *this; res-=o; return res; } mdint operator^(long long b) const { mdint tmp = 1; mdint power = *this; while(b) { if(b&1) { tmp = tmp*power; } power = power*power; b/=2; } return tmp; } friend mdint operator/=(mdint& a, const mdint& o) { a *= (o^(MOD-2)); return a; } mdint operator/(const mdint& o) { mdint res = *this; res/=o; return res; } bool operator==(const mdint& o) { return d==o.d;} bool operator!=(const mdint& o) { return d!=o.d;} friend istream& operator>>(istream& c, mdint& a) {return c >> a.d;} friend ostream& operator<<(ostream& c, const mdint& a) {return c << a.d;} }; using mint = mdint<MD>; /* Current complexity = O(n^2 k ) */ std::mt19937 rng(std::chrono::steady_clock::now().time_since_epoch().count()); template<class I> I rnd(I l,I r){return std::uniform_int_distribution<I>(l,r)(rng);} typedef array<short,3000> P; P operator*(const P& a, const P& b) { int n = a.size(); P res; for(int i=0;i<n;++i) { res[i] = a[b[i]]; } return res; } P operator/(const P& a, const P& b) { int n = a.size(); P res; for(int i=0;i<n;++i) { res[b[i]] = a[i]; } return res; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n,k; cin >> n >> k; auto t0 = chrono::steady_clock::now(); vector<P> a(k); DSU dsu(n*n); auto id = [&](int i, int j) { return i*n+j; }; /* given some cycles connect i,j to p[i],p[j] what does it mean? only i in C_1 get possibly connected with j in C_2 you know exactly who gets connected to where via: all nodes where i is fixed: receive i, ppppp gcd(C_i, C_j) times [j]. i for example gets connected */ // int iter=0; auto t1 = chrono::steady_clock::now(); cerr << (t1-t0).count()*1e-9 << " Before generating Input" << endl; for(auto& p : a) { iota(all(p),0); // for(int i=1;i<n;++i) { // swap(p[rnd(0,i)],p[i]); // } for(int i=0;i<n;++i) { cin >> p[i]; p[i]--; } // loop over gcd of some two cycles? // given gcd = g! // all i,j go to next step? // i,j --> p[i],p[j] } t1 = chrono::steady_clock::now(); cerr << (t1-t0).count()*1e-9 << " After generating Input" << endl; const int IT = min(k, int(3e8)/int(n*n)); for(int rep=0;rep<IT;++rep) { auto& p = a[rep]; for(int i=0;i<n;++i) for(int j=0;j<n;++j) { dsu.unite(id(i,j),id(p[i],p[j])); } } if(IT!=k) { t1 = chrono::steady_clock::now(); cerr << (t1-t0).count()*1e-9 << " After N^3" << endl; auto acc = a[0]; const int LIM = 12321; a.reserve(LIM); while(a.size()<LIM) { a.push_back(a[a.size()-k]); } t1 = chrono::steady_clock::now(); auto next = [&]() { int i= rnd(0,LIM-1); int j = rnd(0,LIM-1); while(i==j) j = rnd(0,LIM-1); if(rnd(0,1)==1) { a[i] = a[i]*a[j]; acc = acc*a[i]; } else { a[i] = a[i]/a[j]; acc = a[i]*acc; } }; for(int iter=0;iter<LIM*5;++iter) { next(); } t1 = chrono::steady_clock::now(); cerr << (t1-t0).count()*1e-9 << " After Generating Random elements" << endl; ll reps=0; while(true) { const int LG = 1; bool done=0; for(int i=0;i<n;++i) { t1 = chrono::steady_clock::now(); if((t1-t0).count()*1e-9>11.4) { done=1; break; } const int B = rnd(0,LIM-1); const int A = rnd(1,LIM-1); for(int j=0;j<n;++j) { reps++; for(int o=0;o<LG;++o) { int v = ((ll)A*(i*n+j) +B)%LIM; dsu.unite(id(i,j), id(a[v][acc[i]],a[v][acc[j]])); } if((reps&((1<<12)-1))==0) { next(); } } } if(done) break; } } vi inv(n*n); for(int i=0;i<n;++i) for(int j=0;j<i;++j) { inv[dsu.find(id(i,j))]++; } mint ans=0; for(int i=0;i<n;++i) for(int j=i+1;j<n;++j) { int who = dsu.find(id(i,j)); int sz = (-dsu.par[who]);; auto myinv = mint(inv[who]); ans+=myinv/sz; } cout << ans << '\n'; t1 = chrono::steady_clock::now(); cerr << (t1-t0).count()*1e-9 << " After Outputting Answer" << endl; } |