AtCoder:Colorful Balls(思维 & 数论)

D - Colorful Balls


Time limit : 2sec / Memory limit : 256MB

Score : 1000 points

Problem Statement

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:

  • Operation 1: Select two balls with the same color. If the total weight of these balls is at most X, swap the positions of these balls.
  • Operation 2: Select two balls with different colors. If the total weight of these balls is at most Y, swap the positions of these balls.

How many different sequences of colors of balls can be obtained? Find the count modulo 109+7.

Constraints

  • 1N2×105
  • 1X,Y109
  • 1ciN
  • 1wi109
  • X,Y,ci,wi are all integers.

Input

Input is given from Standard Input in the following format:

N X Y
c1 w1
:
cN wN

Output

Print the answer.


Sample Input 1

Copy
4 7 3
3 2
4 3
2 1
4 4

Sample Output 1

Copy
2
  • The sequence of colors (2,4,3,4) can be obtained by swapping the positions of the first and third balls by operation 2.
  • It is also possible to swap the positions of the second and fourth balls by operation 1, but it does not affect the sequence of colors.

Sample Input 2

Copy
1 1 1
1 1

Sample Output 2

Copy
1

Sample Input 3

Copy
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

Sample Output 3

Copy
129729600
题意:给N个球的颜色ci和权值wi,对于相同颜色的球,权值和<=x,或者不同颜色的球权值和<=y都可以交换,不限交换次数,问组成的颜色序列数有多少种。

思路:对于不同颜色的三个球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



你可能感兴趣的:(数论)