bzoj#4555. [Tjoi2016&Heoi2016]求和
题目描述
Solution
有一个关于第二类斯特林数的公式:
{
n
m
}
=
1
m
!
∑
i
(
m
k
)
(
m
−
k
)
n
(
−
1
)
k
\left\{ \begin{aligned} n \\ m \end{aligned} \right\}= \frac{1}{m!}\sum_i \left ( \begin{aligned} m \\ k \end{aligned} \right )(m-k)^n(-1)^k
{nm}=m!1i∑(mk)(m−k)n(−1)k
因此:
A
n
s
=
∑
i
=
0
n
∑
j
=
0
i
∑
k
=
0
j
(
j
k
)
(
j
−
k
)
i
(
−
1
)
k
2
j
Ans=\sum_{i=0}^n\sum_{j=0}^i\sum_{k=0}^j\tbinom{j}{k}(j-k)^i(-1)^k2^j
Ans=i=0∑nj=0∑ik=0∑j(kj)(j−k)i(−1)k2j
进一步的:
A
n
s
=
∑
j
2
j
j
!
∑
k
∑
i
(
−
1
)
k
k
!
(
j
−
k
)
i
(
j
−
k
)
!
Ans=\sum_{j}2^jj!\sum_{k}\sum_{i}\frac{(-1)^k}{k!}\frac{(j-k)^i}{(j-k)!}
Ans=j∑2jj!k∑i∑k!(−1)k(j−k)!(j−k)i
发现后面就是一个卷积形式。
直接
N
u
m
b
e
r
T
h
e
o
r
e
t
i
c
T
r
a
n
s
f
o
r
m
Number\;\;Theoretic\;\;Transform
NumberTheoreticTransform计算即可。
时间复杂度 O ( n l g n ) O(nlgn) O(nlgn)。
Code
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <cassert>
#include <string.h>
//#include <unordered_set>
//#include <unordered_map>
//#include <bits/stdc++.h>
#define MP(A,B) make_pair(A,B)
#define PB(A) push_back(A)
#define SIZE(A) ((int)A.size())
#define LEN(A) ((int)A.length())
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define fi first
#define se second
using namespace std;
template<typename T>inline bool upmin(T &x,T y) { return y<x?x=y,1:0; }
template<typename T>inline bool upmax(T &x,T y) { return x<y?x=y,1:0; }
typedef long long ll;
typedef unsigned long long ull;
typedef long double lod;
typedef pair<int,int> PR;
typedef vector<int> VI;
const lod eps=1e-11;
const lod pi=acos(-1);
const int oo=1<<30;
const ll loo=1ll<<62;
const int mods=998244353;
const int G=3;
const int Gi=(mods+1)/G;
const int MAXN=600005;
const int INF=0x3f3f3f3f;//1061109567
/*--------------------------------------------------------------------*/
inline int read()
{
int f=1,x=0; char c=getchar();
while (c<'0'||c>'9') { if (c=='-') f=-1; c=getchar(); }
while (c>='0'&&c<='9') { x=(x<<3)+(x<<1)+(c^48); c=getchar(); }
return x*f;
}
int f[MAXN],g[MAXN],rev[MAXN],fac[MAXN],Limit,L;
int upd(int x,int y) { return x+y>=mods?x+y-mods:x+y; }
int quick_pow(int x,int y)
{
int ret=1;
for (;y;y>>=1)
{
if (y&1) ret=1ll*ret*x%mods;
x=1ll*x*x%mods;
}
return ret;
}
void Number_Theoretic_Transform(int *A,int type)
{
for (int i=0;i<Limit;i++)
if (i<rev[i]) swap(A[i],A[rev[i]]);
for (int mid=1;mid<Limit;mid<<=1)
{
int Wn=quick_pow(type==1?G:Gi,(mods-1)/(mid<<1));
for (int j=0;j<Limit;j+=(mid<<1))
for (int k=j,w=1;k<j+mid;w=1ll*w*Wn%mods,k++)
{
int x=A[k],y=1ll*w*A[k+mid]%mods;
A[k]=upd(x,y),A[k+mid]=upd(x,mods-y);
}
}
if (type==-1)
for (int inv=quick_pow(Limit,mods-2),i=0;i<Limit;i++) A[i]=1ll*A[i]*inv%mods;
}
int main()
{
int n=read();
fac[0]=1;
for (int i=1;i<=n;i++) fac[i]=1ll*fac[i-1]*i%mods;
f[n]=quick_pow(fac[n],mods-2);
for (int i=n-1;i>=0;i--) f[i]=1ll*f[i+1]*(i+1)%mods;
f[0]=g[0]=1,f[1]=n+1,g[1]=mods-1;
for (int i=2;i<=n;i++)
{
g[i]=(i&1)?mods-f[i]:f[i];
f[i]=1ll*upd(quick_pow(i,n+1),mods-1)*quick_pow(i-1,mods-2)%mods*f[i]%mods;
}
Limit=1,L=0;
while (Limit<=n<<1) Limit<<=1,L++;
for (int i=0;i<Limit;i++) rev[i]=(rev[i>>1]>>1)|((i&1)<<(L-1));
Number_Theoretic_Transform(f,1);
Number_Theoretic_Transform(g,1);
for (int i=0;i<Limit;i++) f[i]=1ll*f[i]*g[i]%mods;
Number_Theoretic_Transform(f,-1);
// for (int i=0;i<=n;i++) cout<<i<<" "<<f[i]<<endl;
int ans=0;
for (int i=0,t=1;i<=n;i++,t=upd(t,t)) ans=upd(ans,1ll*fac[i]*t%mods*f[i]%mods);
printf("%d\n",ans);
return 0;
}