Luogu5123 [USACO18DEC]Cowpatibility

Luogu5123 [USACO18DEC]Cowpatibility

\(n\) 个物品,每个物品有 \(5\) 个属性,求有多少对物品交集为 \(\varnothing\)

\(n\leq5\times10^4,\ a_{i,\ j}\leq10^6\)

考虑原题的逆问题,即求有多少对物品交集非空

考虑容斥,如果一个大小为奇数的集合出现了 \(s\) 次,它的贡献为 \(\frac{s(s-1)}{2}\)

如果一个大小为偶数的集合出现了 \(s\) 次,它的贡献为 \(-\frac{s(s-1)}{2}\)

最终答案即为 \(\frac{n(n-1)}{2}-\) 贡献

现在的问题即为快速求出每种物品属性的子集在其它物品中出现的次数

可以用 map+vector 或者 map+hash

时间复杂度 \(O(32n\log n)\)

代码

#include <bits/stdc++.h>
using namespace std;

typedef unsigned long long u64;
const int maxn = 5e4 + 10;
int n;

#define iter map <u64, int> :: iterator
map <u64, int> cnt[2];

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    int a[5];
    for (int j = 0; j < 5; j++) {
      scanf("%d", a + j);
    }
    sort(a, a + 5);
    for (int S = 1; S < 32; S++) {
      u64 s = 0; int c = 0;
      for (int j = 0; j < 5; j++) {
        if (S >> j & 1) s = s * 998244353 + a[j], c++;
      }
      cnt[c & 1][s]++;
    }
  }
  u64 ans = 0;
  for (pair <u64, int> p : cnt[1]) {
    int s = p.second;
    ans += 1ll * s * (s - 1) / 2;
  }
  for (pair <u64, int> p : cnt[0]) {
    int s = p.second;
    ans -= 1ll * s * (s - 1) / 2;
  }
  printf("%lld", 1ll * n * (n - 1) / 2 - ans);
  return 0;
}

转载于:https://www.cnblogs.com/Juanzhang/p/10814742.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值