2023牛客五一集训派对Day1 J-Easy Integration
链接:https://ac.nowcoder.com/acm/contest/55992/J
头一回做一个需要求积分的题目。爽的嘞~~蒟蒻小姐积分不会求,求出积分也不会写代码 😢
题目描述
Given n n n, find the value of ∫ 0 1 ( x − x 2 ) n d x \int_{0}^1 (x - x^2)^n \mathrm{d} x ∫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 998244353 (p \cdot q^{-1}) \bmod 998244353 (p⋅q−1)mod998244353.
输入描述
The input consists of several test cases and is terminated by end-of-file.
Each test case contains an integer n n n.
*
1
≤
n
≤
1
0
6
1 \leq n \leq 10^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 2 3
输出:
166374059 432572553 591816295
备注
For n = 1 n = 1 n=1, ∫ 0 1 ( x − x 2 ) d x = x 2 2 − x 3 3 ∣ 0 1 = 1 6 \int_{0}^1 (x - x^2) \mathrm{d} x = \frac{x^2}{2} - \frac{x^3}{3} |_0^1 = \frac{1}{6} ∫01(x−x2)dx=2x2−3x3∣01=61
题解
先计算公式:
得出结论: ∫ 0 1 ( x − x 2 ) n d x = n ! ⋅ n ! ( 2 n − 1 ) ! \int_{0}^1 (x - x^2)^n \mathrm{d} x=\frac{n!·n!}{(2n-1)!} ∫01(x−x2)ndx=(2n−1)!n!⋅n!
此时题意转化为:给定整数 n n n ,求 $\frac{n!·n!}{(2n-1)!} \bmod 998244353 $ 的值。
注意⚠:
- 求阶乘用数组 f f f 记录,类似于前缀和的做法。
- 使用快速幂求逆元将原本的除法转换为乘法(除法计算会有精度缺失等问题)。
代码
#include <iostream>
#include <cmath>
using namespace std;
const int N=1e6+7, MOD=998244353;
long long f[2*N]; //f[i]表示i的阶乘
long long fun(long long base, long long power) //快速幂模板
{
long long result=1;
while(power>0)
{
if(power&1) result=result*base%MOD;
power>>=1;
base=(base*base)%MOD;
}
return result;
}
int main()
{
int n;
long long ans=1;
for(long long i=1; i<=2*N+1; ++i)
{
ans*=i;
ans%=MOD;
f[i]=ans;
}
while(cin>>n)
{
long long a, b;
a=f[n]%MOD, b=f[2*n+1]%MOD;
cout<<(a*a)%MOD*fun(b, MOD-2)%MOD<<endl; //快速幂求逆元将原本的除法转换为乘法
}
return 0;
}