CS Academy Round 75 Permutations NTT

Description


q次询问(x,y),求长度为n的排列p[],满足p[y]是前y个中最大的,且p[x]*2<p[y]的数量

Solution


考虑枚举第y位选了啥,那么第x位的范围就出来了,于是答案就是f(y)=i=1ni12(i2y2)(y2)!(ny)!f(y)=\sum_{i=1}^n{\lfloor\frac{i-1}{2}\rfloor\binom{i-2}{y-2}\left(y-2\right)!\left(n-y\right)!}f(y)=i=1∑n​⌊2i−1​⌋(y−2i−2​)(y−2)!(n−y)!
可以发现x是一个酱油。。NTT就好了

Code


#include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>

#define rep(i,st,ed) for (int i=st;i<=ed;++i)
#define drp(i,st,ed) for (int i=st;i>=ed;--i)
#define fill(x,t) memset(x,t,sizeof(x))
#define copy(x,t) memcpy(x,t,sizeof(x))

#define fi first
#define se second

typedef std:: pair <int,int> pair;
typedef long double ld;
typedef long long LL;

const int INF=0x3f3f3f3f;
const int MOD=998244353;
const int ny2=(MOD+1)/2;
const int N=2000005;
const ld pi=acos(-1);
const ld e=exp(1);
const ld eps=1e-8;

LL fac[N],inv[N],f[N],g[N];
int rv[N];

int read() {
	int x=0,v=1; char ch=getchar();
	for (;ch<'0'||ch>'9';v=(ch=='-')?(-1):v,ch=getchar());
	for (;ch<='9'&&ch>='0';x=x*10+ch-'0',ch=getchar());
	return x*v;
}

LL ksm(LL x,LL dep) {
	LL res=1;
	for (;dep;dep>>=1,x=x*x%MOD) {
		(dep&1)?(res=res*x%MOD):0;
	}
	return res;
}

void NTT(LL *a,int n,int f) {
	for (int i=0;i<n;++i) if (i<rv[i]) std:: swap(a[i],a[rv[i]]);
	for (int i=1;i<n;i<<=1) {
		LL wn=ksm(3,(f==1)?((MOD-1)/i/2):(MOD-1-(MOD-1)/i/2));
		for (int j=0;j<n;j+=(i<<1)) {
			LL w=1;
			for (int k=0;k<i;++k,w=w*wn%MOD) {
				LL u=a[j+k],v=a[j+k+i]*w%MOD;
				a[j+k]=u+v,(a[j+k]>=MOD)?(a[j+k]-=MOD):0;
				a[j+k+i]=u-v,(a[j+k+i]<0)?(a[j+k+i]+=MOD):0;
			}
		}
	}
	if (f==-1) {
		LL ny=ksm(n,MOD-2);
		for (int i=0;i<n;++i) a[i]=a[i]*ny%MOD;
	}
}

int main(void) {
	fac[0]=fac[1]=inv[0]=inv[1]=1;
	rep(i,2,N-1) {
		fac[i]=fac[i-1]*i%MOD;
		inv[i]=inv[MOD%i]*(MOD-MOD/i)%MOD;
	}
	rep(i,2,N-1) inv[i]=inv[i-1]*inv[i]%MOD;
	int n=read(),q=read();
	rep(i,1,n) f[i]=(LL)((i-1)/2)*fac[i-2]%MOD,g[i]=inv[n-i];
	int len=1,lg=0; for (;len<=n*2;) len<<=1,++lg;
	for (int i=0;i<len;++i) rv[i]=(rv[i>>1]>>1)|((i&1)<<(lg-1));
	NTT(f,len,1),NTT(g,len,1);
	for (int i=0;i<len;++i) f[i]=f[i]*g[i]%MOD;
	NTT(f,len,-1);
	for (;q--;) {
		int x=read(),y=read();
		printf("%lld\n", f[n+y]*fac[n-y]%MOD);
	}
	return 0;
}
上一篇:第一和第二类斯特林数的学习


下一篇:「THUPC2018」赛艇 / Citing