QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#827771 | #4492. Yet Another Easy Permutation Count Problem | Kevin5307 | AC ✓ | 1213ms | 30956kb | C++23 | 3.1kb | 2024-12-23 09:50:00 | 2024-12-23 09:50:08 |
Judging History
answer
//Author: Kevin
#include<bits/stdc++.h>
//#pragma GCC optimize("O2")
using namespace std;
#define ll long long
#define ull unsigned ll
#define pb emplace_back
#define mp make_pair
#define ALL(x) (x).begin(),(x).end()
#define rALL(x) (x).rbegin(),(x).rend()
#define srt(x) sort(ALL(x))
#define rev(x) reverse(ALL(x))
#define rsrt(x) sort(rALL(x))
#define sz(x) (int)(x.size())
#define inf 0x3f3f3f3f
#define pii pair<int,int>
#define lb(v,x) (int)(lower_bound(ALL(v),x)-v.begin())
#define ub(v,x) (int)(upper_bound(ALL(v),x)-v.begin())
#define uni(v) v.resize(unique(ALL(v))-v.begin())
#define longer __int128_t
void die(string S){puts(S.c_str());exit(0);}
const ll mod=998244353;
const ll inv2=(mod+1)/2;
const ll inv6=(mod+1)/6;
ll fact[1001000];
int p[1001000];
int psum[1001000];
namespace bit1
{
int bit[1001000];
void update(int p,int v)
{
while(p<1001000)
{
bit[p]+=v;
p+=(p&(-p));
}
}
int query(int p)
{
int ans=0;
while(p)
{
ans+=bit[p];
p-=(p&(-p));
}
return ans;
}
}
namespace bit2
{
#define int ll
int bit[1001000];
void update(int p,int v)
{
while(p<1001000)
{
bit[p]+=v;
p+=(p&(-p));
}
}
int query(int p)
{
int ans=0;
while(p)
{
ans+=bit[p];
p-=(p&(-p));
}
return ans;
}
#undef int
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
fact[0]=1;
for(int i=1;i<1001000;i++) fact[i]=fact[i-1]*i%mod;
int t;
cin>>t;
while(t--)
{
int n,m;
cin>>n>>m;
for(int i=1;i<=n;i++)
{
bit1::bit[i]=0;
bit2::bit[i]=0;
p[i]=-1;
psum[i]=1;
}
int cc=0;
for(int i=0;i<m;i++)
{
int x,y;
cin>>x>>y;
if(~p[x]) cc++;
p[x]=y;
psum[y]=0;
}
m-=cc;
for(int i=1;i<=n;i++)
psum[i]+=psum[i-1];
int cnt=0;
ll s1=0;
ll ans=0;
for(int i=1;i<n;i++)
{
if(p[i]==-1&&p[i+1]==-1)
{
// AAA
ans=(ans+cnt*fact[n-m]%mod*inv6)%mod;
// BAA
ans=(ans+s1*fact[n-m-2])%mod;
// AA
ans=(ans+fact[n-m]*inv2)%mod;
}
else if(p[i]==-1)
{
// AAB
ans=(ans+1ll*(psum[n]-psum[p[i+1]])*(psum[n]-psum[p[i+1]]-1)/2%mod*fact[n-m-2]%mod*cnt)%mod;
// BAB
ll s=bit2::query(n)-bit2::query(p[i+1]);
int c=bit1::query(n)-bit1::query(p[i+1]);
ans=(ans+(1ll*c*psum[n]-s)%mod*fact[n-m-1])%mod;
// AB
ans=(ans+(psum[n]-psum[p[i+1]])*fact[n-m-1])%mod;
}
else if(p[i+1]==-1)
{
// ABA
ans=(ans+1ll*psum[p[i]]*(psum[p[i]]-1)/2%mod*fact[n-m-2]%mod*cnt)%mod;
// BBA
ll s=bit2::query(p[i]);
ans=(ans+s%mod*fact[n-m-1])%mod;
// BA
ans=(ans+psum[p[i]]*fact[n-m-1])%mod;
}
else
{
// ABB
ans=(ans+fact[n-m-1]*cnt%mod*max(0,psum[p[i]]-psum[p[i+1]]))%mod;
// BBB
ans=(ans+fact[n-m]*max(0,bit1::query(p[i])-bit1::query(p[i+1])))%mod;
// BB
ans=(ans+(p[i]>p[i+1])*fact[n-m])%mod;
}
if(p[i]==-1)
cnt++;
else
{
s1=(s1+1ll*psum[p[i]]*(psum[n]-psum[p[i]]))%mod;
bit1::update(p[i],1);
bit2::update(p[i],psum[p[i]]);
}
}
cout<<ans<<'\n';
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1213ms
memory: 30956kb
input:
100 1 1 1 1 7513 7311 5538 3089 3928 1029 5512 3849 100 1643 7328 4748 5811 196 6557 2258 3348 790 2760 882 1297 689 7500 6955 3619 3686 3724 3401 6412 4691 6471 4947 6315 5754 1752 1086 2648 124 6721 2929 7185 5179 7239 6148 3005 7383 6676 3884 3074 6165 5062 5842 1145 5090 7376 848 5418 6441 275 6...
output:
0 89541616 367554988 216909100 990456909 526769607 506026027 780187494 553834792 144362048 842584375 861132490 730089994 250057476 295193259 96890952 451932499 132980145 430587965 19967698 589057985 811887276 759432199 345092973 588708812 875024174 665937616 34601572 507128381 479135581 798327226 74...
result:
ok 100 lines