Codecraft-17 and Codeforces Round #391 (Div. 1 + Div. 2, combined) C. Felicity is Coming! 哈希

来源:互联网 发布:webpackconfig.js 编辑:程序博客网 时间:2024/05/22 07:56

解法:对于pokermon a和b,只要它们在所有gym中出现的次数都是相同的,则变身的时候,种类可以互换。没有出现过的pokermon,归为所有gym都是0的情况,比赛的时候特判了这个反而没有过pretest。将数字划分到不同的集合内,则答案既是:(集合元素个数阶乘)的乘积。

用vector进行hash:

#include <bits/stdc++.h>using namespace std;typedef long long ll;const int maxm=1000005;const ll mod=1e9+7;vector<int> vec[maxm];int n,m,t,c;ll res,tc;int main(){    scanf("%d%d",&n,&m);    for (int i=0;i<n;++i) {        scanf("%d",&t);        while (t--) {            scanf("%d",&c);            vec[c].push_back(i);        }    }    sort(vec+1,vec+m+1);    res=1;    for (int i=1;i<=m;++i)        if (vec[i]==vec[i-1]) {            ++tc;            res=(res*tc)%mod;        } else            tc=1;    printf("%lld\n",res);    return 0;}

双哈希:

#include <bits/stdc++.h>using namespace std;#define fst first#define snd secondtypedef long long ll;typedef pair<ll,ll> pll;const ll mod=1e9+7;const ll mod1=1e9+7;const ll mod2=1e9+3;map<pll,ll> mp;map<ll,ll> mp2[100005];ll fac[1000006],pow1[100005],pow2[100005],ha1[1000005],ha2[1000005];int n,m,a,t,tf;int main(){    fac[0]=1LL;    fac[1]=1LL;    for (ll i=2;i<=1000005LL;++i)        fac[i]=(i*fac[i-1])%mod;    pow1[0]=1LL;    pow2[0]=1LL;    for (int i=1;i<=100000;++i) {        pow1[i]=(mod1*pow1[i-1])%mod2;        pow2[i]=(mod2*pow2[i-1])%mod1;    }    scanf("%d%d",&n,&m);    map<ll,ll>::iterator it;    for (int i=0;i<n;++i) {        scanf("%d",&t);        tf+=t;        for (int j=0;j<t;++j) {            scanf("%d",&a);            ++mp2[i][a];        }        for (it=mp2[i].begin();it!=mp2[i].end();++it) {            ha1[(*it).fst]=(ha1[(*it).fst]+(*it).snd*pow1[i])%mod2;            ha2[(*it).fst]=(ha2[(*it).fst]+(*it).snd*pow2[i])%mod1;        }    }    for (int i=1;i<=m;++i)        ++mp[pll(ha1[i],ha2[i])];    ll res=1LL;    map<pll,ll>::iterator it2;    for (it2=mp.begin();it2!=mp.end();++it2)        res=(res*fac[(*it2).snd])%mod;    printf("%lld\n",res);    return 0;}


1 0