Educational Codeforces Round 115 (Rated for Div. 2) D. Training Session(组合数学 思维)

简介: Educational Codeforces Round 115 (Rated for Div. 2) D. Training Session(组合数学 思维)

linkkkk

题意:

给出n nn个人,每个人有a , b a,ba,b两种属性,保证没有两个人的a , b a,ba,b属性都相同。

问能够选出三个人满足以下条件之一的方案数:


三个人的a aa属性各不相同

三个人的b bb属性各不相同

思路:

逆向思维,合法的方案等于总方案数减去不合法的方案数。

总方案数相当于C n 3

不合法的方案数如何计算呢。

先处理出c n t a i表示属性a = i出现的次数,同理,也处理出c n t b i

假设有A , B , C三个人,此时A的a属性和B的a属性一样,A的b属性和C的b属性一样,这样的方案数有( c n t a i − 1 ) ∗ ( c n t b i − 1 )− 1是因为要将本身去掉

代码:

// Problem: D. Training Session
// Contest: Codeforces - Educational Codeforces Round 115 (Rated for Div. 2)
// URL: https://codeforces.com/contest/1598/problem/D
// Memory Limit: 256 MB
// Time Limit: 2000 ms
// 
// Powered by CP Editor (https://cpeditor.org)
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;typedef unsigned long long ull;
typedef pair<ll,ll>PLL;typedef pair<int,int>PII;typedef pair<double,double>PDD;
#define I_int ll
inline ll read(){ll x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}return x*f;}
#define read read()
#define rep(i, a, b) for(int i=(a);i<=(b);++i)
#define dep(i, a, b) for(int i=(a);i>=(b);--i)
ll ksm(ll a,ll b,ll p){ll res=1;while(b){if(b&1)res=res*a%p;a=a*a%p;b>>=1;}return res;}
const int maxn=2e5+7,maxm=1e6+7,mod=1e9+7;
ll a[maxn],b[maxn],cnta[maxn],cntb[maxn],n;
void solve()
{
  ll n=read;
  rep(i,1,n) cnta[i]=cntb[i]=0;
  rep(i,1,n) a[i]=read,b[i]=read,cnta[a[i]]++,cntb[b[i]]++;
  ll ans=n*(n-1)*(n-2)/6;
  rep(i,1,n)
    ans=ans-(cnta[a[i]]-1)*(cntb[b[i]]-1);
  cout<<ans<<endl;
}
int main(){
  int _=read;
  while(_--) solve();
  return 0;
}



目录
相关文章
Educational Codeforces Round 113 (Rated for Div. 2)A. Balanced Substring
Educational Codeforces Round 113 (Rated for Div. 2)A. Balanced Substring
60 0
|
人工智能 Windows
Educational Codeforces Round 113 (Rated for Div. 2) C - Jury Meeting (思维 组合数)
Educational Codeforces Round 113 (Rated for Div. 2) C - Jury Meeting (思维 组合数)
72 0
|
人工智能
Educational Codeforces Round 113 (Rated for Div. 2) B. Chess Tournament(思维 构造)
Educational Codeforces Round 113 (Rated for Div. 2) B. Chess Tournament(思维 构造)
67 0
AtCoder Beginner Contest 214 D.Sum of Maximum Weights (思维 并查集)
AtCoder Beginner Contest 214 D.Sum of Maximum Weights (思维 并查集)
88 0
|
机器学习/深度学习 Java
codeforces Educational Codeforces Round 49 (Rated for Div. 2) C题
刚开始拿到这题很懵逼,知道了别人的思路之后开始写,但是还是遇到很多坑,要求求P2/S最大。p=a b。就是求(a2+ b2 +2ab)/ab最大,也就是a/b +b/a最大。那么题意就很明显了。
87 0
|
人工智能
Educational Codeforces Round 98 (Rated for Div. 2)B-Toy Blocks
You are asked to watch your nephew who likes to play with toy blocks in a strange way. He has n boxes and the i-th box has ai blocks. His game consists of two steps: he chooses an arbitrary box i; he tries to move all blocks from the i-th box to other boxes.
236 0