其他分享
首页 > 其他分享> > 【牛客小白月赛51 F平均题】数论,前缀和

【牛客小白月赛51 F平均题】数论,前缀和

作者:互联网

import java.io.IOException;
import java.util.Scanner;

public class Main {
    static int MOD = 1000000007;

    public static void main(String[] args) throws IOException {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int[] arr = new int[n];
        for (int i = 0; i < n; i++) {
            arr[i] = scanner.nextInt();
        }
        //
        int k = n / 2;
        if( n%2 !=0){
            k++;
        }

        long[] sum = new long[k + 1];
        for (int i = 0; i < n; i++) {
            sum[0] += arr[i];
            sum[0] %= MOD;
        }
        for (int x = 1; x < k; x++) {
            sum[x] = (sum[x - 1] + 2 * MOD - arr[x - 1] - arr[n - x]) % MOD;
        }
        //求数组
        long[] sumsum = new long[k + 1];
        for (int i = 0; i < k; i++) {
            if (i > 0) {
                sumsum[i] = sumsum[i - 1] + sum[i];
            } else {
                sumsum[i] = sum[0];
            }
            sumsum[i] %= MOD;
        }
        //再求最简
        long ans = 0;
        for (int llen = 1; llen <= k; llen++) {
            long tmp = 1L;
            tmp = sumsum[llen - 1] * pow(llen, MOD - 2) % MOD;
            ans = (ans + tmp) % MOD;
            if (llen < k) {
                tmp = sumsum[llen - 1] * pow(n +1 - llen, MOD - 2) % MOD;
                ans = (ans + tmp) % MOD;
            } else {
                if (n % 2 == 1) {
                    continue;
                } else {
                    tmp = sumsum[llen - 1] * pow(n +1 -  llen, MOD - 2) % MOD;
                    ans = (ans + tmp) % MOD;
                }
            }
        }
        System.out.println(ans);


    }

    public static long pow(long b, int x) {
        if (x == 0) {
            return 1;
        }
        if (x == 1) {
            return b;
        }
        long a = pow(b, x / 2);
        if (x % 2 == 0) {
            return a * a % MOD;
        } else {
            return ((a * a % MOD) * b) % MOD;
        }
    }


}

 

标签:arr,int,sumsum,sum,51,++,long,牛客,小白月赛
来源: https://www.cnblogs.com/fishcanfly/p/16340741.html