CF Number Of Permutations inclusion and exclusion

Topic links: https://www.luogu.org/problem/CF1207D

Meaning of the questions: to give you a sequence of two-dimensional, two-dimensional if in any dimension to meet nondecreasing from 1-n, is a bad sequence, otherwise it is a good sequence, inquire of the sort which is good sequence of how many

Analysis: Monotone can first row of the first and second dimensions to obtain the total number of, respectively, c1, c2, and then simultaneously satisfy the first row and second dimensions nondecreasing, referred to as c3

Therefore, the answer is: n (Total) - (c1 + c2-c3) (bad sequence number)!

This design further reduced after the modulo multiplication can also be directly processed

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=3e5+7;
const int inf=0x3f3f3f3f;
const int N=1e7;
const ll mod=998244353;
#define meminf(a) memset(a,0x3f,sizeof(a))
#define mem0(a) memset(a,0,sizeof(a))
struct node{
    int x,y;
}a[maxn];
bool cmp1(node a,node &b) {
    return a.x<b.x;
}
bool cmp2(const node &a,const node &b) {
    return a.y<b.y;
}
bool cmp3(const node &a,const node &b){
    return a.x==b.x?a.y<b.y:a.x<b.x;
} 
int main(){
    int n;scanf("%d",&n);
    for(int i=1;i<=n;i++)scanf("%d%d",&a[i].x,&a[i].y);
    sort(a+1, A +. 1 + n-, CMP1); 
    LL FX = . 1 , K;
     // FX record number of the first dimension nondecreasing 
    for ( int I = . 1 ; I <= n-; I ++ ) {
         IF (A [I] .x = = A [I- . 1 ] .x) K ++ ;
         the else K = . 1 ; 
        FX = (FX * K)% MOD; 
    } 
    Sort (A + . 1 , A + . 1 + n-, CMP2); 
    LL FY = . 1 ;
     for ( int = I . 1 ; I <= n-; I ++ ) {
         IF(a[i].y==a[i-1].y)k++;
        else k=1;
        fy=(fy*k)%mod; 
    }
    sort(a+1,a+1+n,cmp3);
    ll f=1;
    for(int i=1;i<=n;i++){
        if(a[i].y<a[i-1].y){
            f=0;
            break;
        }
    }
    if(f){
        for(int i=1;i<=n;i++){
            if(a[i].y==a[i-1].y&&a[i].x==a[i-1].x) k++;
            else k=1;
            f=(f*k)%mod;
        }
    }
    ll res=(fx+fy-f+mod)%mod;
    ll res2=1;
    for(int i=1;i<=n;i++) res2=(res2*i)%mod;
    printf("%lld\n",(res2-res+mod)%mod);
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/qingjiuling/p/11423923.html