Codeforces 1207 D. Number Of Permutations(组合数学+容斥)

话说csdn什么时候把文章标签强制换成专栏了?

题目链接:https://codeforces.com/contest/1207/problem/D

题意:有n个pair (a1,b1),(a2,b2),......,(an,bn),只要这个序列的第一个元素或者第二个元素是非降有序的,那么这个序列就是bad的,你可以打乱顺序,问这个序列是good的个数,输出这个个数。

题解:艹,总的是n!,假设是num,满足第一个关键字是非严格递增有序的序列的个数为所有第一个关键字出现的次数的阶乘的乘积,假设数量是num1,满足第二个关键字是非严格递增有序的序列的个数同样也可以按上所述计算,假设数量是num2,那么先对第一个关键字排序,再对第二个关键字排序之后,第一个关键字和第二个关键字都是非严格递增有序的序列个数为所有pair

出现的次数的阶乘的乘积,假设数量为num3,

那么根据容斥原理可得,答案为num-num1-num2+num3。

最近脑子转不动,说话不利索,是不是提前步入老年生活?,艹。

代码:

# define _CRT_SECURE_NO_WARNINGS
#pragma GCC optimize(2)
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <map>
#include <unordered_map>
#include <vector>
#include <queue>
//#define int long long
#define fir first
#define sec second
using namespace std;
/*---------------------------------------------------------------------------------------------------------------------------*/
const int N = 3e5 + 5;
const double pi = acos(-1.0);
typedef long long ll;
const int mod = 998244353;
//const int mod = 1e9 + 7;
#define inf 0x3f3f3f3f

struct something {
  int num, period, time;
  bool operator < (const something& y)const {
    return y.time < time || (y.time == time && y.num < num);
  }
  something() {}
  something(int num, int period, int time) :num(num), period(period), time(time) {}
};

int dx[8] = { 0, 0, 1,-1, 1, 1,-1,-1 };
int dy[8] = { 1,-1, 0, 0, 1,-1, 1,-1 };

int f[N];
map<int, int>mp1, mp2;
map<pair<int, int>, int>mp3;
vector<pair<int, int> >p;
signed main() {
  /*lz艹*/
  int n;
  scanf("%d", &n);
  f[0] = 1;
  for (int i = 1; i <= n; i++)f[i] = 1LL * f[i - 1] * i % mod;;
  for (int i = 0; i < n; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    p.push_back(make_pair(x, y));
    mp1[p[i].fir]++;
    mp2[p[i].sec]++;
    mp3[p[i]]++;
  }

  int num = f[n];
  int base = 1;
  for (auto it : mp1) base = 1LL * base * f[it.second] % mod;
  num = (num - base + mod) % mod;
  base = 1;
  for (auto it : mp2) base = 1LL * base * f[it.second] % mod;
  num = (num - base + mod) % mod;
  base = 1;
  for (auto it : mp3)base = 1LL * base * f[it.second] % mod;
  
  sort(p.begin(), p.end());
  for (int i = 0; i < n-1; i++){
    if (p[i + 1].sec < p[i].sec)base = 0;
  }
  printf("%d\n", (num + base) % mod);

  return 0;
}

题目描述:

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)] is bad because the sequence of first elements is sorted: [1,3,3][1,3,3];
  • 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)] 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)] 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 (1≤pi≤n1≤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 (1≤n≤3⋅1051≤n≤3⋅105).

The next nn lines contains description of sequence ss. The ii-th line contains two integers aiai and bibi (1≤ai,bi≤n1≤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).

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值