A. Turtle Puzzle: Rearrange and Negate
You are given an array aaa of nnn integers. You must perform the following two operations on the array (the first, then the second):
- Arbitrarily rearrange the elements of the array or leave the order of its elements unchanged.
- Choose at most one contiguous segment of elements and replace the signs of all elements in this segment with their opposites. Formally, you can choose a pair of indices l,rl, rl,r such that 1≤l≤r≤n1 \le l \le r \le n1≤l≤r≤n and assign ai=−aia_i = -a_iai=−ai for all l≤i≤rl \le i \le rl≤i≤r (negate elements). Note that you may choose not to select a pair of indices and leave all the signs of the elements unchanged.
What is the maximum sum of the array elements after performing these two operations (the first, then the second)?
Input
The first line of the input contains a single integer ttt (1≤t≤10001 \le t \le 10001≤t≤1000) — the number of test cases. The descriptions of the test cases follow.
The first line of each test case contains a single integer nnn (1≤n≤501 \le n \le 501≤n≤50) — the number of elements in array aaa.
The second line of each test case contains nnn integers a1,a2,…,ana_1, a_2, \ldots, a_na1,a2,…,an (−100≤ai≤100-100 \le a_i \le 100−100≤ai≤100) — elements of the array.
Output
For each test case, output the maximum sum of the array elements after sequentially performing the two given operations.
Example
input
8
3
-2 3 -3
1
0
2
0 1
1
-99
4
10 -2 -3 7
5
-1 -2 -3 -4 -5
6
-41 22 -69 73 -15 -50
12
1 2 3 4 5 6 7 8 9 10 11 12
output
8
0
1
99
22
15
270
78
Tutorial
输出所有元素的绝对值之和,即 ∑i=0i=n∣ai∣\sum_{i = 0}^{i = n}\vert a_i \vert∑i=0i=n∣ai∣
Solution
for _ in range(int(input())):
input()
a = list(map(int, input().split()))
print(sum(abs(ai) for ai in a))
B. Turtle Math: Fast Three Task
You are given an array a1,a2,…,ana_1, a_2, \ldots, a_na1,a2,…,an.
In one move, you can perform either of the following two operations:
- Choose an element from the array and remove it from the array. As a result, the length of the array decreases by 111;
- Choose an element from the array and increase its value by 111.
You can perform any number of moves. If the current array becomes empty, then no more moves can be made.
Your task is to find the minimum number of moves required to make the sum of the elements of the array aaa divisible by 333. It is possible that you may need 000 moves.
Note that the sum of the elements of an empty array (an array of length 000) is equal to 000.
Input
The first line of the input contains a single integer ttt (1≤t≤1041 \le t \le 10^41≤t≤104) — the number of test cases.
The first line of each test case contains a single integer nnn (1≤n≤1051 \le n \le 10^51≤n≤105).
The second line of each test case contains nnn integers a1,a2,…,ana_1, a_2, \ldots, a_na1,a2,…,an (1≤ai≤1041 \le a_i \le 10^41≤ai≤104).
The sum of nnn over all test cases does not exceed 2⋅1052 \cdot 10^52⋅105.
Output
For each test case, output a single integer: the minimum number of moves.
Example
input
8
4
2 2 5 4
3
1 3 2
4
3 7 6 8
1
1
4
2 2 4 2
2
5 5
7
2 4 8 1 9 3 4
2
4 10
output
1
0
0
1
1
2
1
1
Note
In the first test case, initially the array a=[2,2,5,4]a = [2, 2, 5, 4]a=[2,2,5,4]. One of the optimal ways to make moves is:
- remove the current 444th element and get a=[2,2,5]a = [2, 2, 5]a=[2,2,5];
As a result, the sum of the elements of the array aaa will be divisible by 333 (indeed, a1+a2+a3=2+2+5=9a_1 + a_2 + a_3 = 2 + 2 + 5 = 9a1+a2+a3=2+2+5=9).
In the second test case, initially, the sum of the array is 1+3+2=61+3+2 = 61+3+2=6, which is divisible by 333. Therefore, no moves are required. Hence, the answer is 000.
In the fourth test case, initially, the sum of the array is 111, which is not divisible by 333. By removing its only element, you will get an empty array, so its sum is 000. Hence, the answer is 111.
Tutorial
- 如果对
3
取余等于0
,则需要0
次操作 - 如果对
3
取余等于2
,则需要1
次添加操作即可 - 如果对
3
取余等于1
:- 若数组中有一个元素对
3
取余等于1
,删除这个元素即可 - 否则做两次添加操作
- 若数组中有一个元素对
Solution
for _ in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
ans = (3 - sum(a) % 3) % 3
for ai in a:
if ai % 3 == 3 - ans:
ans = min(ans, 1)
print(ans)
C. Turtle Fingers: Count the Values of k
You are given three positive integers aaa, bbb and lll (a,b,l>0a,b,l>0a,b,l>0).
It can be shown that there always exists a way to choose non-negative (i.e. ≥0\ge 0≥0) integers kkk, xxx, and yyy such that l=k⋅ax⋅byl = k \cdot a^x \cdot b^yl=k⋅ax⋅by.
Your task is to find the number of distinct possible values of kkk across all such ways.
Input
The first line contains the integer ttt (1≤t≤1041 \le t \le 10^41≤t≤104) — the number of test cases.
The following ttt lines contain three integers, aaa, bbb and lll (2≤a,b≤1002 \le a, b \le 1002≤a,b≤100, 1≤l≤1061 \le l \le 10^61≤l≤106) — description of a test case.
Output
Output ttt lines, with the iii-th (1≤i≤t1 \le i \le t1≤i≤t) line containing an integer, the answer to the iii-th test case.
Example
input
11
2 5 20
2 5 21
4 6 48
2 3 72
3 5 75
2 2 1024
3 7 83349
100 100 1000000
7 3 2
2 6 6
17 3 632043
output
6
1
5
12
6
11
24
4
1
3
24
Note
In the first test case, a=2,b=5,l=20a=2, b=5, l=20a=2,b=5,l=20. The possible values of kkk (and corresponding x,yx,yx,y) are as follows:
- Choose k=1,x=2,y=1k = 1, x = 2, y = 1k=1,x=2,y=1. Then k⋅ax⋅by=1⋅22⋅51=20=lk \cdot a^x \cdot b^y = 1 \cdot 2^2 \cdot 5^1 = 20 = lk⋅ax⋅by=1⋅22⋅51=20=l.
- Choose k=2,x=1,y=1k = 2, x = 1, y = 1k=2,x=1,y=1. Then k⋅ax⋅by=2⋅21⋅51=20=lk \cdot a^x \cdot b^y = 2 \cdot 2^1 \cdot 5^1 = 20 = lk⋅ax⋅b