容斥原理+哈希表

恰好k个,那么上容斥原理,我们先2^6枚举相同的位置,用哈希表判断有多少个对应位置相同的元素,然后用容斥原理计算,似乎这里的容斥没有那么简单,详见这里 http://www.cnblogs.com/candy99/p/mobius.html, 要乘上组合数计算

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
typedef long long ll;
const int N = , mod = ;
int n, k;
ll ans;
int c[][], a[N][];
namespace Hash_List {
int cnt, tot;
int h[N], bel[N];
struct List {
int nxt, id, cnt;
} e[N << ];
void ini() { ++cnt; }
bool same(int x, int y, int s) {
for(int i = ; i < ; ++i) if(s & ( << i) && a[x][i] != a[y][i]) return false;
return true;
}
int hash(int *a, int s) {
int h = ;
for(int i = ; i < ; ++i) if(s & ( << i)) h = ((h * % mod + a[i] % mod) % mod + mod) % mod;
return h;
}
int insert(int id, int s) {
int val = hash(a[id], s);
if(bel[val] != cnt) h[val] = , bel[val] = cnt; // 如果这个没有被标记过,那么就暴力查找,否则直接插入
for(int i = h[val]; i; i = e[i].nxt) if(same(e[i].id, id, s)) { ++e[i].cnt; return e[i].cnt - ; }
e[++tot] = (List) {h[val], id, }, h[val] = tot;
return ;
}
} using Hash_List :: insert; using Hash_List :: ini;
int one(int x) {
int ret = ;
while(x) ++ret, x &= (x - );
return ret;
}
int main()
{
scanf("%d%d", &n, &k);
c[][] = ;
for(int i = ; i <= ; ++i)
{
c[i][] = ;
for(int j = ; j <= i; ++j) c[i][j] = c[i - ][j] + c[i - ][j - ];
}
for(int i = ; i <= n; ++i)
for(int j = ; j < ; ++j) scanf("%d", &a[i][j]);
for(int i = ; i < ; ++i)
{
int t = one(i);
if(t < k) continue;
Hash_List :: ini();
for(int j = ; j <= n; ++j) ans += (((t - k) & ) ? - : ) * (ll)Hash_List :: insert(j, i) * (ll)c[t][k];
}
printf("%lld\n", ans);
return ;
}

bzoj3198的更多相关文章

  1. BZOJ3198 [Sdoi2013]spring 哈希 容斥原理

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ3198 题意概括 有n(1<=n<=100000)组数据,每组数据6个数. 现在问有几对 ...

  2. BZOJ3198[SDOI2013]SPRING

    Description Input Output Sample Input 3 3 1 2 3 4 5 6 1 2 3 0 0 0 0 0 0 4 5 6 Sample Output 2 HINT 题 ...

  3. bzoj3198[Sdoi2013]spring 容斥+hash

    3198: [Sdoi2013]spring Time Limit: 40 Sec  Memory Limit: 256 MBSubmit: 1143  Solved: 366[Submit][Sta ...

  4. [BZOJ3622]已经没有什么好害怕的了(容斥DP)

    给定两个数组a[n]与b[n](数全不相等),两两配对,求“a比b大”的数对比“b比a大”的数对个数多k的配对方案数. 据说做了这题就没什么题好害怕的了,但感觉实际上这是一个套路题,只是很难想到. 首 ...

随机推荐

  1. Java并发编程,Condition的await和signal等待通知机制

    Condition简介 Object类是Java中所有类的父类, 在线程间实现通信的往往会应用到Object的几个方法: wait(),wait(long timeout),wait(long tim ...

  2. 心脏滴血漏洞复现(CVE-2014-0160)

    漏洞范围: OpenSSL 1.0.1版本 漏洞成因: Heartbleed漏洞是由于未能在memcpy()调用受害用户输入内容作为长度参数之前正确进 行边界检查.攻击者可以追踪OpenSSL所分配的 ...

  3. Mybatis批量插入与批量删除

    转自:http://www.cnblogs.com/liaojie970/p/5577018.html (一)批量插入 Mapper.xml: <?xml version="1.0&q ...

  4. 【Todo】Spark运行架构

    接上一篇:http://www.cnblogs.com/charlesblc/p/6108105.html 上一篇文章中主要参考的是 Link 这个系列下一篇讲的是Idea,没有细看,又看了再下一篇: ...

  5. OC - 读歌词

    类的头文件: #import <Foundation/Foundation.h> //FILE_PATH是文件名称. #define FILE_PATH @"/Users/qia ...

  6. 配置 yum 源相关

    1. 修改yum配置 http://www.cnblogs.com/shuaixf/archive/2011/11/30/2268496.html 2. centos安装 epel 源 https:/ ...

  7. Eclipse中移除native层编译支持

    1. .project 文件 删除全部包括 org.eclipse.cdt前缀的xml结点元素.以下是一个包括cdt的完整编译配置文件. <?xml version="1.0" ...

  8. 一场由股票提醒助手插件引发的血案——浅入浅出 jquery autocomplete

    我没有学过前端,所以这篇文章注定要班门弄斧了. 通常,须要用到什么技术什么语言时,我才去学,学了也不一定掌握,就是记不住!所以如今明确了.学习的时候,亦或是攻克难点的时候,一定要记录下来.并不一定非要 ...

  9. C#语言基础语句

    case,switch,break的使用 Console.WriteLine("1.汉堡"); Console.WriteLine("2.薯条"); Conso ...

  10. Comparable 和 Comparator的理解

    对Comparable 的解释 Comparable是一个排序接口 此接口给实现类提供了一个排序的方法,此接口有且只有一个方法 public int compareTo(T o); compareTo ...