HOJ 3138 Discover's problem

http://acm.hit.edu.cn/hoj/problem/view?id=3138

HOJ 3138 Discover's problem

My Tags 数学   (Edit)
  Source : HCPC FALL 2012
  Time limit : 2 sec   Memory limit : 64 M

Submitted : 38, Accepted : 13

We define X property of two array A[n] and B[n] as:
Write a program to calculate the X property of two array.

Input

First line give a number T . Then T cases follows. Each case occupy three lines. The first line is a number n which is the length of arrays. The second and third line each consisting of n positive integers indicate array A[n] and B[n].

It’s guaranteed that all the integer in the input are not larger than 1000.

Output

Print the X property in a line for each input case.

Sample Input

3
1
2
2
2
1 2
5 6
3
1 2 3
4 5 6

Sample Output

0
16
54

 

 

 

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

typedef long long ll;

const int X = 1005;

ll a[X],b[X];
ll ans;
int n;

int main(){
	freopen("ain.txt","r",stdin);
	int ncase;
	cin>>ncase;
	while(ncase--){
		cin >> n;
		ans = 0;
		ll x = 0,y = 0;
		for(int i=1;i<=n;i++){
			scanf("%lld",&a[i]);
			x += a[i]*a[i];
		}
		for(int i=1;i<=n;i++){
			scanf("%lld",&b[i]);
			y += b[i]*b[i];
			ans -= a[i]*b[i];
		}
		cout<<-ans*ans+x*y<<endl;
	}
	return 0;
}

  

posted @ 2013-01-16 16:24  yejinru  阅读(186)  评论(0编辑  收藏  举报