双元素非递增(容斥)--Number Of Permutations Educational Codeforces Round 71 (Rated for Div. 2)

题意:https://codeforc.es/contest/1207/problem/D

n个元素,每个元素有a、b两个属性,问你n个元素的a序列和b序列有多少种排序方法使他们不同时非递减(不同时good)。

思路:

真难则反+容斥,反向考虑,ans1=如果a序列非递减则有a中各个数字出现次数的阶乘的乘积个,ans2=b序列也是一样。

ans3=然后还要减去a序列和b序列都是good的方案数,就是元素相同的出现次数阶乘的乘积(注意,如果不存在双good就不算ans3)。

ANS就是:全排列 - ans1 - ans2 + ans3

  1 #define IOS ios_base::sync_with_stdio(0); cin.tie(0);
  2 #include <cstdio>//sprintf islower isupper
  3 #include <cstdlib>//malloc  exit strcat itoa system("cls")
  4 #include <iostream>//pair
  5 #include <fstream>//freopen("C:\\Users\\13606\\Desktop\\草稿.txt","r",stdin);
  6 #include <bitset>
  7 #include <map>
  8 //#include<unordered_map>
  9 #include <vector>
 10 #include <stack>
 11 #include <set>
 12 #include <string.h>//strstr substr
 13 #include <string>
 14 #include <time.h>//srand(((unsigned)time(NULL))); Seed n=rand()%10 - 0~9;
 15 #include <cmath>
 16 #include <deque>
 17 #include <queue>//priority_queue<int, vector<int>, greater<int> > q;//less
 18 #include <vector>//emplace_back
 19 //#include <math.h>
 20 //#include <windows.h>//reverse(a,a+len);// ~ ! ~ ! floor
 21 #include <algorithm>//sort + unique : sz=unique(b+1,b+n+1)-(b+1);+nth_element(first, nth, last, compare)
 22 using namespace std;//next_permutation(a+1,a+1+n);//prev_permutation
 23 #define fo(a,b,c) for(register long long a=b;a<=c;++a)
 24 #define fr(a,b,c) for(register int a=b;a>=c;--a)
 25 #define mem(a,b) memset(a,b,sizeof(a))
 26 #define pr printf
 27 #define sc scanf
 28 #define ls rt<<1
 29 #define rs rt<<1|1
 30 typedef long long ll;
 31 void swapp(int &a,int &b);
 32 double fabss(double a);
 33 int maxx(int a,int b);
 34 int minn(int a,int b);
 35 int Del_bit_1(int n);
 36 int lowbit(int n);
 37 int abss(int a);
 38 //const long long INF=(1LL<<60);
 39 const double E=2.718281828;
 40 const double PI=acos(-1.0);
 41 const int inf=(1<<30);
 42 const double ESP=1e-9;
 43 const int mod=(int)998244353;
 44 const int N=(int)1e6+10;
 45
 46 ll a[N],b[N];
 47 pair<ll,ll>s[N];
 48 map<pair<ll,ll>,ll>mp3;
 49 map<ll,ll> mp1,mp2;
 50
 51 ll v(ll x)
 52 {
 53     ll sum=1;
 54     fo(i,1,x)
 55         sum*=i,sum%=mod;
 56     return sum;
 57 }
 58
 59 int main()
 60 {
 61     int n;
 62     ll ans=1,temp1,temp2,temp3;
 63     sc("%d",&n);
 64     fo(i,1,n)ans*=i,ans%=mod,sc("%lld%lld",&a[i],&b[i]),mp1[a[i]]++,mp2[b[i]]++,s[i]={a[i],b[i]},mp3[s[i]]++;
 65     temp1=temp2=temp3=1;
 66     for(auto i:mp1)
 67         temp1*=v(i.second),temp1%=mod;
 68     for(auto i:mp2)
 69         temp2*=v(i.second),temp2%=mod;
 70     for(auto i:mp3)
 71         temp3*=v(i.second),temp3%=mod;
 72     sort(s+1,s+1+n);
 73     for(int i=1;i<n;++i)
 74         if(s[i].second>s[i+1].second)
 75             temp3=0;
 76     pr("%lld\n",((ans-temp1-temp2+temp3)%mod+mod)%mod);
 77     return 0;
 78 }
 79
 80 /**************************************************************************************/
 81
 82 int maxx(int a,int b)
 83 {
 84     return a>b?a:b;
 85 }
 86
 87 void swapp(int &a,int &b)
 88 {
 89     a^=b^=a^=b;
 90 }
 91
 92 int lowbit(int n)
 93 {
 94     return n&(-n);
 95 }
 96
 97 int Del_bit_1(int n)
 98 {
 99     return n&(n-1);
100 }
101
102 int abss(int a)
103 {
104     return a>0?a:-a;
105 }
106
107 double fabss(double a)
108 {
109     return a>0?a:-a;
110 }
111
112 int minn(int a,int b)
113 {
114     return a<b?a:b;
115 }

