2019牛客暑期多校训练营(第一场)B.integration

链接:https://ac.nowcoder.com/acm/contest/881/B
来源:牛客网

题目描述

Given n distinct positive integers [a1, a2, a3 … an], find the value of
∫ 0 ∞ 1 ∏ i = 1 n ( a i 2 + x 2 ) d x \int_0^\infty {\frac{1}{{\prod\nolimits_{i = 1}^n {\left( {a_i^2 + {x^2}} \right)} }}dx} 0i=1n(ai2+x2)1dx
It can be proved that the value is a rational number p / q p/q p/q
Print the result as p ⋅ q − 1     m o d     ( 1 e 9 + 7 ) p \cdot {q^{ - 1}}\;\bmod \;(1e9 + 7) pq1mod(1e9+7)

输入描述:

The input consists of several test cases and is terminated by end-of-file.

The first line of each test case contains an integer n.
The second line contains n integers [a1 to an]2019牛客暑期多校训练营(第一场)B.integration_第1张图片

输出描述:

For each test case, print an integer which denotes the result.

InputSample

1
1
1
2
2
1 2

OutputSample

500000004
250000002
83333334

Solution:

We know that:
∫ 0 ∞ 1 ∏ i = 1 n ( a i 2 + x 2 ) d x = ∑ i = 1 n arctan ⁡ ( x a i ) a i ∏ f o r    a l l    j ! = i ( a j 2 − a i 2 ) ∣ 0 ∞ \int_0^\infty {\frac{1}{{\prod\nolimits_{i = 1}^n {\left( {a_i^2 + {x^2}} \right)} }}dx} = \sum\limits_{i = 1}^n {\left. {\frac{{\arctan \left( {\frac{x}{{{a_i}}}} \right)}}{{{a_i}\prod\nolimits_{for\;all\;j! = i} {\left( {{a_j}^2 - a_i^2} \right)} }}} \right|_0^\infty } 0i=1n(ai2+x2)1dx=i=1naiforallj!=i(aj2ai2)arctan(aix)0

So we have:
1 π ∫ 0 ∞ 1 ∏ i = 1 n ( a i 2 + x 2 ) d x = 1 π ∑ i = 1 n π 2 a i ∏ f o r    a l l    j ! = i ( a j 2 − a i 2 ) − 0 = ∑ i = 1 n 1 2 a i ∏ f o r    a l l    j ! = i ( a j 2 − a i 2 ) \frac{1}{\pi }\int_0^\infty {\frac{1}{{\prod\nolimits_{i = 1}^n {\left( {a_i^2 + {x^2}} \right)} }}dx} = \frac{1}{\pi }\sum\limits_{i = 1}^n {\frac{{\frac{\pi }{2}}}{{{a_i}\prod\nolimits_{for\;all\;j! = i} {\left( {{a_j}^2 - a_i^2} \right)} }} - 0 = } \sum\limits_{i = 1}^n {\frac{1}{{2{a_i}\prod\nolimits_{for\;all\;j! = i} {\left( {{a_j}^2 - a_i^2} \right)} }}} π10i=1n(ai2+x2)1dx=π1i=1naiforallj!=i(aj2ai2)2π0=i=1n2aiforallj!=i(aj2ai2)1


AC Code:

/*
 * Copyright (c) 2019 Ng Kimbing, HNU, All rights reserved. May not be used, modified, or copied without permission.
 * @Author: Ng Kimbing, HNU.
 * @LastModified:2019-07-18 T 16:09:56.104 +08:00
 */

package NowCoder;

import static ACMProblems.ACMIO.*;

public class Int {
    private static final int MOD = 1000000007;

    private static long powMod(long a, long n, long mod) {
        long ret = 1;
        long temp = a % mod;
        while (n != 0) {
            if ((n & 1) != 0)
                ret = ret * temp % mod;
            temp = temp * temp % mod;
            n >>= 1;
        }
        return ret;
    }

    private static long dist(long a, long b) {
        return (a * a % MOD - b * b % MOD + MOD) % MOD;
    }

    public static void main(String[] args) {
        setStream(System.in);
        int[] a = new int[100005];
        try {
            while (true) {
                int n = nextInt();
                for (int i = 0; i < n; ++i)
                    a[i] = nextInt();
                long ans = 0;
                long foo;
                for (int i = 0; i < n; ++i) {
                    foo = a[i];
                    for (int j = 0; j < n; ++j) {
                        if (i != j)
                            foo = (foo * dist(a[j], a[i])) % MOD;
                    }
                    ans = (ans + powMod(foo, MOD - 2, MOD)) % MOD;
                }
                out.println((ans * powMod(2, MOD - 2, MOD)) % MOD);
            }
        } catch (Exception ignored) {
        }
        out.flush();
    }
}

你可能感兴趣的:(ACM)