题面

题意:

给出n个五元组(一个五元组的五个数互不相同),我们称两个五元组不和谐,当且仅当任意元素都不相同,求有多少对五元组不和谐。

\(Solution:\)

很容易想到 Ans = 总共对数-和谐对数

而和谐对数包括5种:

  • 一个数相同
  • 二个数相同
  • ...
  • 五个数相同

所以我们就可以容斥了。

对于每次读进来的一组,我们计算它与之前读进来的和谐的个数。

于是我们就 \(2^5\) 枚举状态,然后 + (容斥系数) * (之前状态个数), 状态可以用map记因为状态有多个数, 所以用字符串作状态。

\(Source\)

#include <map>
#include <cmath>
#include <cctype>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <assert.h>
#include <algorithm> using namespace std; #define fir first
#define sec second
#define pb push_back
#define mp make_pair
#define LL long long
#define INF (0x3f3f3f3f)
#define mem(a, b) memset(a, b, sizeof (a))
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define Debug(x) cout << #x << " = " << x << endl
#define tralve(i, x) for (register int i = head[x]; i; i = nxt[i])
#define For(i, a, b) for (register int (i) = (a); (i) <= (b); ++ (i))
#define Forr(i, a, b) for (register int (i) = (a); (i) >= (b); -- (i))
#define file(s) freopen(s".in", "r", stdin), freopen(s".out", "w", stdout)
#define ____ debug("go\n") namespace io {
static char buf[1<<21], *pos = buf, *end = buf;
inline char getc()
{ return pos == end && (end = (pos = buf) + fread(buf, 1, 1<<21, stdin), pos == end) ? EOF : *pos ++; }
inline int rint() {
register int x = 0, f = 1;register char c;
while (!isdigit(c = getc())) if (c == '-') f = -1;
while (x = (x << 1) + (x << 3) + (c ^ 48), isdigit(c = getc()));
return x * f;
}
inline LL rLL() {
register LL x = 0, f = 1; register char c;
while (!isdigit(c = getc())) if (c == '-') f = -1;
while (x = (x << 1ll) + (x << 3ll) + (c ^ 48), isdigit(c = getc()));
return x * f;
}
inline void rstr(char *str) {
while (isspace(*str = getc()));
while (!isspace(*++str = getc()))
if (*str == EOF) break;
*str = '\0';
}
template<typename T>
inline bool chkmin(T &x, T y) { return x > y ? (x = y, 1) : 0; }
template<typename T>
inline bool chkmax(T &x, T y) { return x < y ? (x = y, 1) : 0; }
} const int N = 4e5 + 1; map<string, long long> M; LL n;
int main() {
#ifndef ONLINE_JUDGE
file("Cowpatibility");
#endif
string a[6]; cin >> n;
LL ans = n * (n - 1) / 2;
For (i, 1, n) {
For (j, 1, 5) cin >> a[j];
sort(a + 1, a + 6);
LL res = 0;
for (int s = 1; s < (1<<5); ++ s) {
string str = ""; int ou = 0;
for (int j = 1; j < 6; ++ j) {
if (s & (1 << j - 1)) {
ou ++;
str += "," + a[j];
}
}
if (ou & 1) res += M[str] ++;
else res -= M[str] ++;
}
ans -= res;
}
cout << ans << endl;
}

然后因为bitset常数过于优秀, 总复杂度\(O(\frac{1}{32}\times n^2)\), 虽然不是正解但也能过,而且跑的飞快。

