题解:P15714 [JAG 2023 Summer Camp #2] Distance Permutation
jiazhichen844 · · 题解
做十几分钟会了。
首先可以直接找到一个排列
然后考虑固定
首先考虑怎么求出
然后是比较经典的,拆拆式子有
然后就考虑询问,发现我们需要回答的形如
这个看着其实并没有非常好做,我们考虑一下分块,设长度
对于散块显然就暴力处理,整块上设这一块的区间为
然后就直接令
分析一下复杂度,预处理要做
如果你的 poly 板子像我一样慢可能会有点卡常,注意到预处理时
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int mod=998244353;
int pw(int a,int b)
{
int ans=1;
while(b)
{
if(b&1)
ans=ans*a%mod;
a=a*a%mod;
b>>=1;
}
return ans;
}
namespace poly
{
//poly 板子
}
int k,q;
const int n=1e5,len=800;
int f[100005],g[300005],h[300005],fac[100005],invfac[100005],belong[100005];
int tmp[205][100005];
void solve(int l,int r)
{
if(l==r)
return;
int mid=l+r>>1;
solve(l,mid);
for(int i=0;i<=mid-l;i++)
g[i]=f[i+l]*invfac[i+l]%mod;
for(int i=0;i<=r-l;i++)
h[i]=(i>=1)&&(i<=k);
poly::mul(g,mid-l+1,h,r-l+1);
for(int i=mid+1;i<=r;i++)
(f[i]+=fac[i-1]*g[i-l])%=mod;
solve(mid+1,r);
}
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
fac[0]=1;
for(int i=1;i<=n;i++)
fac[i]=fac[i-1]*i%mod;
invfac[n]=pw(fac[n],mod-2);
for(int i=n-1;i>=0;i--)
invfac[i]=invfac[i+1]*(i+1)%mod;
cin>>k>>q;
f[0]=1;
solve(0,n);
for(int i=0;i<=n;i++)
f[i]=f[i]*invfac[i]%mod;
for(int i=1;i<=n;i++)
belong[i]=(i+len-1)/len;
for(int i=0;i<=n;i++)
h[i]=f[i];
int x=poly::complete(n+len+1),ix=pw(x,mod-2);
poly::init(x);
poly::NTT(h,x,0);
for(int i=1;i<=belong[n];i++)
{
for(int j=i*len-len+1;j<=i*len;j++)
g[j-(i*len-len+1)]=(belong[j]==i)*f[j-1];
for(int j=len;j<x;j++)
g[j]=0;
poly::NTT(g,x,0);
for(int j=0;j<x;j++)
g[j]=g[j]*h[j]%mod;
poly::NTT(g,x,1);
for(int j=0;j<x;j++)
g[j]=g[j]*ix%mod;
for(int j=i*len-len+1;j<=n;j++)
tmp[i][j]=fac[j-1]*g[j-(i*len-len+1)]%mod;
}
for(int i=1;i<=q;i++)
{
int n,l,r;
cin>>n>>l>>r;
int ans=0;
if(belong[l]==belong[r])
for(int j=l;j<=r;j++)
(ans+=f[j-1]*f[n-j]%mod*fac[n-1])%=mod;
else
{
int bl=belong[l],br=belong[r];
for(int j=l;j<=bl*len;j++)
(ans+=f[j-1]*f[n-j]%mod*fac[n-1])%=mod;
for(int j=bl+1;j<br;j++)
(ans+=tmp[j][n])%=mod;
for(int j=br*len-len+1;j<=r;j++)
(ans+=f[j-1]*f[n-j]%mod*fac[n-1])%=mod;
}
cout<<ans<<"\n";
}
}