| 比赛 |
NOIP2025模拟赛3 |
评测结果 |
AAAAAAAAAA |
| 题目名称 |
Good Triplets |
最终得分 |
100 |
| 用户昵称 |
李奇文 |
运行时间 |
0.908 s |
| 代码语言 |
C++ |
内存使用 |
28.57 MiB |
| 提交时间 |
2025-11-26 10:33:22 |
显示代码纯文本
#include<bits/stdc++.h>
#define int long long
using namespace std;
template<typename T>T read(){
T res=0ll,f=1ll;
char ch=getchar();
for(;!isdigit(ch);ch=getchar())if(ch=='-')f=-1ll;
for(;isdigit(ch);ch=getchar()) res=(res<<3ll)+(res<<1ll)+(ch^48ll);
return res*f;
}
template<typename T>void write(T x){
if(x<0ll) x=-x,putchar('-');
int sta[20ll],top=0ll;
do{
sta[++top]=x%10ll+'0';
x/=10ll;
}while(x);
while(top){
putchar(sta[top--]);
}
}
const int N=1e6+5;
int n,c,a[N],ans,cnt[N<<1],sum[N<<1];
int C(int x,int y){
int res=1ll;
for(int i(1);i<=y;++i) res=res*(x-y+i)/(i);
return res;
}
signed main(){
freopen("Triplets.in","r",stdin);
freopen("Triplets.out","w",stdout);
n=read<int>(),c=read<int>();
for(int i(1ll);i<=n;++i){
a[i]=read<int>();
++cnt[a[i]];
++cnt[a[i]+c];
}
for(int i(1ll);i<=(c<<1ll);++i){
sum[i]=sum[i-1ll]+cnt[i];
}
for(int i(1ll);i<=n;++i){
ans+=C(sum[a[i]+(c>>1ll)]-sum[a[i]],2ll);
}
for(int i(1ll);i<=c;++i){
if(cnt[i]>=3ll){
ans+=C(cnt[i],3ll);
}
if(cnt[i]>=2ll){
ans+=C(cnt[i],2ll)*(sum[i+(c%2ll)+(c>>1ll)-1ll]-sum[i]);
}
}
ans=C(n,3ll)-ans;
write(ans);
return 0;
}