【2020年牛客暑假第一场】J题 Easy integration

【2020年牛客暑假第一场】J题 Easy integration--积分、逆元

题目链接: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(xx2)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 (pq1)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 1n106.
*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(xx2)ndx可得 ∫ 0 1 x n ( 1 − x ) n d x \int_{0}^{1}x^{n}(1-x)^ndx 01xn(1x)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)12...(n1)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;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值