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

【思路】

容斥原理+Hash

恰有k个元素相同的对数=至少k+1个相同*C(k+1,k) - 至少k+2个相同*C(k+2,k) + ……

枚举状态i,如果是101表示至少1和3两个相同,把n个年份关于i构造一个hash,然后放入hash中统计。这里只是关于位是1的构造hash,其他位都忽略了,所以得到的是至少有多少个相同的数目。

学了个hash表的写法,新姿势get :)

【代码】

 #include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std; typedef unsigned long long ull;
typedef long long ll;
const int N = ;
const int B = ; namespace Hash_set
{
struct node {
node* nxt;
ull H; int v;
node(){}
node(node* _,ull __) :
nxt(_),H(__),v() {}
}*head[N],mempool[N],*C=mempool;
int vis[N],kase;
void init() {
kase++; C=mempool;
}
int& insert(ull st) {
int pos=st%;
if(vis[pos]!=kase) {
vis[pos]=kase; head[pos]=0x0;
}
for(node* p=head[pos];p;p=p->nxt)
if(p->H==st) return p->v;
head[pos]=new (C++) node(head[pos],st);
return head[pos]->v;
}
} int n,K,a[N][],C[][]; ll calc(int st)
{
using namespace Hash_set;
ll ans=;
init();
for(int i=;i<n;i++) {
ull hash=;
for(int j=;j<;j++)
if( st&(<<j) )
hash=(hash*B+a[i][j]);
int& val=insert(hash);
ans+=(val++);
}
return ans;
} void get_C()
{
for(int i=;i<=;i++) {
C[i][]=C[i][i]=;
for(int j=;j<i;j++)
C[i][j]=C[i-][j-]+C[i-][j];
}
} void read(int& x) {
char c=getchar(); int f=; x=;
while(!isdigit(c)){if(c=='-')f=-; c=getchar();}
while(isdigit(c)) x=x*+c-'',c=getchar();
} int main()
{
get_C();
read(n),read(K);
for(int i=;i<n;i++)
for(int j=;j<;j++) read(a[i][j]);
ll ans=;
for(int i=;i<;i++) {
int cnt=;
for(int j=;j<;j++)
if(i&(<<j)) cnt++;
if(cnt>=K) ans+=((cnt-K)&?-:)*calc(i)*C[cnt][K];
}
printf("%lld",ans);
return ;
}

bzoj 3198 [Sdoi2013]spring(容斥原理+Hash)的更多相关文章

  1. BZOJ 3198: [Sdoi2013]spring [容斥原理 哈希表]

    3198: [Sdoi2013]spring 题意:n个物品6个属性,求有多少不同的年份i,j满足有k个属性对应相等 一开始读错题了,注意是对应相等 第i个属性只能和第i个属性对应 容斥一下 \[ 恰 ...

  2. [BZOJ 3198] [Sdoi2013] spring 【容斥 + Hash】

    题目链接:BZOJ - 3198 题目分析 题目要求求出有多少对泉有恰好 k 个值相等. 我们用容斥来做. 枚举 2^6 种状态,某一位是 1 表示这一位相同,那么假设 1 的个数为 x . 答案就是 ...

  3. BZOJ 3198 SDOI2013 spring

    为什么SDOI省选一年考两次容斥原理? 我们很容易发现>=k个相等时很好计算的 但是我们要求恰好k个,那么我们容斥即可 至于计算>=k个相等,首先我们枚举相等位置,对每个串对应位置做一遍h ...

  4. 3198: [Sdoi2013]spring【容斥原理+hash】

    容斥是ans= 至少k位置相等对数C(k,k)-至少k+1位置相等对数C(k+1,k)+至少k+2位置相等对数*C(k+2,k) -- 然后对数的话2^6枚举状态然后用hash表统计即可 至于为什么要 ...

  5. bzoj 3197 [Sdoi2013]assassin(Hash+DP+KM)

    Description Input Output Sample Input 4 1 2 2 3 3 4 0 0 1 1 1 0 0 0 Sample Output 1 HINT [思路] Hash,D ...

  6. sdoi2013 spring(hash+容斥)

    大体思路是先求出来\(f[i]\)代表有至少\(i\)个位置相同的点对数. 然后就已经没什么好害怕的了(跟BZOJ3622一样) 然后这个\(f[i\)]怎么求呢? 最无脑的方法就是枚举位置,然后\( ...

  7. bzoj3198[Sdoi2013]spring 容斥+hash

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

  8. [Sdoi2013] [bzoj 3198] spring (hash+容斥原理)

    题目描述 给出nnn个666维坐标,求有多少对点对满足恰好mmm个位置相等 1<=n<=1051<=n<=10^51<=n<=105 0<=k<=60& ...

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

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

随机推荐

  1. 1509 -- Glass Beads POJ

    题意:求一个字符串的最小表示的开始下标 就当模板题写了 把字符串重复一遍,再建后缀自动机,贪心的选最小字典序在上面走len步 因为走出来的一定是子串,长度又是len,所以一定是原来的字符串旋转得到的, ...

  2. 3.4 spring- lookup-method 子元素的使用与解析

    1. lookup-method的应用: 1.1 子元素lookup-method 似乎不是很常用,但是在某些时候他的确是非常有用的属性,通常我们称它为 "获取器注入" . 引用 ...

  3. hdu 3778

    简单的dfs  但繁琐的可以了 0.0 #include<cstdio> #include<cstring> #include<algorithm> using s ...

  4. 安装ADT Cannot complete the install because one or more required items could not be found.

    点击进行安装,将会弹出 错误提示是: Cannot complete the install because one or more required items could not be found ...

  5. iOS如何把导航默认的返回按钮设置成“返回”

    版权声明:本CSDN博客所有文章不更新,请关注标哥博客:http://www.henishuo.com/ - (void)addBackItemWithAction:(SEL)action { if  ...

  6. 理解extern char s[100]与extern char *s

    在x.c中定义了一个字符数组 char s[100],在l.c中进行引用extern char s[200], 有些c程序新手经常把它写成extern char *s. 这两种写法的含义一样吗? 首先 ...

  7. 【HDOJ】4355 Party All the Time

    好久没做过三分的题目了. /* 4355 */ #include <iostream> #include <sstream> #include <string> # ...

  8. poj 3414 Pots ( bfs )

    题目:http://poj.org/problem?id=3414 题意:给出了两个瓶子的容量A,B, 以及一个目标水量C, 对A.B可以有如下操作: FILL(i)        fill the ...

  9. 51nod水题记

    妈呀51nod已经刷不动了又开始跟bzoj一样总是得看题解了...那么发一下总结吧... 1051:最大子矩阵 #include<cstdio> #include<cstring&g ...

  10. hdu4177:Super Mario

    主席树+离散化.给一段区间.多次询问[l,r]中有多少个数小于k.啊主席树用指针版写出来优美多了QAQ... #include<cstdio> #include<cstring> ...