发现就是要求不能有一个点度数超过 2 2 2,且不能存在任何长度为 3 3 3的倍数的环。
那么每个连通分量要么就是链,要么就是长度不为 3 3 3的环。
发现带标号,构造两者的EGF,加起来Exp即可。
代码:
#include
#define ll long long
#define re register
#define cs const
using std::cerr;
using std::cout;
std::ostream &operator<<(std::ostream &out,cs std::vector<int> &a){
if(!a.size())out<<"empty ";
for(int re i:a)out<<i<<" ";
return out;
}
cs int mod=1004535809;
inline int add(int a,int b){return (a+=b)>=mod?a-mod:a;}
inline int dec(int a,int b){return (a-=b)<0?a+mod:a;}
inline int mul(int a,int b){ll r=(ll)a*b;return r>=mod?r%mod:r;}
inline int power(int a,int b,int res=1){
for(;b;b>>=1,a=mul(a,a))(b&1)&&(res=mul(res,a));
return res;
}
inline void Inc(int &a,int b){(a+=b)>=mod&&(a-=mod);}
inline void Dec(int &a,int b){(a-=b)<0&&(a+=mod);}
typedef std::vector<int> Poly;
cs int bit=21,N=1<<bit|1;
int r[N],*w[bit+1];
inline void init_NTT(){
for(int re i=1;i<=bit;++i)w[i]=new int[(1<<i-1)+1];
int wn=power(3,mod-1>>bit);
w[bit][0]=1;
for(int re i=1;i<(1<<bit-1);++i)w[bit][i]=mul(w[bit][i-1],wn);
for(int re i=bit-1;i;--i)
for(int re j=0;j<(1<<i-1);++j)w[i][j]=w[i+1][j<<1];
}
inline void NTT(Poly &A,int len,int typ){
for(int re i=0;i<len;++i)if(i<r[i])std::swap(A[i],A[r[i]]);
for(int re i=1,t=1;i<len;i<<=1,++t)
for(int re j=0;j<len;j+=i<<1)
for(int re k=0;k<i;++k){
int x=A[j+k],y=mul(A[j+k+i],w[t][k]);
A[j+k]=add(x,y),A[j+k+i]=dec(x,y);
}
if(typ==-1){
std::reverse(A.begin()+1,A.begin()+len);
for(int re i=0,inv=power(len,mod-2);i<len;++i)A[i]=mul(A[i],inv);
}
}
inline void init_rev(int len){
for(int re i=0;i<len;++i)r[i]=r[i>>1]>>1|((i&1)?len>>1:0);
}
inline Poly operator*(Poly a,Poly b){
int deg=a.size()+b.size()-1,l=1;
while(l<=deg)l<<=1;init_rev(l);
a.resize(l),NTT(a,l,1);
b.resize(l),NTT(b,l,1);
for(int re i=0;i<l;++i)a[i]=mul(a[i],b[i]);
NTT(a,l,-1),a.resize(deg);
return a;
}
int fac[N],ifac[N],inv[N];
inline void init_inv(){
fac[0]=fac[1]=ifac[0]=ifac[1]=inv[0]=inv[1]=1;
for(int re i=2;i<N;++i){
fac[i]=mul(fac[i-1],i);
inv[i]=mul(inv[mod%i],mod-mod/i);
ifac[i]=mul(ifac[i-1],inv[i]);
}
}
inline Poly Deriv(Poly a){
for(int re i=0;i+1<a.size();++i)a[i]=mul(a[i+1],i+1);
a.pop_back();return a;
}
inline Poly Integ(Poly a){
a.push_back(0);
for(int re i=a.size()-1;i;--i)a[i]=mul(a[i-1],inv[i]);
a[0]=0;return a;
}
inline Poly Inv(cs Poly &a,int lim){
Poly c,b(1,power(a[0],mod-2));
for(int re l=4;(l>>2)<lim;l<<=1){
init_rev(l);
c.resize(l>>1);for(int re i=0;i<(l>>1);++i)c[i]=i<a.size()?a[i]:0;
c.resize(l),NTT(c,l,1);
b.resize(l),NTT(b,l,1);
for(int re i=0;i<l;++i)b[i]=mul(b[i],dec(2,mul(b[i],c[i])));
NTT(b,l,-1),b.resize(l>>1);
}b.resize(lim);
return b;
}
inline Poly Ln(Poly a,int lim){
a=Integ(Deriv(a)*Inv(a,lim));
a.resize(lim);
return a;
}
inline Poly Exp(cs Poly a){
Poly c,b(1,1);int lim=a.size();
for(int re l=2;(l>>1)<lim;l<<=1){
c=Ln(b,l);
for(int re i=0;i<l;++i)c[i]=dec(i<a.size()?a[i]:0,c[i]);
Inc(c[0],1);
b=b*c;b.resize(l);
}b.resize(lim);
return b;
}
Poly f;
int n;
signed main(){
#ifdef zxyoi
// freopen("tibbar.in","r",stdin);
#endif
init_NTT();init_inv();
std::cin>>n;
f.resize(n+1);
f[1]=1;f[2]=inv[2];
for(int re i=3;i<=n;++i){
f[i]=mul(fac[i],inv[2]);
if(i%3)Inc(f[i],mul(inv[2],fac[i-1]));
f[i]=mul(f[i],ifac[i]);
}
std::cout<<mul(Exp(f)[n],fac[n])<<"\n";
return 0;
}