[USACO18DEC]Cowpatibility(容斥 or bitset优化暴力)
题面
题意:
给出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优化暴力)的更多相关文章
- HDU - 4059: The Boss on Mars (容斥 拉格朗日 小小的优化搜索)
pro: T次询问,每次给出N(N<1e8),求所有Σi^4 (i<=N,且gcd(i,N)==1) ; sol: 因为N比较小,我们可以求出素因子,然后容斥. 主要问题就是求1到P的 ...
- bzoj4810 [Ynoi2017]由乃的玉米田 bitset优化+暴力+莫队
[Ynoi2017]由乃的玉米田 Time Limit: 30 Sec Memory Limit: 256 MBSubmit: 917 Solved: 447[Submit][Status][Di ...
- 洛谷 P2634 [国家集训队]聪聪可可-树分治(点分治,容斥版) +读入挂+手动O2优化吸点氧才过。。。-树上路径为3的倍数的路径数量
P2634 [国家集训队]聪聪可可 题目描述 聪聪和可可是兄弟俩,他们俩经常为了一些琐事打起来,例如家中只剩下最后一根冰棍而两人都想吃.两个人都想玩儿电脑(可是他们家只有一台电脑)……遇到这种问题,一 ...
- BZOJ3589 动态树[树剖/暴力/容斥]
操作0,显然直接线段树解决. 操作1,瓶颈在于重叠的链只算一次.在线段树上来看,如果一个区间被覆盖了,那么只算这个区间,子树里面也就不管了. 考虑对节点打标记来表示是否覆盖.但是,如果统一打完之后,并 ...
- LOJ #2541. 「PKUWC 2018」猎人杀(容斥 , 期望dp , NTT优化)
题意 LOJ #2541. 「PKUWC 2018」猎人杀 题解 一道及其巧妙的题 , 参考了一下这位大佬的博客 ... 令 \(\displaystyle A = \sum_{i=1}^{n} w_ ...
- HDU 5514 Frogs 容斥定理
Frogs Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5514 De ...
- [UOJ422][集训队作业2018]小Z的礼物——轮廓线DP+min-max容斥
题目链接: [集训队作业2018]小Z的礼物 题目要求的就是最后一个喜欢的物品的期望得到时间. 根据$min-max$容斥可以知道$E(max(S))=\sum\limits_{T\subseteq ...
- BZOJ4671 异或图(容斥+线性基)
题意 定义两个结点数相同的图 \(G_1\) 与图 \(G_2\) 的异或为一个新的图 \(G\) ,其中如果 \((u, v)\) 在 \(G_1\) 与 \(G_2\) 中的出现次数之和为 \(1 ...
- 【BZOJ5287】[HNOI2018]毒瘤(动态规划,容斥)
[BZOJ5287][HNOI2018]毒瘤(动态规划,容斥) 题面 BZOJ 洛谷 题解 考场上想到的暴力做法是容斥: 因为\(m-n\le 10\),所以最多会多出来\(11\)条非树边. 如果就 ...
随机推荐
- hadoop 错误
错误:DataXceiver error processing WRITE_BLOCK operation 2014-05-06 15:21:30,378 ERROR org.apache.hadoo ...
- PHP IDE PHPStorm配置支持友好Laravel代码提示方法
PHPStorm神器可以支持更友好的laravel框架代码提示(点击查看),只需要执行如下才做:第一步:在项目的composer.json中添加如下一行 代码如下: "require&quo ...
- ASP.NET mvc 验证码 (转)
ASP.net 验证码(C#) MVC http://blog.163.com/xu_shuhao/blog/static/5257748720101022697309/ 网站添加验证码,主要为防止机 ...
- js两个浮点数相减出现多位小数的bug
- mysql获取汉字首字母函数
DELIMITER ;;CREATE FUNCTION `GET_FIRST_PINYIN_CHAR`(PARAM VARCHAR(255)) RETURNS VARCHAR(2) CHARSET u ...
- vue+nodejs+express+mysql 建立一个在线网盘程序
vue+nodejs+express+mysql 建立一个在线网盘程序 目录 vue+nodejs+express+mysql 建立一个在线网盘程序 第一章 开发环境准备 1.1 开发所用工具简介 1 ...
- 技能get:用HTML5实现波浪效果
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- Java集合类——Set、List、Map、Queue接口
目录 Java 集合类的基本概念 Java 集合类的层次关系 Java 集合类的应用场景 一. Java集合类的基本概念 在编程中,常需要集中存放多个数据,数组是一个很好的选择,但数组的长度需提前指定 ...
- git的初始配置(简易的命令行)
Git 全局设置: git config --global user.name "You name" git config --global user.email "Yo ...
- phpstudy配置域名后apache无法启动
1.设置域名后重启 apache停止了 检查步骤1.php路径不要有中文,phpstudy重新安装在无中文路径 2.检查80端口是否被占用,如果被占用可以停止该程序或者修改apache/nginx 端 ...