列表

详情


NC25397. HRY and array

描述

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
It is easy to know that there 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.
.
It is guaranteed that the sum of n does not exceed .

输出描述

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

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

Java(javac 1.8) 解法, 执行用时: 366ms, 内存消耗: 14960K, 提交时间: 2019-04-30 16:20:23

import java.io.*;
import java.math.*;
public class Main{
	static int t,n,a,b;
	static StreamTokenizer in=new StreamTokenizer(
		new BufferedReader(new InputStreamReader(System.in)));
	public static void main(String[] args)throws IOException{
		in.nextToken();t=(int)in.nval;
		while(t-->0){
			in.nextToken();n=(int)in.nval;
			a=b=0;
			for(int i=0;i<n;i++){
				in.nextToken();
				a+=(int)in.nval;
			}
			for(int i=0;i<n;i++){
				in.nextToken();
				b+=(int)in.nval;
			}
			System.out.println(BigDecimal.valueOf(a).multiply(BigDecimal.valueOf(b))
				.divide(BigDecimal.valueOf(n),30,BigDecimal.ROUND_HALF_UP).toPlainString());
		}
	}
}

C++11(clang++ 3.9) 解法, 执行用时: 756ms, 内存消耗: 988K, 提交时间: 2019-05-13 09:20:46

#include<bits/stdc++.h>

using namespace std;

int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		long long sa=0,sb=0,n,anss[35],s,i,x;
		cin>>n;
		for(i=0;i<n;i++)
		{
			cin>>x;
			sa+=x;
		}
		for(i=0;i<n;i++)
		{
			cin>>x;
			sb+=x;
		}
		s=sa*sb;
		anss[0]=s/n;
		for(i=1;i<=31;i++)
		{
			s=s%n*10;
			anss[i]=s/n;
		}
		if(anss[31]>=5)
		anss[30]++;
		i=30;
		while(i>0)
		{
			if(anss[i]==10)
			{
				anss[i]=0;
				i--;
				anss[i]++;
			}
			else
			break;
		}
		printf("%lld.",anss[0]);
		for(i=1;i<=30;i++)
		printf("%d",anss[i]);
		printf("\n");
	}
	return 0;
}

C++14(g++5.4) 解法, 执行用时: 223ms, 内存消耗: 612K, 提交时间: 2019-05-01 21:33:07

#include<bits/stdc++.h>
int a[31];
typedef long long ll;
void solve(ll s,int n){
	printf("%lld.",s/n);
	s%=n;
	for(int i=0;i<=30;i++){
		s*=10;
		a[i]=s/n;
		s%=n;
	}
	if(a[30]>4)
		a[29]++;
	for(int i=29;i>=0;i--){
		if(a[i]==10){
			a[i]=0;
			a[i-1]++;
		}
	}
	for(int i=0;i<30;i++)
		printf("%d",a[i]);
	printf("\n");
}
int main(){
	int T;
	scanf("%d",&T);
	while(T--){
		int n;
		int x;
		scanf("%d",&n);
		ll a=0,b=0;
		for(int i=0;i<n;i++)
			scanf("%d",&x),a+=x;
		for(int i=0;i<n;i++)
			scanf("%d",&x),b+=x;
		solve(a*b,n);
	}
}

Python3(3.5.2) 解法, 执行用时: 614ms, 内存消耗: 21780K, 提交时间: 2019-04-30 15:35:25

from decimal import *
T=int(input())
for case in range(T):
        n=int(input())
        getcontext().prec=99
        a=Decimal(sum(map(int,input().split())))
        b=Decimal(sum(map(int,input().split())))
        ans=a*b/n
        print(format(ans,".30f"))
    

上一题