codeforces1207D - Number Of Permutations

D. Number Of Permutations
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
You are given a sequence of ? pairs of integers: (?1,?1),(?2,?2),…,(??,??). 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:

?=[(1,2),(3,2),(3,1)] is bad because the sequence of first elements is sorted: [1,3,3];
?=[(1,2),(3,2),(1,2)] is bad because the sequence of second elements is sorted: [2,2,2];
?=[(1,1),(2,2),(3,3)] is bad because both sequences (the sequence of first elements and the sequence of second elements) are sorted;
?=[(1,3),(3,3),(2,2)] is good because neither the sequence of first elements ([1,3,2]) nor the sequence of second elements ([3,3,2]) is sorted.
Calculate the number of permutations of size ? such that after applying this permutation to the sequence ? it turns into a good sequence.

A permutation ? of size ? is a sequence ?1,?2,…,?? consisting of ? distinct integers from 1 to ? (1≤??≤?). If you apply permutation ?1,?2,…,?? to the sequence ?1,?2,…,?? you get the sequence ??1,??2,…,???. For example, if ?=[(1,2),(1,3),(2,3)] and ?=[2,3,1] then ? turns into [(1,3),(2,3),(1,2)].

Input
The first line contains one integer ? (1≤?≤3⋅105).

The next ? lines contains description of sequence ?. The ?-th line contains two integers ?? and ?? (1≤??,??≤?) — the first and second elements of ?-th pair in the sequence.

The sequence ? may contain equal elements.

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

Examples
inputCopy
3
1 1
2 2
3 1
outputCopy
3
inputCopy
4
2 3
2 2
2 1
2 4
outputCopy
0
inputCopy
3
1 1
1 1
2 3
outputCopy
4
Note
In first test case there are six permutations of size 3:

if ?=[1,2,3], then ?=[(1,1),(2,2),(3,1)] — bad sequence (sorted by first elements);
if ?=[1,3,2], then ?=[(1,1),(3,1),(2,2)] — bad sequence (sorted by second elements);
if ?=[2,1,3], then ?=[(2,2),(1,1),(3,1)] — good sequence;
if ?=[2,3,1], then ?=[(2,2),(3,1),(1,1)] — good sequence;
if ?=[3,1,2], then ?=[(3,1),(1,1),(2,2)] — bad sequence (sorted by second elements);
if ?=[3,2,1], then ?=[(3,1),(2,2),(1,1)] — good sequence.

题意: 一些二元组(x,y)。求多少种排列,使得x不递增(包括相等),y不递增(包括相等)。

思路: 比赛的时候一秒就想出了思路,数学能力不行,实现的时候一直出问题。。。

明显的组合问题!!

  • 要考虑多少个不递增的排列是很困难的,正难则反,我们考虑多少个递增的排列,再用全排列减去这个就可以了。
    1.先考虑问题的简化版本:如果是一元组x,并且没有相同的情况,怎么求?此时x的递增情况只有一种,其他情况全是非递增的,只需要n的阶乘-1就可以了。
    2.再考虑:如果一元组x,有重复的数字,怎么办?此时x的递增情况,与其中的重复数组有关,比如1 2 2。第二个2和第三个2可以调换位置。此时的递增数目为所有重复数目阶乘和积。再用fac[n]减去重复部分就可以了。
    3.继续考虑:如果变成了二元组(x,y),没有重复数字:只考虑x的递增情况,有一种,只考虑y的递增情况,有一种。但是!这是二元组,你算x的时候,忽略了y,算y的时候,忽略了x。其中会有重复的部分。假设(x,y)x,y同时递增。那么x递增的情况和y递增的情况是等价的,要减去1.
    4.最后是题目的情况:二元组(x,y),重复数字。只考虑x,y,就是第二种情况的和:重复数目阶乘的积。在考虑第三种情况,重复部分。假设x,y能同时递增,并且出现了相同的二元组,那么这个相同的二元组被计算了两遍,就必须减去相同二元组的情况。

比如(1,2) (2,3) (2,3)(4,5)。
只考虑x是1,2,2,4。有两个2答案是2。
再考虑y是2,3,3,5。有两个3答案是3.
但是你算x的2和你算y的3其实是一种情况——交换2的时候也在交换3,所以要减去二元组的重复部分。

#include <cstdio>
#include <algorithm>
#include <vector>
#include <iostream>
#include <cstring>
#include <map>
using namespace std;
typedef long long ll;
const ll MOD = 998244353;
const ll maxn = 4e5 + 7;
ll fac[maxn];
map<ll,ll>vis,vis2;

struct Node
{
    ll x,y;
}nodes[maxn];
pair<ll,ll>N[maxn];
map<pair<ll,ll>,ll>mp;
ll cmp1(Node a,Node b)
{
    return a.x < b.x;
}

ll cmp2(Node a,Node b)
{
    if(a.x == b.x)
        return a.y < b.y;
    return a.x < b.x;
}

void init()
{
    fac[0] = 1;
    for(ll i = 1;i <= 400000;i++)
    {
        fac[i] = (fac[i - 1] * i) % MOD;
    }
}

int main()
{
    init();
    
    ll tmp1 = 0,tmp2 = 0,tmp3 = 0;
    ll n;scanf("%lld",&n);
    for(ll i = 1;i <= n;i++)
    {
        scanf("%lld%lld",&nodes[i].x,&nodes[i].y);
        vis[nodes[i].x]++;
        vis2[nodes[i].y]++;
        if(vis[nodes[i].x] == n)
        {
            printf("0\n");
            return 0;
        }
        else if(vis2[nodes[i].y] == n)
        {
            printf("0\n");
            return 0;
        }
    }
    
    ll ans = 0;
    ll tmp = 1;
    for(int i = 1;i < maxn;i++)//感谢 F-幕:大佬的提醒,一开始写了 i<=maxn,其实越界了
    {
        if(vis[i])
            tmp = (tmp * fac[vis[i]] + MOD) % MOD;
    }
    ans = (ans + tmp) % MOD;
    tmp1 = tmp;
    tmp = 1;
    for(int i = 1;i < maxn;i++)
    {
        if(vis2[i])
        {
            tmp = (tmp * fac[vis2[i]] + MOD) % MOD;
        }
    }
    tmp2 = tmp;
    ans = (ans + tmp) % MOD;
    sort(nodes + 1,nodes + 1 + n,cmp2);
    int ok = 1;
    for(int i = 2;i <= n;i++)
    {
        if(nodes[i].x < nodes[i - 1].x || nodes[i].y < nodes[i - 1].y)
        {
            ok = 0;
            break;
        }
    }
    
    for(int i = 1;i <= n;i++)
    {
        N[i].first = nodes[i].x;
        N[i].second = nodes[i].y;
    }
    
    if(ok == 1)
    {
        tmp = 1;
        for(int i = 1;i <= n;i++)
        {
            mp[N[i]]++;
        }
        
        map<pair<ll,ll>,ll>::iterator it;
        for(it = mp.begin();it != mp.end();it ++)
        {
            tmp = tmp * fac[it -> second] % MOD;
        }
        ans = ((ans + MOD) - tmp) % MOD;
        tmp3 = tmp;
    }
    
    ans = (fac[n] + MOD - ans) % MOD;
    
    
    printf("%lld\n",ans);
    return 0;
}

评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值