#include <bitset>
#include <tr1/unordered_map>
#include <cctype>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <assert.h>
#include <algorithm> using namespace std; #define fir first
#define sec second
#define pb push_back
#define mp make_pair
#define LL long long
#define INF (0x3f3f3f3f)
#define mem(a, b) memset(a, b, sizeof (a))
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define Debug(x) cout << #x << " = " << x << endl
#define tralve(i, x) for (register int i = head[x]; i; i = nxt[i])
#define For(i, a, b) for (register int (i) = (a); (i) <= (b); ++ (i))
#define Forr(i, a, b) for (register int (i) = (a); (i) >= (b); -- (i))
#define file(s) freopen(s".in", "r", stdin), freopen(s".out", "w", stdout)
#define ____ debug("go\n") namespace io {
static char buf[1<<21], *pos = buf, *end = buf;
inline char getc()
{ return pos == end && (end = (pos = buf) + fread(buf, 1, 1<<21, stdin), pos == end) ? EOF : *pos ++; }
inline int rint() {
register int x = 0, f = 1;register char c;
while (!isdigit(c = getc())) if (c == '-') f = -1;
while (x = (x << 1) + (x << 3) + (c ^ 48), isdigit(c = getc()));
return x * f;
}
inline LL rLL() {
register LL x = 0, f = 1; register char c;
while (!isdigit(c = getc())) if (c == '-') f = -1;
while (x = (x << 1ll) + (x << 3ll) + (c ^ 48), isdigit(c = getc()));
return x * f;
}
inline void rstr(char *str) {
while (isspace(*str = getc()));
while (!isspace(*++str = getc()))
if (*str == EOF) break;
*str = '\0';
}
template<typename T>
inline bool chkmin(T &x, T y) { return x > y ? (x = y, 1) : 0; }
template<typename T>
inline bool chkmax(T &x, T y) { return x < y ? (x = y, 1) : 0; }
}using namespace io; const int N = 5e4 + 1; tr1::unordered_map<int, bitset<N> > buc; int n, a[N][6];
int main() {
#ifndef ONLINE_JUDGE
file("Cowpatibility");
#endif
n = rint();
For (i, 1, n) {
For (j, 1, 5)
buc[ a[i][j] = rint() ].set(i);
}
bitset<N> tmp;
int ans = 0;
For (i, 1, n) {
tmp.reset();
For (j, 1, 5)
tmp |= buc[a[i][j]];
ans += n - tmp.count();
}
cout << ans / 2 << endl;
}

