题目链接:https://ac.nowcoder.com/acm/contest/5666/J
题目描述
Given n, find the value of ∫ 0 1 ( x − x 2 ) n d x \int_{0}^{1}(x-x^2)^ndx ∫01(x−x2)ndx
It can be proved that the value is a rational number p q \frac{p}{q} qp
Print the result as ( p ⋅ q − 1 ) m o d (p⋅q^{-1})mod (p⋅q−1)mod 998244353 998244353 998244353.
输入描述
The input consists of several test cases and is terminated by end-of-file.
Each test case contains an integer n.
* 1 ≤ n ≤ 1 0 6 1\leq n \leq10^6 1≤n≤106.
*The number of test cases does not exceed 1 0 5 10^5 105.
输出描述
For each test case, print an integer which denotes the result.
输入
1 1 1
2 2 2
3 3 3
输出
166374059 166374059 166374059
432572553 432572553 432572553
591816295 591816295 591816295
思路
根据 ∫ 0 1 ( x − x 2 ) n d x \int_{0}^{1}(x-x^2)^ndx ∫01(x−x2)ndx可得 ∫ 0 1 x n ( 1 − x ) n d x \int_{0}^{1}x^{n}(1-x)^ndx ∫01xn(1−x)ndx。
利用n次分部积分法可得计算式 1 ∗ 2 ∗ . . . ∗ ( n − 1 ) ∗ n ( n + 1 ) ∗ ( n + 2 ) ∗ . . . . ∗ ( 2 n ) ∗ ( 2 n + 1 ) \frac{1*2*...*(n-1)*n}{(n + 1)*(n+2)*....*(2n)*(2n+1)} (n+1)∗(n+2)∗....∗(2n)∗(2n+1)1∗2∗...∗(n−1)∗n
上下同乘 n ! n! n!得
( n ! ) 2 ( 2 n + 1 ) ! \frac{(n!)^2}{(2n+1)!} (2n+1)!(n!)2
先对阶乘做一个预处理,然后就成分子除以分母,可以用逆元(费马小定理)求。
Code
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define INF 0x7f7f7f
#define mem(a,b) memset(a , b , sizeof(a))
#define FOR(i, x, n) for(int i = x;i <= n; i++)
//const ll mod = 1e9 + 7;
// const int maxn = 1e5 + 10;
const ll mod = 998244353;
int n;
ll f[2000005];
ll FPM(ll x,ll power)
{
ll ans = 1;
while(power)
{
if(power & 1)
{
ans = (ans * x) % mod;
}
x = (x * x) % mod;
power >>= 1;
}
return ans % mod;
}
void F()
{
f[0] = 1;
for(int i = 1;i <= 2000001; i++)
{
f[i] = f[i - 1] * i % mod;
}
}
void solve()
{
F();
while(scanf("%d",&n)!=EOF)
{
ll ans = f[n] * f[n] % mod;
ans = (ans * FPM(f[2 * n + 1], mod - 2)) % mod;
printf("%lld\n",ans % mod);
}
}
signed main()
{
//ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
#ifdef FZT_ACM_LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#else
//ios::sync_with_stdio(false);
int T = 1;
//cin >> T;
while(T--)
solve();
#endif
return 0;
}