关闭

Codecraft-17 and Codeforces Round #391 (Div. 1 + Div. 2, combined) C. Felicity is Coming!组合学+集合

标签: ACMCodeforces组合学+集合vector的妙用
566人阅读 评论(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:






Source

Codecraft-17 and Codeforces Round #391 (Div. 1 + Div. 2, combined)


My Solution

题意:给出n组数,每组gi个数,每个数属于1~m,每个数可以变化但变化前相同的数变化后依然相同,变化前不同的速变化后依然不同,且可能不变,但经过变化后每组的每种数的个数不会变化,求变化的总方案数。


组合学+集合

//这题是根据Codeforces的官方题解写的

把1~m分成一些集合,在同一个集合里的数在各个gi里它们的个数都是相等的,比如一个集合里有x、y,则xy在所有gi里的个数是相等的,但x在2个不同的集合里的个数可能不同,但x的个数必然等于y的个数。而方案数就是这些集合元素的个数的阶乘的积。

把这些数储存到vector< vector<int> > vec(m)里,vec[i]表示数i出现的gi的i,出现几次就是几个i,

记录完后,把vec排序,

然后扫一遍把当前vec和前一个vec比较,如果和前一个满足上面所说的集合条件则这2个vec是逻辑相等的,cnt++。如果不相等ans = mod(ans * factor[cnt]); cnt = 1;

复杂度 O(nlogn)

此外,如果求 a % b,经过实验 a - a / b * b 确实比 a % b 运算更快一些。


如图第2、3个用的是 a - a/b*b,第1、4个用的是 a % b
似乎确实inline LL mod(const LL x){return x - x / MOD * MOD;} 比 % 快一些。


#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long LL;
const int maxn = 1e6 + 8;
const LL MOD = 1e9 + 7;

LL factor[maxn];
inline LL mod(const LL x)
{
    return x - x / MOD * MOD;
}
int main()
{
    #ifdef LOCAL
    freopen("c.txt", "r", stdin);
    //freopen("c.out", "w", stdout);
    int T = 5;
    while(T--){
    #endif // LOCAL
    ios::sync_with_stdio(false); cin.tie(0);

    factor[0] = factor[1] = 1;
    for(int i = 2; i < maxn; i++){
        factor[i] = mod(factor[i - 1] * i);
    }
    int n, m, gi, x;
    cin >> n >> m;
    vector<vector<int>> vec(m);

    int i, j;
    for(i = 0; i < n; i++){
        cin >> gi;
        for(j = 0; j < gi; j++){
            cin >> x;
            vec[x-1].push_back(i);
        }
    }

    for(i = 0; i < m; i++){
        sort(vec[i].begin(), vec[i].end());
    }
    sort(vec.begin(), vec.end());//化O(n^2) 为O(nlogn)

    LL ans = 1, cnt = 1;
    for(i = 1; i < m; i++){
        if(vec[i] == vec[i - 1]){
            cnt++;
        }
        else{
            ans = mod(ans * factor[cnt]);
            cnt  = 1;
        }
    }
    ans = mod(ans * factor[cnt]);

    cout << ans << endl;



    #ifdef LOCAL
    cout << endl;
    }
    #endif // LOCAL
    return 0;
}



  Thank you!

                                                                                                                                               ------from ProLights

0
0
查看评论

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

大体题意: 有n 个体育馆每个体育馆里有 不同的精灵同样数字的精灵进化的结果是一样的不同数字进化的结果一定不同有多少个不同的进化方法使得每个体育馆进化后 和原来一样? 思路: 两个精灵可以互相进化的话,那么它们两个在所有的体育馆的数量和位置都必须一样。这样找出同类的所有数量后,求阶乘即可,...
  • aozil_yang
  • aozil_yang
  • 2017-01-13 18:32
  • 499

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

C. Felicity is Coming! time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output It's th...
  • Ginray
  • Ginray
  • 2017-01-14 01:13
  • 462

【Codeforces 757 C Felicity is Coming! 】+ 思维

C. Felicity is Coming! time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard outputIt’s that ...
  • WYK1823376647
  • WYK1823376647
  • 2017-01-15 16:29
  • 458

Codecraft-17 and Codeforces Round #391 (Div. 1 + Div. 2, combined) A. Gotta Catch Em' All!(水题)

题目链接:http://codeforces.com/contest/757/problem/AA. Gotta Catch Em’ All! time limit per test1 second memory limit per test256 megabytes inputstandar...
  • qq_32866009
  • qq_32866009
  • 2017-01-13 09:17
  • 199

Codecraft-17 and Codeforces Round #391 (Div. 1 + Div. 2, combined)A+B

A. Gotta Catch Em’ All!time limit per test:1 secondmemory limit per test:256 megabytesinput:standard inputoutput:standard outputBash wants to become a...
  • qq_33183401
  • qq_33183401
  • 2017-01-16 17:12
  • 239

ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined) E. The Holmes Children

E. The Holmes Children time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard ...
  • mystery_guest
  • mystery_guest
  • 2017-03-04 18:40
  • 282

Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) -- C. Destroying Array (并查集)

大体题意: 给你n 个数字,告诉你破坏每个数字的顺序,每次破坏后你要求一个线段,使得线段不包含破坏的点,并且线段上的值和最大? 思路: 正着删除感觉不好处理,就反过来求了,相当于从一个空序列往上面添加数值,加一次,求一次线段的最大和,在加之前,我们可以先定义一个变量Max 表示之前的操作所得到...
  • aozil_yang
  • aozil_yang
  • 2016-10-02 00:37
  • 372

Codeforces Round #254 (Div. 1)C. DZY Loves Colors(线段树经典操作/分块)

题目链接 C. DZY Loves Colors time limit per test 2 seconds memory limit per test 256 megabytes input standard input output stan...
  • fouzhe
  • fouzhe
  • 2017-02-11 15:17
  • 275

Codeforces Round #459 (Div. 2) C. The Monster

题目链接 C. The Monster time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output As Will is...
  • ffgcc
  • ffgcc
  • 2018-01-30 09:48
  • 45

【解题报告】Codeforces Round #367 (Div. 2)

简略的解题报告。
  • TRiddle
  • TRiddle
  • 2016-08-12 21:37
  • 735
    个人资料
    • 访问:335858次
    • 积分:7242
    • 等级:
    • 排名:第3677名
    • 原创:396篇
    • 转载:0篇
    • 译文:0篇
    • 评论:89条
    ProLights
    方徐靖华, 喜欢搞怪,喜欢美丽的星空,常常在房间里散步 哈哈*^_^*。 Pro是program,Lights是ID,fxjh是名字缩写。
    ☺Email☺
    如果哪个地方笔者理解错了,请告诉笔者,☺☺谢谢 prolightsfx@163.com
    博客专栏
    文章分类
    最新评论