Codeforces 757C Felicity is Coming!【思维+vector应用】STL好神奇啊!

本文探讨了一种特殊的小精灵进化营,在确保每个道馆中各类型小精灵数量不变的前提下,寻找可能的进化方案数量。通过分析不同道馆中小精灵类型的分布情况,利用组合数学的方法求解出满足条件的有效进化方案总数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 ≤ 105, 1 ≤ 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:


题目大意:

游戏进化是一个全排列,对应我们假设有三个种类的小精灵,那么对应就可以有:

(1,2,3)(1,3,2),(2,1,3)(2,3,1)(3,1,2)(3,2,1)这六种进化方案(六种全排列);

这里(1,3,2)相当于:1进化变成1,2进化变成了3,3进化变成了2.

对应需要满足一个约束条件才能作为可行进化方案:
就是说对于一个gym来讲,之前种类i有j个小精灵,那么对应进化之后,种类i还要有j个小精灵才行,而且种类不能增加,也不能减少,也不能改变。

问一共有多少个可行方案。


思路:


1、首先考虑,对于一个gym来讲,肯定是需要两个种类的小精灵的个数都要相等才能相互进化(转化)。

而且需要对于所有gym来讲,这两种小精灵,都要个数相等才能相互进化。


2、那么我们设定一个vector,a【i】用于存入第i种小精灵,都在哪些gym出现过,同样统计数量。

再接下来对于a【i】内元素的个数从小到大排序。

那么对于两个临近的a【i】和a【i-1】,如果其中元素个数,以及元素的种类都相同,那么就是说这两种小精灵之间可以互相转化。

对于N个可以相互转化的小精灵。显然有N!种进化方案。我们接下来的任务就是去统计并且计算即可。


Ac代码(感谢巨巨的分享:http://blog.youkuaiyun.com/jeremy1149/article/details/54407467):

#include<stdio.h>
#include<string.h>
#include<vector>
#include<algorithm>
using namespace std;
#define ll __int64
#define mod 1000000007
vector<int >a[1000050];
int main()
{
    int n,m;
    while(~scanf("%d%d",&n,&m))
    {
        for(int i=1;i<=m;i++)a[i].clear();
        for(int i=1;i<=n;i++)
        {
            int num;
            scanf("%d",&num);
            while(num--)
            {
                int x;
                scanf("%d",&x);
                a[x].push_back(i);
            }
        }
        sort(a+1,a+1+m);
        ll t=1,ans=1;
        for(int i=2;i<=m;i++)
        {
            if(a[i]==a[i-1])
            {
                t++;
                ans=(ans*t)%mod;
            }
            else
            {
                t=1;
            }
        }
        printf("%I64d\n",ans);
    }
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值