Problem F: help Johnny
Time Limit: 1 sec memory limit: 128 MB
Submit: 66 solved: 23
[Submit] [Status] [Web
Board]
Descriptionpoor Johnny is so busy this term. His Tutor threw lots of hard problems to him and demanded him to accomplish those problems in a month. What a wicked tutor! After cursing his tutor thousands of times, Johnny realized that he must start his work immediately. the very problem Johnny shoshould solve firstly is about a strange machine called warmouth. in the warmouth there are using pairs of bils. each pair consists of a red ball and a blue ball and each ball is assigned a value. we can represent a pair in the form of (R, B) in which R is the value of the RE D ball and B is of the blue one. warmouth has a generator to calculate the match value of two pairs. the match value of (R1, B1) and (R2, B2) is R1 * B2 + R2 * B1. initially, warmouth is empty. pairs are sent into warmouth in order. once a new pair comes, it will be taken into the generator with all the pairs already in warmouth. johnny's work is to tell his tutor the sum of all match values given the L Ist of pairs in order. As the best friend of Johnny, wowould you like to help him?
Inputthe first line of the input is T (no more than 10), which stands for the number of lists Johnny initialized ed. each list begins with "N" (without quotes ). n is the number of pairs of this list and is no more than 100000. the next line gives n pairs in chronological order. the 2i-th number is the value of the red ball of the I-th pair and the (2I + 1)-th number is the value of the blue ball of the I-th pair. the numbers are positive integers and smaller than 100000.
Output
Please output the result in a single line for each list.
Sample input2
3
1 3 2 2 3 1
2
4 5 6 7
Sample output26
58
This question is quite simple. The question is to calculate the total value: (the calculation method of the value between two points) the match value of (R1, B1) and (R2, B2) is R1 * B2 + R2 * B1. this problem will time out if it is directly simulated (two values are calculated and added up... I solve it like this: Take A1 (1 2), A2 (2 2), and A3 (3 1) in the example for the following three points: first, use the formula to calculate the value of a1a2. (key !) Then let's talk about how to add the A1 value to A2. Then A2 becomes (3 4), and then we can use the formula to set a2a3. (reason: Multiplication is the allocation law of addition) in this way, only O (n) is provided for reference: # include <stdio. h>
# Include <string. h>
Struct PP
{
Long long X, Y;
} Z [101000];
Int main ()
{
Int t, n;
Long long sum;
Scanf ("% d", & T );
While (t --)
{
Scanf ("% d", & N );
Memset (z, 0, sizeof (z ));
For (INT I = 1; I <= N; I ++)
Scanf ("% LLD", & Z [I]. X, & Z [I]. y );
Sum = 0;
For (INT I = 1; I <n; I ++)
{
Sum + = Z [I]. x * Z [I + 1]. Y + Z [I]. y * Z [I + 1]. X;
Z [I + 1]. x + = Z [I]. X;
Z [I + 1]. Y + = Z [I]. Y;
}
Printf ("% LLD \ n", sum );
}
Return 0;
} Decline copy.