原文地址:https://www.cnblogs.com/--HPY-7m/p/11440707.html

时间: 2024-11-07 03:31:42

双元素非递增(容斥)--Number Of Permutations Educational Codeforces Round 71 (Rated for Div. 2)的相关文章

Educational Codeforces Round 71 (Rated for Div. 2) D - Number Of Permutations

原文链接:https://www.cnblogs.com/xwl3109377858/p/11405773.html Educational Codeforces Round 71 (Rated for Div. 2) D - Number Of Permutations You are given a sequence of n pairs of integers: (a1,b1),(a2,b2),…,(an,bn). This sequence is called bad if it is

Educational Codeforces Round 81 (Rated for Div. 2) A Display The Number

A. Display The Number time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output You have a large electronic screen which can display up to 998244353998244353 decimal digits. The digits are displayed

【Educational Codeforces Round 81 (Rated for Div. 2) A】Display The Number

题目链接 [题解] 优先用2个棒子来凑1. 如果为奇数的话,多出来一根用3根来凑个7放在开头 [代码] #include <bits/stdc++.h> using namespace std; int main(){ #ifdef LOCAL_DEFINE freopen("E:\\rush.txt","r",stdin); #endif // LOCAL_DEFINE ios::sync_with_stdio(0),cin.tie(0); int T

XTU 1242 Yada Number 容斥

Yada Number Problem Description: Every positive integer can be expressed by multiplication of prime integers. Duoxida says an integer is a yada number if the total amount of 2,3,5,7,11,13 in its prime factors is even. For instance, 18=2 * 3 * 3 is no

ZOJ 3233 Lucky Number 容斥

给你a数组和b数组 求x到y之间有多少个数至少被a中一个数整除并且至少不被b中一个数整除 容斥第一问很简单 第二问可以考虑反面 设满足被a中至少一个数整除的数有sum1个 在被a中至少一个数整除的前提下 被b中所有数整除的数有sum2 答案就是sum1-sum2 在dfs的时候溢出了 借鉴了某大牛的方法 #include <cstdio> #include <cstring> using namespace std; typedef long long LL; const int

HDU 4135 Co-prime(容斥+数论)

Co-prime Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5526    Accepted Submission(s): 2209 Problem Description Given a number N, you are asked to count the number of integers between A and B

hdu 1796 How many integers can you find 容斥第一题

How many integers can you find Time Limit: 12000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6710    Accepted Submission(s): 1946 Problem Description Now you get a number N, and a M-integers set, you should

hdu5072 Coprime 2014鞍山现场赛C题 计数+容斥

http://acm.hdu.edu.cn/showproblem.php?pid=5072 Coprime Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 354    Accepted Submission(s): 154 Problem Description There are n people standing in a

HDOJ1796 How many integers can you find(dfs+容斥)

How many integers can you find Time Limit: 12000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6048    Accepted Submission(s): 1735 Problem Description Now you get a number N, and a M-integers set, you should