关闭

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

标签: CodeForcesSTLc++
421人阅读 评论(0) 收藏 举报
分类:

大体题意:

有n 个体育馆每个体育馆里有 不同的精灵同样数字的精灵进化的结果是一样的不同数字进化的结果一定不同有多少个不同的进化方法使得每个体育馆进化后 和原来一样?

思路:

两个精灵可以互相进化的话,那么它们两个在所有的体育馆的数量和位置都必须一样。这样找出同类的所有数量后,求阶乘即可,因为它们内部随便排列都是合适的。

然后把所有的阶乘乘起来即可!

思路明确后,方法就很多了! 就随便写了。

例如:

我们可以把每种精灵的位置和数量 放在multiset里,最后把这个放在map里,最后枚举每个multiset的数量!

详细见代码:

#include <bits/stdc++.h>
#define fi first
#define se second
#define ps push_back
#define mr make_pair
using namespace std;
const int inf = 0x3f3f3f3f;
const int mod = 1e9+7;
typedef long long ll;
typedef unsigned long long LLU;
const double eps = 1e-10;
const double pi = acos(-1.0);
const int maxn = 1000000 + 10;
map<multiset<int>,int>mp;
multiset<int>g[maxn];
map<multiset<int>,int>::iterator it;
ll J(int x){
    ll ans = 1LL;
    for (int i = 1; i <= x; ++i){
        ans = (ans % mod * i % mod) % mod;
    }
    return ans;
}
int main(){
    int n, m;
    scanf("%d %d",&n, &m);
    for (int i = 0; i < n; ++i){
        int k;
        scanf("%d",&k);
        for (int j = 0; j < k; ++j){
            int v;
            scanf("%d",&v);
            g[v].insert(i);
        }
    }
    for (int i = 1; i <= m; ++i){
        mp[g[i]]++;
    }
    ll ans = 1LL;
    for (it = mp.begin(); it != mp.end(); ++it){
        int v = it->se;
        ans = (ans%mod * J(v)%mod)%mod;
    }
    printf("%lld\n",ans);
    return 0;
}




C. Felicity is Coming!
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

It's that time of the year, Felicity is around the corner and you can see people celebrating all around the Himalayan region. The Himalayan region has n gyms. The i-th gym has gi Pokemon in it. There are m distinct Pokemon types in the Himalayan region numbered from 1 to m. There is a special evolution camp set up in the fest which claims to evolve any Pokemon. The type of a Pokemon could change after evolving, subject to the constraint that if two Pokemon have the same type before evolving, they will have the same type after evolving. Also, if two Pokemon have different types before evolving, they will have different types after evolving. It is also possible that a Pokemon has the same type before and after evolving.

Formally, an evolution plan is a permutation f of {1, 2, ..., m}, such that f(x) = y means that a Pokemon of type x evolves into a Pokemon of type y.

The gym leaders are intrigued by the special evolution camp and all of them plan to evolve their Pokemons. The protocol of the mountain states that in each gym, for every type of Pokemon, the number of Pokemon of that type before evolving any Pokemon should be equal the number of Pokemon of that type after evolving all the Pokemons according to the evolution plan. They now want to find out how many distinct evolution plans exist which satisfy the protocol.

Two evolution plans f1 and f2 are distinct, if they have at least one Pokemon type evolving into a different Pokemon type in the two plans, i. e. there exists an i such that f1(i) ≠ f2(i).

Your task is to find how many distinct evolution plans are possible such that if all Pokemon in all the gyms are evolved, the number of Pokemon of each type in each of the gyms remains the same. As the answer can be large, output it modulo 109 + 7.

Input

The first line contains two integers n and m (1 ≤ n ≤ 1051 ≤ m ≤ 106) — the number of gyms and the number of Pokemon types.

The next n lines contain the description of Pokemons in the gyms. The i-th of these lines begins with the integer gi (1 ≤ gi ≤ 105) — the number of Pokemon in the i-th gym. After that gi integers follow, denoting types of the Pokemons in the i-th gym. Each of these integers is between 1 and m.

The total number of Pokemons (the sum of all gi) does not exceed 5·105.

Output

Output the number of valid evolution plans modulo 109 + 7.

Examples
input
2 3
2 1 2
2 2 3
output
1
input
1 3
3 1 2 3
output
6
input
2 4
2 1 2
3 2 3 4
output
2
input
2 2
3 2 2 1
2 1 2
output
1
input
3 7
2 1 2
2 3 4
3 5 6 7
output
24
Note

In the first case, the only possible evolution plan is:

In the second case, any permutation of (1,  2,  3) is valid.

In the third case, there are two possible plans:

In the fourth case, the only possible evolution plan is:




0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:166633次
    • 积分:7453
    • 等级:
    • 排名:第3314名
    • 原创:602篇
    • 转载:2篇
    • 译文:0篇
    • 评论:35条
    文章分类
    最新评论