贪心模拟,边权越小越靠近根节点。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=200010;
ll s[maxn];
int k,m,n;
ll p;
int powermod(int x,int n)
{
int res=1;
while(n>0)
{
if(n&1) res=(res*x);
x=x*x;
n=n>>1;
}
return res;
}
int main()
{
ios::sync_with_stdio(false);
while(cin>>k>>m>>n>>p)
{
ll ans=0;
for(int i = 1; i <= k; i++) cin>>s[i];
sort(s+1,s+k+1);
ll S = (powermod(n, m)-n)/(n-1);
for(int i = 1; i <= S; i++)
{
s[i] = (s[i] + s[(i-1)/n]) % p;
ans = (ans + s[i]) % p;
}
cout<<ans<<endl;
}
return 0;
}