-
-
Save ArthurLoboLobo/5579beaa821e0c4c2d633c53a89e4c57 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<bits/stdc++.h> | |
using namespace std; | |
const long long inf = (long long) 1e18 + 10; | |
const int inf1 = (int) 1e9 + 10; | |
#define int long long | |
#define dbl long double | |
#define endl '\n' | |
#define sc second | |
#define fr first | |
#define mp make_pair | |
#define pb push_back | |
#define all(x) x.begin(), x.end() | |
const int maxn = 550; | |
int n, a[maxn], mod, ds[maxn], dsz[maxn]; | |
int exp(int x, int y) { | |
int ans = 1; | |
for(int i = 0; i <= 30; i++) { | |
if((1<<i)&y) ans = ans*x%mod; | |
x = x*x%mod; | |
} | |
return ans; | |
} | |
int find(int v) { | |
if(ds[v] == v) return v; | |
return ds[v] = find(ds[v]); | |
} | |
void join(int u, int v) { | |
if(dsz[u] < dsz[v]) swap(u,v); | |
dsz[u]+= dsz[v]; | |
ds[v] = u; | |
} | |
void solve() { | |
cin >> n >> mod; | |
for(int i = 1; i <= n; i++) { | |
cin >> a[i]; | |
ds[i] = i; | |
dsz[i] = 1; | |
} | |
vector<pair<int,pair<int,int>>> edgs; | |
for(int i = 1; i <= n; i++) { | |
for(int j = i+1; j <= n; j++) { | |
edgs.pb(mp((exp(a[i],a[j])+exp(a[j],a[i]))%mod,mp(i,j))); | |
} | |
} | |
sort(all(edgs),greater<pair<int,pair<int,int>>>()); | |
int ans = 0; | |
for(auto X : edgs) { | |
int w = X.fr; | |
int u = X.sc.fr; | |
int v = X.sc.sc; | |
u = find(u); | |
v = find(v); | |
if(u == v) continue; | |
ans+= w; | |
join(u,v); | |
} | |
cout << ans << endl; | |
} | |
int32_t main() { | |
ios::sync_with_stdio(false); cin.tie(0); | |
// freopen("in.in", "r", stdin); | |
// freopen("out.out", "w", stdout); | |
int tt = 1; | |
// cin >> tt; | |
while(tt--) { | |
solve(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment