Time limit : 2sec / Memory limit : 256MB
Score : 1000 points
Snuke arranged N colorful balls in a row. The i-th ball from the left has color ci and weight wi.
He can rearrange the balls by performing the following two operations any number of times, in any order:
How many different sequences of colors of balls can be obtained? Find the count modulo 109+7.
Input is given from Standard Input in the following format:
N X Y c1 w1 : cN wN
Print the answer.
4 7 3 3 2 4 3 2 1 4 4
2
1 1 1 1 1
1
21 77 68
16 73
16 99
19 66
2 87
2 16
7 17
10 36
10 68
2 38
10 74
13 55
21 21 v
3 7
12 41
13 88
18 6
2 12
13 87
1 9
2 27
13 15
129729600
思路:对于不同颜色的三个球a,b,c,如果a和b能换,b和c能换,那么a和c一定能换,因为abc->acb->bca->cba,于是找到最小权值的球,建边,就变成有重复元素的组合数问题了,用乘法逆元可以解决。
# include
using namespace std;
typedef long long LL;
const LL mod = 1e9+7;
const int maxn = 2e5+3;
LL fac[maxn], inv[maxn];
int cnt, n, x, y, co[maxn], va[maxn], vis[maxn]={0}, ge[maxn]={0};
vectorv[maxn], tmp;
vector >r[maxn];
vector >p;
void dfs(int u)
{
vis[u] = 1;
++cnt, ++ge[co[u]], tmp.push_back(co[u]);
for(int i=0; iy) continue;
v[id].push_back(i), v[i].push_back(id);
}
}
if(p.size()>1)//该颜色的其他球也要考虑进去。
{
int id = p[1].second, imin = co[p[0].second];
for(auto it = r[imin].begin(); it!=r[imin].end(); ++it)
if(it->first + va[id]<=y)
v[it->second].push_back(id), v[id].push_back(it->second);
}
dfs(p[0].second);
LL ans = fac[cnt];
for(int i=0; i