bzoj3198 [Sdoi2013]spring 哈希表+容斥

11 篇文章 0 订阅
10 篇文章 0 订阅

题意:有n个六元组,给出一个数k,要求这n个组中有多少对之间有k元是相同的。/

这种玩意儿一眼容斥啊。
首先2^6枚举是哪k位,然后用hash求出在这种情况下有多少对是相同的,设为f[i].
那么答案就是 f[k]Ckkf[k+1]Ckk+1....
注意到容斥系数是组合数,原因是对应i和i+1会有交集,明显不能只是正负1来计算。

#include<cstdio>
#include<algorithm>
#include<cstring>
#define fo(i,a,b) for(int i=a;i<=b;i++)
#define fd(i,a,b) for(int i=a;i>=b;i--)
using namespace std;
const int N=1e5+5;
int n,m;
typedef long long ll;
const int mo1=1e6+7;
const int mo2=998244353;
bool vis[N];
ll ans;
int a[N][7],fac[10],q[N],sz;
int head[mo1],next[N],go[N],s[N];
inline int C(int n,int m)
{
    return fac[n]/fac[m]/fac[n-m];
}
inline int add(int x1,int x2)
{
    for(int i=head[x1];i;i=next[i])
    {
        int v=go[i];
        if(v==x2)
        {
            s[i]++;
            return s[i]-1;
        }
    }
    go[++sz]=x2;
    s[sz]=1;next[sz]=head[x1];
    head[x1]=sz;
    return 0;
}
inline void calc(int cnt)
{
    ll tot=0;sz=0;
    int top=0;
    fo(i,1,n)
    {
        int hash1=0,hash2=0;
        fo(j,1,6)
        if(vis[j])
        {
            hash1=(1ll*hash1*233+1ll*a[i][j])%mo1;
            hash2=(1ll*hash2*1221+1ll*a[i][j])%mo2;
        }
        tot+=add(hash1,hash2);
        q[++top]=hash1;
    }
    while (top)head[q[top]]=0,top--;
    int c=0;
    if ((cnt-m)%2==0)c=1;else c=-1;
    ans=(ans+1ll*c*tot*C(cnt,m));
}
inline void dfs(int x,int cnt)
{
    if (x>6)
    {
        if (cnt>=m)calc(cnt);
        return;
    }
    dfs(x+1,cnt);
    vis[x]=1;
    dfs(x+1,cnt+1);
    vis[x]=0;
}
int main()
{
    scanf("%d%d",&n,&m);
    fac[0]=1;
    fo(i,1,6)fac[i]=fac[i-1]*i;
    fo(i,1,n)
    {
        fo(j,1,6)
        scanf("%d",&a[i][j]);
    }
    dfs(1,0);
    printf("%lld\n",ans);
    return 0;
} 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值