[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
容斥部分其实很好想,我们考虑枚举有哪些泉区的泉水可能相同,答案即为\(Ans=\sum\limits_{i=k}^6(-1)^{i-k}\times \binom{i}{k}\times work(i)\)
考虑如何求work?我们可以\(2^6\)枚举考虑哪些泉区,然后对其进行hash,记录下相同hash值的cnt,每次枚举的答案即为\(\sum\limits_{i=1}^k\dfrac{cnt_i(cnt_i+1)}{2}\)
不要用map,不要用map,不要用map,请自己手打hash,不要问我怎么知道的
/*problem from Wolfycz*/
#include<cmath>
#include<ctime>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define inf 0x7f7f7f7f
using namespace std;
typedef long long ll;
typedef unsigned int ui;
typedef unsigned long long ull;
inline char gc(){
static char buf[1000000],*p1=buf,*p2=buf;
return p1==p2&&(p2=(p1=buf)+fread(buf,1,1000000,stdin),p1==p2)?EOF:*p1++;
}
inline int frd(){
int x=0,f=1; char ch=gc();
for (;ch<'0'||ch>'9';ch=gc()) if (ch=='-') f=-1;
for (;ch>='0'&&ch<='9';ch=gc()) x=(x<<3)+(x<<1)+ch-'0';
return x*f;
}
inline int read(){
int x=0,f=1; char ch=getchar();
for (;ch<'0'||ch>'9';ch=getchar()) if (ch=='-') f=-1;
for (;ch>='0'&&ch<='9';ch=getchar()) x=(x<<3)+(x<<1)+ch-'0';
return x*f;
}
inline void print(int x){
if (x<0) putchar('-');
if (x>9) print(x/10);
putchar(x%10+'0');
}
const int N=1e5,Mod=5249857;
int C[10][10],v[10][N+10],cnt[10];
int n,k,limit;
struct S1{//hash链
int pre[N+10],now[Mod+10],val[N+10],tot;
ull child[N+10];
void clear(){
tot=0;
memset(now,0,sizeof(now));
memset(val,0,sizeof(val));
}
void insert(ull x){
int Now=x%Mod,p; ull son;
for (p=now[Now],son=child[p];p;p=pre[p],son=child[p]){
if (son==x){
val[p]++;
return;
}
}
pre[++tot]=now[Now],now[Now]=tot,child[tot]=x,val[tot]=1;
}
}Hash;
void Extract(int sta,int &len){for (int i=1;i<=6;++i) cnt[i]=sta&1,len+=sta&1,sta>>=1;}
ll work(int len){
Hash.clear();
for (int i=1;i<=n;++i){
ull tmp=0;
for (int j=1;j<=6;j++) if (cnt[j]) tmp=tmp*limit+v[j][i];
Hash.insert(tmp);
}
ll res=0;
for (int i=1;i<=Hash.tot;i++) res+=1ll*Hash.val[i]*(Hash.val[i]-1)/2;
return res;
}
bool check(int x){
for (int i=2;i*i<=x;i++) if (x%i==0) return 0;
return 1;
}
void prepare(){//得到随机膜数
srand(time(0));
int p=1e9;
while (true){
int x=1ll*rand()*rand()%p;
if (x<1000000) continue;
if (check(x)){
limit=x;
break;
}
}
}
int main(){
prepare();
n=read(),k=read(); ll Ans=0;
for (int i=1;i<=n;++i) for (int j=1;j<=6;++j) v[j][i]=read();
for (int i=0;i<=6;++i){
C[i][0]=1;
for (int j=1;j<=i;++j) C[i][j]=C[i-1][j-1]+C[i-1][j];
}
for (int sta=0;sta<1<<6;++sta){
int len=0;
Extract(sta,len);
if (len<k) continue;
ll res=work(len)*C[len][k];
(len-k)&1?Ans-=res:Ans+=res;
}
printf("%lld\n",Ans);
return 0;
}