D. Number Of Permutations 符合条件的排列种类

time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given a sequence of nn pairs of integers: (a1,b1),(a2,b2),,(an,bn)(a1,b1),(a2,b2),…,(an,bn). This sequence is called bad if it is sorted in non-descending order by first elements or if it is sorted in non-descending order by second elements. Otherwise the sequence is good. There are examples of good and bad sequences:

  • s=[(1,2),(3,2),(3,1)]s=[(1,2),(3,2),(3,1)] is bad because the sequence of first elements is sorted: [1,3,3][1,3,3];
  • s=[(1,2),(3,2),(1,2)]s=[(1,2),(3,2),(1,2)] is bad because the sequence of second elements is sorted: [2,2,2][2,2,2];
  • s=[(1,1),(2,2),(3,3)]s=[(1,1),(2,2),(3,3)] is bad because both sequences (the sequence of first elements and the sequence of second elements) are sorted;
  • s=[(1,3),(3,3),(2,2)]s=[(1,3),(3,3),(2,2)] is good because neither the sequence of first elements ([1,3,2])([1,3,2]) nor the sequence of second elements ([3,3,2])([3,3,2]) is sorted.

Calculate the number of permutations of size nn such that after applying this permutation to the sequence ss it turns into a good sequence.

A permutation pp of size nn is a sequence p1,p2,,pnp1,p2,…,pn consisting of nn distinct integers from 11 to nn (1pin1≤pi≤n). If you apply permutation p1,p2,,pnp1,p2,…,pn to the sequence s1,s2,,sns1,s2,…,sn you get the sequence sp1,sp2,,spnsp1,sp2,…,spn. For example, if s=[(1,2),(1,3),(2,3)]s=[(1,2),(1,3),(2,3)] and p=[2,3,1]p=[2,3,1] then ss turns into [(1,3),(2,3),(1,2)][(1,3),(2,3),(1,2)].

Input

The first line contains one integer nn (1n31051≤n≤3⋅105).

The next nn lines contains description of sequence ss. The ii-th line contains two integers aiai and bibi (1ai,bin1≤ai,bi≤n) — the first and second elements of ii-th pair in the sequence.

The sequence ss may contain equal elements.

Output

Print the number of permutations of size nn such that after applying this permutation to the sequence ss it turns into a good sequence. Print the answer modulo 998244353998244353 (a prime number).

Examples
input
Copy
3
1 1
2 2
3 1
output
Copy
3
input
Copy
4
2 3
2 2
2 1
2 4
output
Copy
0
input
Copy
3
1 1
1 1
2 3
output
Copy
4
Note

In first test case there are six permutations of size 33:

  1. if p=[1,2,3]p=[1,2,3], then s=[(1,1),(2,2),(3,1)]s=[(1,1),(2,2),(3,1)] — bad sequence (sorted by first elements);
  2. if p=[1,3,2]p=[1,3,2], then s=[(1,1),(3,1),(2,2)]s=[(1,1),(3,1),(2,2)] — bad sequence (sorted by second elements);
  3. if p=[2,1,3]p=[2,1,3], then s=[(2,2),(1,1),(3,1)]s=[(2,2),(1,1),(3,1)] — good sequence;
  4. if p=[2,3,1]p=[2,3,1], then s=[(2,2),(3,1),(1,1)]s=[(2,2),(3,1),(1,1)] — good sequence;
  5. if p=[3,1,2]p=[3,1,2], then s=[(3,1),(1,1),(2,2)]s=[(3,1),(1,1),(2,2)] — bad sequence (sorted by second elements);
  6. if p=[3,2,1]p=[3,2,1], then s=[(3,1),(2,2),(1,1)]s=[(3,1),(2,2),(1,1)] — good sequence.

题意:题目要求使二元组序列中x和y都不递增的排列的数量,

例如二元组 [1,1] [2,2] [3,1] 有三种 [2,2] [1,1] [3,1] 

  [2,2] [3,1] [1,1]   [3,1] [2,2] [1,1] 符合条件。

 

题解:考虑到求不递增的排列比较难,我们可以反过来考虑求递增的排列,

然后用全排列的数量即阶乘减去递增序列数量即为答案。

求递增的排列时要考虑单个x和y排列中重复元素数量,

而x与y同时递增时有重复的二元组我们应该减掉。

例如 [1,2] [3,4] [3,4] [5,6],排列x里重复的数量为2,排列y里重复的数量也为2,

但是二元组重复了排列数量2,所以答案为n阶乘减去2而不是减4。

 

因为x,y的捆绑关系,交换x的时候y也跟着变换;

所以当p[i].x==p[i+1].x&&p[i].y==p[i+1].y这种情况出现的时候存在重复计算

 

 

#include<iostream>
#include<string.h>
#include<string>
#include<algorithm>
#include<math.h>
#include<string>
#include<string.h>
#include<vector>
#include<utility>
#include<map>
#include<queue>
#include<set>
#include<stack>
#define mx 0x3f3f3f3f3f3f3f
#define ll long long
#define MAXN 100
#define mod 998244353
using namespace std;
struct node
{
    int x;
    int y;
}p[300005];
bool cmp(node a,node b)
{
    if(a.x==b.x)
        return a.y<b.y;
    else
        return a.x<b.x;
}
ll fac[300005],visx[300005],visy[300005];
void init()//计算阶乘
{
    fac[0]=1;//0!=1
    for(int i=1;i<=300005;i++)
        fac[i]=fac[i-1]*i%mod;
}
int main()
{
    int n;
    init();
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        scanf("%d%d",&p[i].x,&p[i].y);
        visx[p[i].x]++;
        visy[p[i].y]++;
    }
    ll k1=1,k2=1,k3=1,ans=0;
    for(int i=1;i<=300005;i++)//对所有重复出现的数求阶乘在累加求和
    {
        if(visx[i])
            k1=(k1*fac[visx[i]]+mod)%mod;
        if(visy[i])
            k2=(k2*fac[visy[i]]+mod)%mod;
    }
    ans=(ans+k1+k2+mod)%mod;//避免最后作差出现负数
    sort(p+1,p+n+1,cmp);
    int flag=1;
    for(int i=2;i<=n;i++)//如果x,y不能同时递增,就不会有重复计算的部分
    {
        if(p[i].y<p[i-1].y)
            flag=0;
    }
    if(flag)//减去重复计算的部分
    {
        int i,j;
        for(i=1;i<=n;i=j)
        {
            for(j=i;j<=n&&p[i].x==p[j].x&&p[i].y==p[j].y;++j)
                ;
            k3=(k3*fac[j-i]+mod)%mod;
        }
        printf("%lld\n",(fac[n]-ans+k3+mod)%mod );
        //cout<<k1<<' '<<k2<<' '<<k3<<endl;
    }
    else
        printf("%lld\n",(fac[n]-ans+mod)%mod);
    return 0;
}

 

posted @ 2019-09-06 17:48  知道了呀~  阅读(459)  评论(0编辑  收藏  举报