容斥原理+哈希表

恰好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. inux IO 内核参数调优 之 参数调节和场景分析

    http://backend.blog.163.com/blog/static/2022941262013112081215609/ http://blog.csdn.net/icycode/arti ...

  2. HeatMap(热图)的原理和实现

    先来看两张图: (1)10年世界杯决赛,冠军西班牙队中门将.后卫.中场及前锋的跑位热图 通过热图,我们可以很清楚的看出四个球员在比赛中跑动位置的差异. (2)历史地震震源位置的热图 也可以很清楚的看出 ...

  3. DeepFM

    DeepFM integrates the architectures of FM and deep neural networks (DNN). It models low-order featur ...

  4. python发声

    python发声 学习了:http://www.jb51.net/article/62644.htm import winsound winsound.Beep(600,1000) #其中600表示声 ...

  5. 根据wsdl反向生成webservice服务端(3种方法)

    前言 正常情况下,都是我们项目组创建一个webservice服务端,客户通过我们提供的wsdl地址生成客户端并进行访问:但是最近和一个国企做接口对接,他们却只提供给我们wsdl,需要我们根据wsdl生 ...

  6. 具体解释window.history

    Window.history保存用户在一个会话期间的站点訪问记录,用户每次訪问一个新的URL即创建一个新的历史记录. history.go().history.back().history.forwa ...

  7. [Node.js] Trigger a File Download in Express

    Downloading and saving a file is a common scenario when building out your web application. Using Exp ...

  8. javascript 复制粘贴操作

    function CopyCode(key){ var trElements = document.all.tags("tr");//获取tr元素 var i; for(i = 0 ...

  9. LeetCode232 Implement Queue using Stacks Java 题解

    题目: Implement the following operations of a queue using stacks. push(x) -- Push element x to the bac ...

  10. Seesion和Cookie详解2

    转载来自: https://www.toutiao.com/a6693986851193094664/?tt_from=weixin&utm_campaign=client_share& ...