首页 > 其他分享 >SMU Summer 2023 Contest Round 6

SMU Summer 2023 Contest Round 6

时间:2023-08-03 12:58:26浏览次数:40  
标签:Summer temp Contest int SMU ans pair fac mod

Problem - D. Number Of Permutations

传送门

容斥原理

思路:利用容斥,首先所有可能的排列肯定是fac[n],然后可能会有三种 bad 的情况:

①第一个元素的排列是非递减

②第二种是第二个元素的排列是非递减

③这两个可能出现的重叠情况,意思就是说同时导致①②成立

这个时候我们利用容斥的思想,用fac[n]-①-②+③即可

我们把所有的pair按照第一个元素优先排列的方式把所有的pair sort 一下( sort 对pair的排序方式是默认第一个元素优先的),这个时候我们就保证了所有pair的第一个元素组成的排列的肯定是一个不严格递增的排列

求③的时候需要注意的一点是,在已经按照第一个元素排完序之后,如果存在s[i+1].se<s[i].se,那么就表示不会有第三种情况发生因为s[i].fi<=s[i+1].fi所以如果按照第二个元素非降序排序的话,就会导致s[i+1].fi<s[i].fi,所以,如果出现这种情况则表明③=0

code

#include <bits/stdc++.h>

using namespace std;
#define int long long
typedef pair<int, int> pii;
const int mod = 998244353, N = 3e5 + 5;
pii s[N];
int fac[N];
map<pii, int> ab;
map<int, int> a, b;

signed main () {
    ios::sync_with_stdio (false);
    cin.tie (nullptr), cout.tie (nullptr);
    int n;
    cin >> n;
    fac[0] = 1;
    for (int i = 1; i <= n; ++i)
        fac[i] = fac[i - 1] * i % mod;
    for (int i = 1; i <= n; ++i) {
        int x, y;
        cin >> x >> y;
        a[x]++, b[y]++, ab[{x, y}]++;
        s[i] = make_pair (x, y);
    }
    sort (s + 1, s + n + 1);
    int ans = fac[n], temp = 1;
    for (auto i: a)
        temp = temp * fac[i.second] % mod;
    ans = (ans - temp + mod) % mod, temp = 1;
    for (auto i: b)
        temp = temp * fac[i.second] % mod;
    ans = (ans - temp + mod) % mod, temp = 1;
    for (auto i: ab)
        temp = temp * fac[i.second] % mod;
    for (int i = 1; i < n; ++i)
        if (s[i + 1].second < s[i].second) temp = 0;
    ans = (ans + temp) % mod;
    cout << ans << endl;
    return 0;
}

标签:Summer,temp,Contest,int,SMU,ans,pair,fac,mod
From: https://www.cnblogs.com/Lazyboyjdj/p/17603015.html

相关文章

  • SMU Summer 2023 Contest Round 1
    Problem-ATheContest(纯属眼瞎)#include<bits/stdc++.h>usingnamespacestd;typedeflonglongll;constintN=1e7+50,M=5050,mod=9901,MAX_N=1e3+50,INF=0x3f3f3f3f;constdoublePI=3.1415926;#defineIOSios_base::sync_with_stdio(f......
  • SMU Summer 2023 Contest Round 2
    Problem-ATreasureHunt#include<bits/stdc++.h>usingnamespacestd;typedeflonglongll;constintN=1e5+50,M=5e3+50,mod=9901,MAX_N=6e3+50,INF=0x3f3f3f3f;constdoublePI=3.1415926;#defineIOSios_base::sync_with_stdio(f......
  • SMU Summer 2023 Contest Round 3
    Problem-A-CurriculumVitae#include<bits/stdc++.h>usingnamespacestd;constintN=1e6+50,M=5050,mod=9901,MAX_N=1e3+50,INF=0x3f3f3f3f;#defineintlonglong#defineldlongdouble#defineIOSios_base::sync_with_stdio(false)......
  • The 10th Shandong Provincial Collegiate Programming Contest
    The10thShandongProvincialCollegiateProgrammingContestK-HappyEquation思路:a,x的奇偶性相同(因为都对偶数取模),且打表得出a为奇数时,答案为1。(¿)a为偶数时,令a=t1*2q  → ax=t1x*2qx若axmod2p为0,则qx>=p,x>=p/q;由于q>=1(a为偶数),则x>=p;x与a同为偶数,令x'=t2*2k→x'a......
  • AtCoder Beginner Contest 165
    AtCoderBeginnerContest165https://atcoder.jp/contests/abc165C-ManyRequirementsdfs#include<bits/stdc++.h>usingnamespacestd;constintN=15,M=55;intn,m,q,ans;inta[M],b[M],c[M],d[M],A[N];voiddfs(intx){if(x>......
  • AtCoder Beginner Contest 312
    A-Chord#include<bits/stdc++.h>usingnamespacestd;int32_tmain(){ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr);strings;cin>>s;if(s=="ACE")cout<<"Yes\n";e......
  • [UNIQUE VISION Programming Contest 2023 Summer(AtCoder Beginner Contest 312) - A
    UNIQUEVISIONProgrammingContest2023Summer(AtCoderBeginnerContest312)-AtCoderA-Chord(atcoder.jp)#include<bits/stdc++.h>#defineendl'\n'usingnamespacestd;intmain(){vector<string>str{"ACE",&qu......
  • Atcoder-Beginner-Contest-312 A~Ex
    \(Atcoder-Beginner-Contest-312\)AB过于简单,在此略去。\(C-Invisible\)\(Hand\)题意:给定长为\(n\)的数组\(a\),长为\(m\)的数组\(b\),找到最小的非负整数\(x\),使得\(\sum_{i=1}^n[a_i\lex]\ge\sum_{i=1}^m[b_i\gex]\)题解:容易发现,随着\(x\)的增大,右式单调不升,左......
  • (AtCoder Beginner Contest 312)
    (AtCoderBeginnerContest312)A-Chord#include<bits/stdc++.h>usingnamespacestd;#defineintlonglong//#defineint__int128typedefpair<int,int>PII;typedefpair<string,int>PSI;typedefpair<string,string>PSS;constintN......
  • SMU Summer 2023 Contest Round 7
    SMUSummer2023ContestRound7A.TwoRivalStudents答案不能大于\(n-1\);如果竞争对手之间的当前距离小于\(n-1\),我们总是可以将这个距离增加一个交换数;即答案等于\(min(n-1,|a-b|+x)\)。#include<bits/stdc++.h>#defineintlonglongusingnamespac......