[USACO18DEC]Cowpatibility(容斥 or bitset优化暴力)的更多相关文章

  1. HDU - 4059: The Boss on Mars (容斥 拉格朗日 小小的优化搜索)

    pro: T次询问,每次给出N(N<1e8),求所有Σi^4 (i<=N,且gcd(i,N)==1) ; sol:  因为N比较小,我们可以求出素因子,然后容斥.  主要问题就是求1到P的 ...

  2. bzoj4810 [Ynoi2017]由乃的玉米田 bitset优化+暴力+莫队

    [Ynoi2017]由乃的玉米田 Time Limit: 30 Sec  Memory Limit: 256 MBSubmit: 917  Solved: 447[Submit][Status][Di ...

  3. 洛谷 P2634 [国家集训队]聪聪可可-树分治(点分治,容斥版) +读入挂+手动O2优化吸点氧才过。。。-树上路径为3的倍数的路径数量

    P2634 [国家集训队]聪聪可可 题目描述 聪聪和可可是兄弟俩,他们俩经常为了一些琐事打起来,例如家中只剩下最后一根冰棍而两人都想吃.两个人都想玩儿电脑(可是他们家只有一台电脑)……遇到这种问题,一 ...

  4. BZOJ3589 动态树[树剖/暴力/容斥]

    操作0,显然直接线段树解决. 操作1,瓶颈在于重叠的链只算一次.在线段树上来看,如果一个区间被覆盖了,那么只算这个区间,子树里面也就不管了. 考虑对节点打标记来表示是否覆盖.但是,如果统一打完之后,并 ...

  5. LOJ #2541. 「PKUWC 2018」猎人杀(容斥 , 期望dp , NTT优化)

    题意 LOJ #2541. 「PKUWC 2018」猎人杀 题解 一道及其巧妙的题 , 参考了一下这位大佬的博客 ... 令 \(\displaystyle A = \sum_{i=1}^{n} w_ ...

  6. HDU 5514 Frogs 容斥定理

    Frogs Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5514 De ...

  7. [UOJ422][集训队作业2018]小Z的礼物——轮廓线DP+min-max容斥

    题目链接: [集训队作业2018]小Z的礼物 题目要求的就是最后一个喜欢的物品的期望得到时间. 根据$min-max$容斥可以知道$E(max(S))=\sum\limits_{T\subseteq ...

  8. BZOJ4671 异或图(容斥+线性基)

    题意 定义两个结点数相同的图 \(G_1\) 与图 \(G_2\) 的异或为一个新的图 \(G\) ,其中如果 \((u, v)\) 在 \(G_1\) 与 \(G_2\) 中的出现次数之和为 \(1 ...

  9. 【BZOJ5287】[HNOI2018]毒瘤(动态规划,容斥)

    [BZOJ5287][HNOI2018]毒瘤(动态规划,容斥) 题面 BZOJ 洛谷 题解 考场上想到的暴力做法是容斥: 因为\(m-n\le 10\),所以最多会多出来\(11\)条非树边. 如果就 ...

随机推荐

  1. py faster rcnn+ 1080Ti+cudnn5.0

    看了py-faster-rcnn上的issue,原来大家都遇到各种问题. 我要好好琢磨一下,看看到底怎么样才能更好地把GPU卡发挥出来.最近真是和GPU卡较上劲了. 上午解决了g++的问题不是. 然后 ...

  2. LeetCode6.Z字形变换 JavaScript

    将一个给定字符串根据给定的行数,以从上往下.从左到右进行 Z 字形排列. 比如输入字符串为 "LEETCODEISHIRING" 行数为 3 时,排列如下: L C I R E T ...

  3. iOS | CAShapeLayer转场动画

    什么也不说了,作为一名乐于分享技术的小开发,直接先上个样式最为直观贴切,有需要的朋友可以直接拿过去用. 需要demo请点击这里 :github 在这个demo中,核心为选用画布CAShapeLayer ...

  4. ISCC 2018——write up

    WEB Web1-比较数字大小 直接修改input 标签里的maxlength 为999突破长度限制,使得能输入大于999 的数,然后随便输一个数字就行了 或者post修改值 Web2-Web01 s ...

  5. PTA 最多删除3个字符(DP) - 30分

    给定一个全部由小写英文字母组成的字符串,允许你至多删掉其中 3 个字符,结果可能有多少种不同的字符串? 输入格式: 输入在一行中给出全部由小写英文字母组成的.长度在区间 [4, 1] 内的字符串. 输 ...

  6. BZOJ1086: [SCOI2005]王室联邦(贪心,分块?)

    Time Limit: 10 Sec  Memory Limit: 162 MBSec  Special JudgeSubmit: 2610  Solved: 1584[Submit][Status] ...

  7. Java分享笔记:Java网络编程--TCP程序设计

    [1] TCP编程的主要步骤 客户端(client): 1.创建Socket对象,构造方法的形参列表中需要InetAddress类对象和int型值,用来指明对方的IP地址和端口号. 2.通过Socke ...

  8. ABAP术语-BW (Business Information Warehouse)

    BW (Business Information Warehouse) 原文:http://www.cnblogs.com/qiangsheng/archive/2008/01/14/1037761. ...

  9. XAMPP中的MySQL与本地MySQL冲突的问题

    学习SQL时在本地中先安装了MySQL,后来因为项目需要又安装了XAMPP集成环境,今天在启动项目的时候发现启动MySQL各种问题(期望启动的是XAMPP中的MySQL服务),在Navicat中显示成 ...

  10. jquery和vue分别对input输入框手机号码格式化(344)

    jQuery function fomatterTel(val, old) {//val: 当前input的值,old: input上次的值 var str = ""; var t ...