比赛 NOIP2025模拟赛3 评测结果 AAAAAAAAAA
题目名称 Good Triplets 最终得分 100
用户昵称 wdsjl 运行时间 1.136 s
代码语言 C++ 内存使用 12.67 MiB
提交时间 2025-11-26 10:55:17
显示代码纯文本
#include <bits/stdc++.h>
#define int long long
using namespace std;

const int N = 1e6+10;

int p[N],n,c,cnt[N],lm,ans,now,sum;

int c2(int x){
	if(x<=1){
		return 0;
	}
    return x*(x-1)/2;
}
int c3(int x){
    if(x<=2){
        return 0;
    }
    return x*(x-1)*(x-2)/6;
}

signed main(){
	freopen("Triplets.in","r",stdin);
	freopen("Triplets.out","w",stdout); 
	scanf("%lld%lld",&n,&c);
	for(int i=1;i<=n;i++){
		scanf("%lld",&p[i]);
		cnt[p[i]]++;
	}
	lm=c/2;
	for(int i=1;i<=lm;i++){
		now+=cnt[i];
	} 
	for(int i=0;i<c;i++){
		ans+=cnt[i]*c2(now);
		if(c%2==0)ans+=c2(cnt[i])*(now-cnt[(i+lm)%c]);
		else ans+=c2(cnt[i])*now;
		now-=cnt[(i+1)%c];
        now+=cnt[(i+lm+1)%c];
	}
	for(int i=0;i<c;i++){
        ans+=c3(cnt[i]);
    }
    sum=c3(n);
    printf("%lld\n",sum-ans);
	return 0;
}