# bzoj 4555: [Tjoi2016&Heoi2016]求和

 1 #include <cstdio>
2 #include <cstring>
3 #include <cmath>
4 #include <algorithm>
5 #include <iostream>
6 #define LL long long
7 using namespace std;
8
9 const int mod=998244353,G=3,maxn=(1<<18)+5;
10
11 LL ksm(LL a, LL p)
12 {
13     LL sum=1;
14     for (;p;p>>=1,a=a*a%mod)
15         if (p&1) sum=sum*a%mod;
16     return sum;
17 }
18
19 int N,rev[maxn];
20 void init(int n)
21 {
22     int L=0;
23     for (N=1; N<n; N<<=1) L++;
24     for (int i=0; i<N; i++) rev[i]=(rev[i>>1]>>1)|((i&1)<<(L-1));
25 }
26 void DFT(LL *a, int f)
27 {
28     for (int i=0; i<N; i++) if (i<rev[i]) swap(a[i],a[rev[i]]);
29     for (int h=2; h<=N; h<<=1)
30     {
31         LL wn=ksm(G,f==1?(mod-1)/h:mod-1-(mod-1)/h);
32         for (int i=0; i<N; i+=h)
33         {
34             LL w=1;
35             for (int j=0; j<(h>>1); j++,w=w*wn%mod)
36             {
37                 LL x=a[i+j],y=w*a[i+j+(h>>1)]%mod;
38                 a[i+j]=(x+y)%mod; a[i+j+(h>>1)]=(x-y+mod)%mod;
39             }
40         }
41     }
42     if (f==-1){
43         LL inv_N=ksm(N,mod-2);
44         for (int i=0; i<N; i++) a[i]=a[i]*inv_N%mod;
45     }
46 }
47 void mul(LL *a, LL *b)
48 {
49     DFT(a,1); DFT(b,1);
50     for (int i=0; i<N; i++) a[i]=a[i]*b[i]%mod;
51     DFT(a,-1);
52 }
53
54 int n;
55 LL inv[maxn], fac[maxn], facinv[maxn];
56 LL a[maxn], b[maxn];
57
58 int main()
59 {
60     cin>>n;
61     inv[1]=1; fac[0]=facinv[0]=1;
62     for (int i=1; i<=n; i++)
63     {
64         if (i!=1) inv[i]=(mod-mod/i)*inv[mod%i]%mod;
65         fac[i]=fac[i-1]*i%mod;
66         facinv[i]=facinv[i-1]*inv[i]%mod;
67     }
68     a[0]=1; b[0]=1; b[1]=n+1;
69     for (int i=1; i<=n; i++) a[i]=(i&1?-1:1)*facinv[i];
70     for (int i=2; i<=n; i++) b[i]=(ksm(i,n+1)-1)*inv[i-1]%mod*facinv[i]%mod;
71     init(2*n+1); mul(a,b);
72     LL ans=0;
73     for (int i=0; i<=n; i++) ans+=ksm(2,i)*fac[i]%mod*a[i]%mod,ans=(ans+mod)%mod;
74     cout<<ans<<endl;
75     return 0;
76 }
77 /*
78 #include<bits/stdc++.h>
79 using namespace std;
80 int s[15][15];
81 int main()
82 {
83     for (int i=0; i<=10; i++) s[i][i]=1;
84     for (int i=1; i<=10; i++)
85         for (int j=1; j<i; j++)
86             s[i][j]=s[i-1][j]*j+s[i-1][j-1];
87     for (int i=1; i<=10; i++,cout<<endl)
88         for (int j=1; j<=i; j++)
89             cout<<s[i][j]<<" ";
90     return 0;
91 }*/

posted @ 2017-04-19 11:33  ws_ccd  阅读(259)  评论(0编辑  收藏  举报