[bzoj3198][Sdoi2013]spring——容斥+哈希表

题目大意:

给定一些六元组,求有多少对\((i,j)\)满足\(i,j\)中恰有\(k\)对对应相同。

思路:

考虑\(\geq k\)的对数然后简单容斥。
考虑到只有六元组,于是直接枚举子集之后把那几位提取出来,单独把那几位哈希然后计算相同的对数有多少。
但是哈希冲突很大,直接手写哈希表即可。

#include<bits/stdc++.h>

#define REP(i,a,b) for(int i=a,i##_end_=b;i<=i##_end_;++i)
#define DREP(i,a,b) for(int i=a,i##_end_=b;i>=i##_end_;--i)
#define debug(x) cout<<#x<<"="<<x<<" "
#define pii pair<int,int>
#define fi first
#define se second
#define mk make_pair
#define pb push_back
typedef long long ll;

using namespace std;

void File(){
	freopen("bzoj3198.in","r",stdin);
	freopen("bzoj3198.out","w",stdout);
}

template<typename T>void read(T &_){
	_=0; T f=1; char c=getchar();
	for(;!isdigit(c);c=getchar())if(c=='-')f=-1;
	for(;isdigit(c);c=getchar())_=(_<<1)+(_<<3)+(c^'0');
	_*=f;
}

const ll base=97;
const ll mod=100003;
const int maxn=1e5+10;
const int maxm=10+10;
int n,m,all=(1<<6)-1;
ll a[maxn][10],f[maxm],g[maxm];

struct Hash_Table{
	int S;
	vector<pii>to[maxn];
	void reset(int S0){
		S=S0;
		REP(i,0,mod-1)to[i].clear();
	}
	bool judge(int x,int y){
		REP(i,1,6)if((1<<(i-1))&S)
			if(a[x][i]!=a[y][i])
				return false;
		return true;
	}
	ll insert(int x){
		int num=0;
		REP(i,1,6)if((1<<(i-1))&S)
			num=(num*base+a[x][i])%mod;
		REP(i,0,to[num].size()-1)
			if(judge(x,to[num][i].fi)){
				++to[num][i].se;
				return to[num][i].se-1;
			}
		to[num].pb(mk(x,1));
		return 0;
	}
}T;

ll calc(int S){
	T.reset(S);
	ll ret=0;
	REP(i,1,n)ret+=T.insert(i);
	return ret;
}

ll C(ll x,ll y){
	ll ret=1;
	REP(i,x-y+1,x)ret*=i;
	REP(i,1,y)ret/=i;
	return ret;
}

int main(){
	File();
	read(n),read(m);
	REP(i,1,n)REP(j,1,6)read(a[i][j]);

	REP(S,0,all){
		int k=__builtin_popcount(S);
		f[k]+=calc(S);
	}

	DREP(i,6,m){
		g[i]=f[i];
		REP(j,i+1,6)g[i]-=C(j,i)*g[j];
	}

	printf("%lld\n",g[m]);

	return 0;
}

posted @ 2019-01-05 14:42  ylsoi  阅读(156)  评论(0编辑  收藏  举报