Solution
d p [ n ] = E + ∑ i = 0 n − 1 C n i d p [ i ] + d p [ n ] 2 n dp[n]=E+\frac{\sum\limits_{i=0}^{n-1}C_n^idp[i]+dp[n]}{2^n} dp[n]=E+2ni=0∑n−1Cnidp[i]+dp[n]
E = n ( n − 1 ) 4 E=\frac{n(n-1)}{4} E=4n(n−1)
d p [ n ] = n ( n − 1 ) 4 + ∑ i = 0 n − 1 C n i d p [ i ] 2 n 1 − 1 2 n dp[n]=\frac{\frac{n(n-1)}{4}+\frac{\sum\limits_{i=0}^{n-1}C_n^idp[i]}{2^n}}{1-\frac{1}{2^n}} dp[n]=1−2n14n(n−1)+2ni=0∑n−1Cnidp[i]
r e s = ∑ i = 0 n d p [ i ] n res=\frac{\sum\limits_{i=0}^{n}dp[i]}{n} res=ni=0∑ndp[i]
Code
/*SiberianSquirrel*/
/*CuteKiloFish*/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MOD = 998244353;
int dp[3010] = {0, 0};
ll quick_pow(ll ans, ll p, ll res = 1) {
for(; p; p >>= 1, ans = ans * ans % MOD)
if(p & 1) res = res * ans % MOD;
return res % MOD;
}
ll inv(ll ans) {
return quick_pow(ans , MOD - 2);
}
ll inv4 = inv(4);
ll fac[3010] = {1, 1};
ll C[3010][3010];
void solve() {
for(ll i = 2; i <= 3000; ++ i) {
fac[i] = fac[i - 1] * i % MOD;
}
for(int i = 1; i <= 3000; ++ i) {
C[i][0] = C[i][i] = 1;
for(int j = 1; j < i; ++ j) {
C[i][j] = (C[i - 1][j] + C[i - 1][j - 1]) % MOD;
}
}
for(ll i = 2; i <= 3000; ++ i) {
ll res = i * (i - 1) % MOD * inv4 % MOD;
ll ans = 0;
for(ll j = 0; j <= i - 1; ++ j) {
ans = (ans + C[i][j] * dp[j] % MOD) % MOD;
}
ll invn2 = inv(quick_pow(2, i));
ans = ans * invn2 % MOD;
ll temp = inv((1 - invn2 + MOD) % MOD);
dp[i] = (res + ans) % MOD * temp % MOD;
}
int n; while (cin >> n) {
ll res = 0;
for(int i = 0; i <= n; ++ i) {
res = (res + dp[i]) % MOD;
}
res = res * inv(n) % MOD;
cout << res << endl;
}
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
#ifdef ACM_LOCAL
freopen("input", "r", stdin);
freopen("output", "w", stdout);
signed test_index_for_debug = 1;
char acm_local_for_debug = 0;
do {
if (acm_local_for_debug == '$') exit(0);
if (test_index_for_debug > 20)
throw runtime_error("Check the stdin!!!");
auto start_clock_for_debug = clock();
solve();
auto end_clock_for_debug = clock();
cout << "Test " << test_index_for_debug << " successful" << endl;
cerr << "Test " << test_index_for_debug++ << " Run Time: "
<< double(end_clock_for_debug - start_clock_for_debug) / CLOCKS_PER_SEC << "s" << endl;
cout << "--------------------------------------------------" << endl;
} while (cin >> acm_local_for_debug && cin.putback(acm_local_for_debug));
#else
solve();
#endif
return 0;
}