A. Integer Moves
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
There's a chip in the point (0,0)(0,0) of the coordinate plane. In one operation, you can move the chip from some point (x1,y1)(x1,y1) to some point (x2,y2)(x2,y2) if the Euclidean distance between these two points is an integer (i.e. (x1−x2)2+(y1−y2)2−−−−−−−−−−−−−−−−−−√(x1−x2)2+(y1−y2)2 is integer).
Your task is to determine the minimum number of operations required to move the chip from the point (0,0)(0,0) to the point (x,y)(x,y).
Input
The first line contains a single integer tt (1≤t≤30001≤t≤3000) — number of test cases.
The single line of each test case contains two integers xx and yy (0≤x,y≤500≤x,y≤50) — the coordinates of the destination point.
Output
For each test case, print one integer — the minimum number of operations required to move the chip from the point (0,0)(0,0) to the point (x,y)(x,y).
Example
input
Copy
3 8 6 0 0 9 15
output
Copy
1 0 2
Note
In the first example, one operation (0,0)→(8,6)(0,0)→(8,6) is enough. (0−8)2+(0−6)2−−−−−−−−−−−−−−−√=64+36−−−−−−√=100−−−√=10(0−8)2+(0−6)2=64+36=100=10 is an integer.
In the second example, the chip is already at the destination point.
In the third example, the chip can be moved as follows: (0,0)→(5,12)→(9,15)(0,0)→(5,12)→(9,15). (0−5)2+(0−12)2−−−−−−−−−−−−−−−−√=25+144−−−−−−−√=169−−−√=13(0−5)2+(0−12)2=25+144=169=13 and (5−9)2+(12−15)2−−−−−−−−−−−−−−−−−√=16+9−−−−−√=25−−√=5(5−9)2+(12−15)2=16+9=25=5 are integers.
import sys
import math
#sys.stdin = open("in.txt", "r")
T = int(input())
for _ in range(T):
n, l = [int(i) for i in input().split()]
if n==0 and l==0:
print(0)
continue
b=n*n+l*l
c=int(math.sqrt(n*n+l*l))
if c*c==b:print(1)
else: print(2)
B. XY Sequence
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
You are given four integers nn, BB, xx and yy. You should build a sequence a0,a1,a2,…,ana0,a1,a2,…,an where a0=0a0=0 and for each i≥1i≥1 you can choose:
- either ai=ai−1+xai=ai−1+x
- or ai=ai−1−yai=ai−1−y.
Your goal is to build such a sequence aa that ai≤Bai≤B for all ii and ∑i=0nai∑i=0nai is maximum possible.
Input
The first line contains a single integer tt (1≤t≤1041≤t≤104) — the number of test cases. Next tt cases follow.
The first and only line of each test case contains four integers nn, BB, xx and yy (1≤n≤2⋅1051≤n≤2⋅105; 1≤B,x,y≤1091≤B,x,y≤109).
It's guaranteed that the total sum of nn doesn't exceed 2⋅1052⋅105.
Output
For each test case, print one integer — the maximum possible ∑i=0nai∑i=0nai.
Example
input
Copy
3 5 100 1 30 7 1000000000 1000000000 1000000000 4 1 7 3
output
Copy
15 4000000000 -10
Note
In the first test case, the optimal sequence aa is [0,1,2,3,4,5][0,1,2,3,4,5].
In the second test case, the optimal sequence aa is [0,109,0,109,0,109,0,109][0,109,0,109,0,109,0,109].
In the third test case, the optimal sequence aa is [0,−3,−6,1,−2][0,−3,−6,1,−2].
import sys
import math
#sys.stdin = open("in.txt", "r")
T = int(input())
for _ in range(T):
n,B,x,y = [int(i) for i in input().split()]
sm=0
ans=0
for i in range(n):
if sm+x>B:
sm-=y
else:
sm+=x
ans+=sm
print(ans)