华南理工大学“三七互娱杯”程序设计竞赛(重现赛)D HRY and array

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

题目描述

Given two arrays A,B with length n, perform the following operations in order:
1. Randomly shuffle array A
2. Randomly shuffle array B
3. Calculate S=∑ni=1AiBi.S=∑i=1nAiBi.
It is easy to know that there n!×n!n!×n! possible combinations in total. Please calculate the expectation of S.

输入描述:

The first line contains an integer T, indicating the number of test case.
For each test case there are three lines :
The first line is a positive integer n, indicating the length of array A, B.
The second line contains n non-negative integers, indicating array A.
The third line contains n non-negative integers, indicating array B.
1≤T≤100,1≤n≤105,0≤Ai,Bi≤100001≤T≤100,1≤n≤105,0≤Ai,Bi≤10000.
It is guaranteed that the sum of n does not exceed 106106.

输出描述:

For each test case output a line of a real number, indicating the expectation of S, rounded to 30 decimal places.

示例1

输入

复制

2
3
1 1 1
1 1 1
2
1 2
3 4

输出

复制

3.000000000000000000000000000000
10.500000000000000000000000000000
import java.math.*;
import java.util.*;
import java.io.*;
class InputReader {
      
    private final InputStream stream;
    private final byte[] buf = new byte[8192];
    private int curChar, snumChars;
  
    public InputReader(InputStream st) {
        this.stream = st;
    }
  
    public int read() {
        if (snumChars == -1)
            throw new InputMismatchException();
        if (curChar >= snumChars) {
            curChar = 0;
            try {
                snumChars = stream.read(buf);
            } catch (IOException e) {
                throw new InputMismatchException();
            }
            if (snumChars <= 0)
                return -1;
        }
        return buf[curChar++];
    }
  
    public int nextInt() {
        int c = read();
        while (isSpaceChar(c)) {
            c = read();
        }
        int sgn = 1;
        if (c == '-') {
            sgn = -1;
            c = read();
        }
        int res = 0;
        do {
            res *= 10;
            res += c - '0';
            c = read();
        } while (!isSpaceChar(c));
        return res * sgn;
    }
  
    public long nextLong() {
        int c = read();
        while (isSpaceChar(c)) {
            c = read();
        }
        int sgn = 1;
        if (c == '-') {
            sgn = -1;
            c = read();
        }
        long res = 0;
        do {
            res *= 10;
            res += c - '0';
            c = read();
        } while (!isSpaceChar(c));
        return res * sgn;
    }
  
    public int[] nextIntArray(int n) {
        int a[] = new int[n];
        for (int i = 0; i < n; i++) {
            a[i] = nextInt();
        }
        return a;
    }
  
    public String readString() {
        int c = read();
        while (isSpaceChar(c)) {
            c = read();
        }
        StringBuilder res = new StringBuilder();
        do {
            res.appendCodePoint(c);
            c = read();
        } while (!isSpaceChar(c));
        return res.toString();
    }
  
    public String nextLine() {
        int c = read();
        while (isSpaceChar(c))
            c = read();
        StringBuilder res = new StringBuilder();
        do {
            res.appendCodePoint(c);
            c = read();
        } while (!isEndOfLine(c));
        return res.toString();
    }
  
    public boolean isSpaceChar(int c) {
        return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
    }
  
    private boolean isEndOfLine(int c) {
        return c == '\n' || c == '\r' || c == -1;
    }
  
}
public class Main
{
    public static void main(String[]args)throws IOException
    {
        InputReader in=new InputReader(System.in);
        //Scanner sc=new Scanner(System.in);
        int t =in.nextInt();
        while(t-->0)
        {
            int n=in.nextInt();
            long a[]=new long [n];
            long b[]=new long [n];
            long  sum_a=0;long sum_b=0;
            for(int i=0;i<n;i++) {a[i]=in.nextLong(); sum_a+=a[i];}
            for(int i=0;i<n;i++) {b[i]=in.nextInt(); sum_b+=b[i];}
            BigDecimal suma=new BigDecimal(sum_a);
            BigDecimal sumb=new BigDecimal(sum_b);
            BigDecimal nn=new BigDecimal(n);
            System.out.println(suma.multiply(sumb).divide(nn,30,BigDecimal.ROUND_HALF_UP).toPlainString());
        }
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值