链接: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());
}
}
}