[ TJOI 2016 / HEOI 2016 ] 求和
题目
思路
代码
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 500010, p = 998244353, g = 3;
int n, m, A[N], B[N], rev[N], tot = 1, bit;
int FA[N], IF[N], G[N];
int qmi(int a, int b) {
int res = 1;
for (; b; b >>= 1, a = 1ll * a * a % p)
if (b & 1) res = 1ll * res * a % p;
return res;
}
int inv(int x) { return qmi(x, p - 2); }
void NTT(int A[], int t) {
for (int i = 0; i < tot; i++)
if (i < rev[i]) swap(A[i], A[rev[i]]);
for (int len = 1; len < tot; len <<= 1) {
int w = qmi(t ? 3 : 332748118, (p - 1) / (len << 1));
for (int i = 0; i < tot; i += len << 1) {
for (int j = 0, k = 1; j < len; j++, k = 1ll * k * w % p) {
int x = A[i + j], y = 1ll * k * A[i + j + len] % p;
A[i + j] = (x + y) % p, A[i + j + len] = (x - y + p) % p;
}
}
}
}
void NTT(int A[], int B[]) {
while (tot <= n + n) tot <<= 1, bit++;
for (int i = 0; i < tot; i++)
rev[i] = (rev[i >> 1] >> 1) | ((i & 1) << bit - 1);
NTT(A, 1), NTT(B, 1);
for (int i = 0; i < tot; i++) A[i] = 1ll * A[i] * B[i] % p;
NTT(A, 0);
for (int i = 0; i <= n; i++) G[i] = 1ll * A[i] * inv(tot) % p;
}
int main() {
cin >> n;
FA[0] = IF[0] = B[0] = 1, B[1] = n + 1;
for (int i = 1; i <= n; i++)
FA[i] = 1ll * FA[i - 1] * i % p, IF[i] = inv(FA[i]);
for (int i = 0; i <= n; i++)
A[i] = 1ll * (qmi(-1, i) + p) * IF[i] % p;
for (int i = 2; i <= n; i++)
B[i] = 1ll * (qmi(i, n + 1) - 1) * inv(i - 1) % p * IF[i] % p;
NTT(A, B);
int res = 0;
for (int i = 0; i <= n; i++)
res = (res + (1ll * qmi(2, i) * FA[i] % p * G[i]) % p) % p;
cout << res << endl;
return 0;
}