您的位置:首页 > Web前端

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

2017-01-14 01:13 477 查看
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 hasn gyms. The
i-th gym hasgi Pokemon in it. There arem distinct Pokemon types in the Himalayan region numbered from1to 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 permutationf of
{1, 2, ..., m}, such thatf(x) = y means that a Pokemon of typex 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 distinctevolution plans exist which satisfy the protocol.

Two evolution plans f1 andf2 are distinct, if they have at least one Pokemon type evolving into a different Pokemon
type in the two plans, i. e. there exists ani 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 andm (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. Thei-th of these lines begins with the integergi
(1 ≤ gi ≤ 105) — the number of Pokemon in thei-th gym. After that
gi integers follow, denoting types of the Pokemons in thei-th gym. Each of these integers is between1 and
m.

The total number of Pokemons (the sum of all gi) does not exceed5·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 23 2 2 12 1 2


Output
1


Input
3 7
2 1 22 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:



题意:

   每个道场有一定数量的小精灵,每个宠物小精灵都有自己的类型。相同类型的小精灵会进化成相同类型的“大精灵”,不同类型的小精灵进化成的“大精灵”的类型都不相同。

           你的任务是找到不同的进化计划的个数,使得所有宠物小精灵在它们的道馆都进化过,且每个道馆的每种类型的宠物小精灵的数量保持不变。

思路:

   保存下每个类型的小精灵所在道场,判断两个小精灵的道场种类和数量是否相等(vector直接用“==”就行)。

         
设相等的小精灵数量为n ,则为答案提供的值为n的阶乘。

代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;

const int maxn = 1000005;
const int mod  = 1000000007;
int n, m;

vector<int> a[maxn];

int main(){
scanf("%d%d",&n,&m);

for(int i=1;i<=n;i++){
int num;
scanf("%d",&num);
while(num--){
int flag;
scanf("%d",&flag);
a[flag].push_back(i);
}
}

sort(a+1,a+m+1);

long long ans = 1;
int fnum=1;
for(int i=2;i<=m;i++)
if(a[i] == a[i-1]){
fnum++;
ans = (ans*fnum)%mod;
}
else
fnum = 1;

printf("%lld\n",ans);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