读懂题意就是水题,按照出现次数对下标排一下序,暴力。。
#include<cstdio> #include<algorithm> #include<cstring> using namespace std; typedef long long ll; const int maxn = 1e4+77; ll cnt[maxn]; ll r[maxn]; bool cmp(int a,int b) { return cnt[a] > cnt[b]; } int main() { int T; scanf("%d",&T); for(int k = 1; k <= T; k++){ int M,N,C; scanf("%d%d%d",&M,&N,&C); memset(cnt,0,sizeof(cnt)); for(int i = 0; i < M; i++){ int tmp; scanf("%d",&tmp); cnt[tmp]++; } for(int i = 0; i < N; i++) { r[i] = i+1; } sort(r,r+N,cmp); ll amus = (r[0]*r[0])%C; ll best = cnt[r[0]]*amus; for(int i = 1; i < N; i++) { amus += (r[i]*r[i])%C; ll tmp = cnt[r[i]]*amus; best = max(best,tmp); } printf("Case %d: %I64d ",k,best); } return 0; }