D. Number Of Permutations 符合条件的排列种类
2 seconds
256 megabytes
standard input
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 (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)].
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.
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).
3
1 1
2 2
3 1
3
4
2 3
2 2
2 1
2 4
0
3
1 1
1 1
2 3
4
In first test case there are six permutations of size 33:
- 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);
- 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);
- 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;
- 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;
- 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);
- 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[];
bool cmp(node a,node b)
{
if(a.x==b.x)
return a.y<b.y;
else
return a.x<b.x;
}
ll fac[],visx[],visy[];
void init()//计算阶乘
{
fac[]=;//0!=1
for(int i=;i<=;i++)
fac[i]=fac[i-]*i%mod;
}
int main()
{
int n;
init();
scanf("%d",&n);
for(int i=;i<=n;i++)
{
scanf("%d%d",&p[i].x,&p[i].y);
visx[p[i].x]++;
visy[p[i].y]++;
}
ll k1=,k2=,k3=,ans=;
for(int i=;i<=;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+,p+n+,cmp);
int flag=;
for(int i=;i<=n;i++)//如果x,y不能同时递增,就不会有重复计算的部分
{
if(p[i].y<p[i-].y)
flag=;
}
if(flag)//减去重复计算的部分
{
int i,j;
for(i=;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 ;
}
D. Number Of Permutations 符合条件的排列种类的更多相关文章
- mysql 单表,多表,符合条件,子查询
单表: HAVING过滤 二次筛选 只能是group by 之后的字段 1.查询各岗位内包含的员工个数小于2的岗位名.岗位内包含员工名字.个数 select post,group_concat(nam ...
- Number Of Permutations
Number Of Permutations 思路:利用容斥,首先所有可能的排列肯定是fac[n],然后可能会有三种 bad 的情况: ①第一个元素的排列是非递减 ②第二种是第二个元素的排列是非递减 ...
- Shell 筛选符合条件的 ELF 文件
0 运行环境 本机系统:Windows 10 虚拟机软件:Oracle VM VirtualBox 6 虚拟机系统:Ubuntu 18 1 引言 - 编译过程 我们知道在 CPU 上执行的是低级别的机 ...
- JavaScript利用replace更改所有符合条件字符
利用replace替换字符串时,在正常使用情况下默认只能更改匹配到的第一个字符 var a=new String("fffffddd"); console.log(a.replac ...
- 遍历List集合,删除符合条件的元素
List集合的遍历有三种方式:增强for循环,普通for循环,Iterator迭代器遍历 如果只是对集合进行遍历,以上三种循环都可正常遍历: (1)增强For循环遍历List集合 List<St ...
- PHP函数preg_replace() 正则替换所有符合条件的字符串
PHP preg_replace() 正则替换,与JavaScript 正则替换不同,PHP preg_replace() 默认就是替换所有符号匹配条件的元素. preg_replace (正则表达式 ...
- mongodb查询返回内嵌符合条件的文档
db.T_Forum_Thread.find({ "ThreadReply.ReplyContent" : /范甘迪/ }, { "ThreadReply.$" ...
- PHP preg_replace() 正则替换所有符合条件的字符串示例
PHP preg_replace() 正则替换,与Javascript 正则替换不同,PHP preg_replace() 默认就是替换所有符号匹配条件的元素. 需要用程序处理的数据并不总是预先以数 ...
- PHP删除符合条件的整个目录
<?php /** * @name delFile函数与delDir函数一起使用, 删除符合条件的整个目录 * @param string $path 指定操作路径 * @return null ...
随机推荐
- 「JSOI2015」送礼物
「JSOI2015」送礼物 传送门 看到这题首先想到分数规划. 我们发现对于当前区间,如果它的最大值和最小值不是分居区间的两个端点的话,那么我们显然可以把两端多出去的部分舍掉,因为,在区间最大值最小值 ...
- 转载--php 7.2 安装 mcrypt 扩展
在 php 官网下载 mcrypt 包,php 扩展官网 # wget http://pecl.php.net/get/mcrypt-1.0.1.tgz # tar xf mcrypt-1.0.1.t ...
- .NET Core快速入门教程 3、使用VS Code开发.NET Core控制台应用程序
一.前言 本篇开发环境 1.操作系统: Windows 10 X642.SDK: .NET Core 2.0 Preview3.VS Code:1.14 二.安装&配置 1.VS Code下载 ...
- java模板字符串功能的简单实现
package com.Interface.util; import lombok.extern.slf4j.Slf4j; /** * 测试类 * * @author 华文 * @date 2019年 ...
- proto school tutorial: blog: lesson 1
https://proto.school/#/blog/01 我们现在希望:把一个post 和他的作者联系起来. 从之前的教程here中,你可以知道, 一个cid 就是代表一个link 比如: { l ...
- Android读取权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <use ...
- 从蜘蛛侠到仙剑,为何知名IP都要开发VR游戏?
去年3月底斯皮尔伯格指导的<头号玩家>上映,为全球玩家和影迷塑造出一个庞大.刺激而又真实无比的虚拟游戏世界--"绿洲".电影上映后,在人们感叹斯皮尔伯格旺盛的艺术想象力 ...
- [Unity] Shader Graph Error 当前渲染管道与此主节点不兼容(The current render pipeline is not compatible with this master node)
Shader Graph Error : The current render pipeline is not compatible with this master node 问题产生环境: Un ...
- Python 基础之面向对象之类中的方法和与类相关的魔术属性以及反射
一.类中的方法 普通方法: 没有参数,只能类调用绑定方法: (1)绑定到对象(自动传递对象参数) (2)绑定到类(自动传递参数)静态方法: 无论是类还是对象,都能调用的方法#例:class Dog() ...
- 字符流---Day32
时隔多久,我又回来写博客了,最近忙于两个课设,五周,搞得头发都不知道掉了多少根了,还没成为程序员就开始掉了,等我成为一名程序员的时候岂不是要秃头了,IT界的人会不会帮我当成大佬了,哈哈哈哈,希望我以后 ...