【Codeforces 757 C Felicity is Coming! 】+ 思维
2017-01-15 16:29
281 查看
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:
![](http://img.blog.csdn.net/20170115161808772?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvV1lLMTgyMzM3NjY0Nw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
In the second case, any permutation of (1, 2, 3) is valid.
![](http://img.blog.csdn.net/20170115161901257?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvV1lLMTgyMzM3NjY0Nw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
In the third case, there are two possible plans:
In the fourth case, the only possible evolution plan is:
![](http://img.blog.csdn.net/20170115162015570?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvV1lLMTgyMzM3NjY0Nw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
统计每个 x 在 gym 中出现的次数,然后统计每个出现相同次数的集合个数num(贡献 = num !),最后求乘积即可~~
AC代码:
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:
统计每个 x 在 gym 中出现的次数,然后统计每个出现相同次数的集合个数num(贡献 = num !),最后求乘积即可~~
AC代码:
#include<bits/stdc++.h> using namespace std; typedef long long LL; const int MAXN = 1e6 + 10; const LL mod = 1e9 + 7; vector <int> v[MAXN]; int main() { int N,M,a,b; scanf("%d %d",&N,&M); for(int i = 1 ; i <= N; i++){ scanf("%d",&a); while(a--){ scanf("%d",&b); v[b].push_back(i); } } sort(v + 1, v + 1 + M); LL cut = 1,ans = 1; for(int i = 2 ; i <= M; i++) if(v[i] == v[i - 1]){ cut++; ans = (ans * cut) % mod; } else cut = 1; printf("%lld\n",ans); return 0; }
相关文章推荐
- Codeforces 757 C Felicity is Coming! 【STL+思维】
- Codeforces 757 C Felicity is Coming! 思维+STL
- codeforces 757 C. Felicity is Coming!
- codeforces-757-【B、C思维】
- CodeForces - 757C - Felicity is Coming!(思维)
- Codeforces 296C Greg and Array【思维】
- Codeforces 591E Three States【优先队列Bfs+思维】
- CodeForces 628B New Skateboard 思维
- Codeforces 500B:New Year Permutation(并查集||Floyd+思维)
- CodeForces - 729D Sea Battle(思维题)
- Codeforces 599C Day at the Beach【思维+优先队列】
- codeforces 777C Alyona and Spreadsheet(DP+思维)
- CodeForces - 711C Coloring Trees(DP)(思维)
- Codeforces 689E Mike and Geometry Problem 思维
- Codeforces 789A Anastasia and pebbles(数学,思维题)
- codeforces- 949B - A Leapfrog in the Array(思维)
- Codeforces 711D Directed Roads【Dfs+思维】
- Codeforces400E - Inna and Binary Logic - 思维、数学
- CodeForces 672D Robin Hood(二分 + 思维)
- codeforces-305A Strange Addition(思维+模拟)