A: Integer Points
้ข็ฎ๏ผ
DLS and JLS are bored with a Math lesson. In order to entertain themselves, DLS took a sheet of paper and drew ๐ distinct lines, given by equations ๐ฆ=๐ฅ+๐๐ for some distinct ๐1,๐2,โฆ,๐๐.
Then JLS drew on the same paper sheet ๐ distinct lines given by equations ๐ฆ=โ๐ฅ+๐๐ for some distinct ๐1,๐2,โฆ,๐๐.
DLS and JLS are interested in counting how many line pairs have integer intersection points, i.e. points with both coordinates that are integers. Unfortunately, the lesson will end up soon, so DLS and JLS are asking for your help.
Input
The first line contains one integer ๐ก (1โค๐กโค1000), the number of test cases in the input. Then follow the test case descriptions.
The first line of a test case contains an integer ๐ (1โค๐โค10^5), the number of lines drawn by DLS.
The second line of a test case contains ๐ distinct integers ๐๐ (0โค๐๐โค10^9) describing the lines drawn by DLS. The integer ๐๐ describes a line given by the equation ๐ฆ=๐ฅ+๐๐.
The third line of a test case contains an integer ๐ (1โค๐โค10^5), the number of lines drawn by JLS.
The fourth line of a test case contains ๐ distinct integers ๐๐ (0โค๐๐โค10^9) describing the lines drawn by JLS. The integer ๐๐ describes a line given by the equation ๐ฆ=โ๐ฅ+๐๐.
The sum of the values of ๐ over all test cases in the input does not exceed 105. Similarly, the sum of the values of ๐ over all test cases in the input does not exceed 105.
In hacks it is allowed to use only one test case in the input, so ๐ก=1 should be satisfied.
Output
For each test case in the input print a single integer โ the number of line pairs with integer intersection points.
Example
input
1 | 3 |
output
1 | 3 |
Note
The picture shows the lines from the first test case of the example. Black circles denote intersection points with integer coordinates.
้ข่งฃ๏ผ
piไธqiๅไธบๅฅๆฐๆ่ ๅไธบๅถๆฐๆถๆไผๆๆดๆฐ็ไบค็นใ
ไปฃ็ ๏ผ
1 | #include<stdio.h> |
B Grow The Tree
้ข็ฎ๏ผ
Gardener Alexey teaches competitive programming to high school students. To congratulate Alexey on the Teacherโs Day, the students have gifted him a collection of wooden sticks, where every stick has an integer length. Now Alexey wants to grow a tree from them.
The tree looks like a polyline on the plane, consisting of all sticks. The polyline starts at the point (0,0). While constructing the polyline, Alexey will attach sticks to it one by one in arbitrary order. Each stick must be either vertical or horizontal (that is, parallel to ๐๐ or ๐๐ axis). It is not allowed for two consecutive sticks to be aligned simultaneously horizontally or simultaneously vertically. See the images below for clarification.
Alexey wants to make a polyline in such a way that its end is as far as possible from (0,0). Please help him to grow the tree this way.
Note that the polyline defining the form of the tree may have self-intersections and self-touches, but it can be proved that the optimal answer does not contain any self-intersections or self-touches.
Input
The first line contains an integer ๐ (1โค๐โค100000) โ the number of sticks Alexey got as a present.
The second line contains ๐ integers ๐1,โฆ,๐๐ (1โค๐๐โค10000) โ the lengths of the sticks.
Output
Print one integer โ the square of the largest possible distance from (0,0) to the tree end.
Examples
input
1 | 3 |
output
1 | 26 |
input
1 | 4 |
output
1 | 20 |
Note
The following pictures show optimal trees for example tests. The squared distance in the first example equals 5โ
5+1โ
1=26, and in the second example 4โ
4+2โ
2=20.
้ข่งฃ๏ผ
ๅฏนๆฐ็ปๆๅบ๏ผๅn/2็ๆปๅไธๅฉไฝ็ๆปๅๅณๆฏans
ไปฃ็ ๏ผ
1 | #include<stdio.h> |
D1. The World Is Just a Programming Task (Easy Version)
้ข็ฎ๏ผ
This is an easier version of the problem. In this version, ๐โค500.
Vasya is an experienced developer of programming competitionsโ problems. As all great minds at some time, Vasya faced a creative crisis. To improve the situation, Petya gifted him a string consisting of opening and closing brackets only. Petya believes, that the beauty of the bracket string is a number of its cyclical shifts, which form a correct bracket sequence.
To digress from his problems, Vasya decided to select two positions of the string (not necessarily distinct) and swap characters located at this positions with each other. Vasya will apply this operation exactly once. He is curious what is the maximum possible beauty he can achieve this way. Please help him.
We remind that bracket sequence ๐ is called correct if:
๐ is empty;
๐ is equal to โ(๐ก)โ, where ๐ก is correct bracket sequence;
๐ is equal to ๐ก1๐ก2, i.e. concatenation of ๐ก1 and ๐ก2, where ๐ก1 and ๐ก2 are correct bracket sequences.
For example, โ(()())โ, โ()โ are correct, while โ)(โ and โ())โ are not.
The cyclical shift of the string ๐ of length ๐ by ๐ (0โค๐<๐) is a string formed by a concatenation of the last ๐ symbols of the string ๐ with the first ๐โ๐ symbols of string ๐ . For example, the cyclical shift of string โ(())()โ by 2 equals โ()(())โ.
Cyclical shifts ๐ and ๐ are considered different, if ๐โ ๐.
Input
The first line contains an integer ๐ (1โค๐โค500), the length of the string.
The second line contains a string, consisting of exactly ๐ characters, where each of the characters is either โ(โ or โ)โ.
Output
The first line should contain a single integer โ the largest beauty of the string, which can be achieved by swapping some two characters.
The second line should contain integers ๐ and ๐ (1โค๐,๐โค๐) โ the indices of two characters, which should be swapped in order to maximize the stringโs beauty.
In case there are several possible swaps, print any of them.
Examples
input
1 | 10 |
output
1 | 5 |
input
1 | 12 |
output
1 | 4 |
input
1 | 6 |
output
1 | 0 |
Note
In the first example, we can swap 7-th and 8-th character, obtaining a string โ()()()()()โ. The cyclical shifts by 0,2,4,6,8 of this string form a correct bracket sequence.
In the second example, after swapping 5-th and 10-th character, we obtain a string โ)(())()()(()โ. The cyclical shifts by 11,7,5,3 of this string form a correct bracket sequence.
In the third example, swap of any two brackets results in 0 cyclical shifts being correct bracket sequences.
้ข่งฃ๏ผ
ๆดๅๆฑ่งฃ๏ผๅจไบคๆขijไนๅ๏ผ่ฎก็ฎๅ็ผๅ๏ผ่ฎก็ฎๆๅฐๅผ๏ผๆฑๅบๆๅฐๅผๅบ็ฐ็ๆฌกๆฐ๏ผๅณไธบๆฏ็งไบคๆขๅ๏ผๆๅพๅฐ็ๅผ๏ผๅๆๅคงๅผๅณๅฏใ
ไปฃ็ ๏ผ
1 | #include<stdio.h> |