A - Balloons


  • محدودیت زمان: ۱ ثانیه
  • محدودیت حافظه: ۲۵۶ مگابایت

Several balloons are arranged sequentially, each showing one of the letters from a to z. Amin wants to give a needle to Niloufar and ask her to select one or more balloons so that after popping them and joining the remaining balloons (without changing their order), he can reach a state where exactly 44 balloons remain in sequence, with the letters acpc written on them in order.

توضیح تصویر

You need to write a program that, given the initial arrangement of balloons, determines whether this is possible or not.

ورودی🔗

The first line of input contains a positive integer tt, representing the number of test cases.

1t1041 \leq t \leq 10^4

In each test case, there is a single string ss, consisting of lowercase English letters, showing the sequence of letters written on the balloons.

1s1001 \leq |s| \leq 100

خروجی🔗

For each test case, print YES if it is possible to form the word acpc; otherwise, print NO.

مثال‌ها🔗

ورودی نمونه ۱🔗

2
amirkabirprogrammingcontest
amirkabircollegiateprogramcontest
Plain text

خروجی نمونه ۱🔗

NO
YES
Plain text

B - Memorial


  • محدودیت زمان: ۱ ثانیه
  • محدودیت حافظه: ۲۵۶ مگابایت

We want to engrave the names of several distinguished professors on the wall of Amirkabir University. The name of each professor will be written on a tile 1×11 \times 1. We intend to select a rectangle of size a×ba \times b on the wall with a.ba.b tiles and inscribe the professors' names on them.

The university will approve this request if the following conditions are met:

  1. First, the names of nn faculty members of the Computer Engineering Department must be included in this tribute.
  2. Second, the perimeter of this rectangle, that is, 2a+2b2a + 2b, must be minimized.
  3. Third, the values of aa and bb must be prime numbers.

Given a natural number nn, you are asked to calculate the minimum possible perimeter for this tribute.

ورودی🔗

The first line of input contains a positive integer tt, representing the number of test cases.

1t1001 \leq t \leq 100

In the next tt lines, each line contains an integer nn, representing the number of professors for that test case.

1n1091 \leq n \leq 10^9

خروجی🔗

For each of the tt test cases, print a natural number representing the minimum required perimeter.

مثال‌ها🔗

ورودی نمونه ۱🔗

3
1
17
10
Plain text

خروجی نمونه ۱🔗

8
20
14
Plain text

C - Rock-Paper-Scissors


  • محدودیت زمان: ۱ ثانیه
  • محدودیت حافظه: ۲۵۶ مگابایت

Amin and Niloufar are playing Rock-Paper-Scissors together. They are not kids, so instead of playing with their hands, they use actual rocks, papers, and scissors.

Amin has RaR_a rocks, PaP_a papers, and SaS_a scissors. Similarly, Niloufar has RnR_n rocks, PnP_n papers, and SnS_n scissors.

توضیح تصویر

In each round of the game, both players use one of their rocks, papers, or scissors (if a player has no items left, they'll lose that round). According to the traditional rules of this game, paper beats rock, scissors beat paper, and rock beats scissors. If both items are the same, the round is a draw.

Each win earns 33 points, draw earns 11 point, and lose earns no points. Your task is to write a program to calculate the maximum score that Amin can achieve among all possible outcomes.

ورودی🔗

The first line of input contains an integer tt, representing the number of test cases.

1t1051 \leq t \leq 10^5

In the first line of each test case, three integers RaR_a, PaP_a, and SaS_a are given. In the second line of each test case, three integers RnR_n, PnP_n, and SnS_n are given.

0Ra,Pa,Sa,Rn,Pn,Sn1090 \leq R_a, P_a, S_a, R_n, P_n, S_n \leq 10^9

خروجی🔗

For each test case, print a single integer representing the maximum score Amin can achieve.

مثال‌ها🔗

ورودی نمونه ۱🔗

3
1 1 1
1 1 1
3 0 2
0 3 1
8 0 0
8 0 0
Plain text

خروجی نمونه ۱🔗

9
12
8
Plain text

D - Array's Value


  • محدودیت زمان: ۱ ثانیه
  • محدودیت حافظه: ۲۵۶ مگابایت

The symbol \oplus represents the bitwise XOR operator for integers. For example: 93=10012112=10102=109 \oplus 3 = 1001_2 \oplus 11_2 = 1010_2 = 10

The \textit{value} of an array such as a1,a2,,ana_1, a_2, \dots, a_n is defined as: a1a2ana_1 \oplus a_2 \oplus \dots \oplus a_n

You are given two arrays of numbers, a1,a2,,ana_1, a_2, \dots, a_n and b1,b2,,bnb_1, b_2, \dots, b_n.

For each ii from 11 to nn, you can swap the values of aia_i and bib_i. Write a program to perform these swaps in such a way that the sum of the values of both arrays is maximized.

ورودی🔗

The first line of input contains a positive integer nn, representing the length of the arrays.

1n1051 \leq n \leq 10^5

The second and third lines each contain nn positive integers separated by space, with the second line representing the elements of array aa and the third line representing the elements of array bb.

0ai,bi10180 \leq a_i, b_i \leq 10^{18}

خروجی🔗

Print a single integer, the maximum possible value for the sum of the values of both arrays.

مثال‌ها🔗

ورودی نمونه ۱🔗

3
1 2 3
3 1 2
Plain text

خروجی نمونه ۱🔗

6
Plain text

E - Easy Computations


  • محدودیت زمان: ۱ ثانیه
  • محدودیت حافظه: ۲۵۶ مگابایت

Board

Amin has given an array of nn distinct numbers, a1,a2,,ana_1, a_2, \dots, a_n to Niloufar. She came and calculated the value max(ai,aj)×aiaj\max(a_i, a_j) \times |a_i - a_j| for each two-element subset {ai,aj}\{ a_i, a_j \} and wrote it on the board. Now Amin wants to calculate the sum of the numbers written on the board. Help him perform this calculation.

ورودی🔗

The first line of input contains a positive integer tt, representing the number of test cases. 1t1001 \leq t \leq 100

In the first line of each test case, there is an integer nn indicating the number of elements in the array. 2n5×1042 \leq n \leq 5 \times 10^4

The second line of each test case contains nn positive integers, representing the elements of the array. 1ai5×1041 \leq a_i \leq 5 \times 10^4

Note that the input array is not necessarily sorted.

خروجی🔗

For each test case, print a single integer representing the sum of the numbers written on the board.

مثال‌ها🔗

ورودی نمونه ۱🔗

3
2
1403 9
4
2 1 4 3
7
10 20 3 15 1000 60 16
Plain text

خروجی نمونه ۱🔗

1955782
35
5891525
Plain text

F - Shortest Path


  • محدودیت زمان: ۳ ثانیه
  • محدودیت حافظه: ۲۵۶ مگابایت

Amin and Niloufar need to travel from point (0,0)(0, 0) to (3n,3n)(3n, 3n). In normal terrain, they can travel 11 km per day. Their path is interrupted by a wide swamp that runs parallel to the xx-axis. The swamp is nn kilometers wide, with boundaries at y=ny = n and y=2ny = 2n. The map of the area is shown in the diagram below:

River

The travel speed of Amin and Niloufar through the swamp is 1k\frac{1}{k} kilometers per day. Find the shortest possible time to travel from point (0,0)(0, 0) to (3n,3n)(3n, 3n) and print the result in days.

If the shortest travel time is dd days hh hours mm minutes, and so on, print only the value dd.

ورودی🔗

The first line of input contains a positive integer tt, representing the number of test cases.

1t1051 \leq t \leq 10^5

For each test case, the only line contains two integers nn and kk.

1n,k1071 \leq n, k \leq 10^7

خروجی🔗

For each test case, print a single integer representing the minimum number of days required to reach the destination.

مثال‌ها🔗

ورودی نمونه ۱🔗

2
1 1
100 2
Plain text

خروجی نمونه ۱🔗

4
543
Plain text

G - Birth Date


  • محدودیت زمان: ۱ ثانیه
  • محدودیت حافظه: ۲۵۶ مگابایت

Niloufar wants to give Amin a gift after counting his birth date in calendar from begining. Help Niloufar to solve this problem.

Date

You are given two integers mm and nn and finds the nn-th smallest natural number that contains the number mm as a subsequence.

We know that the number mm consists of distinct digits.

ورودی🔗

The first line of input contains a positive integer tt, representing the number of test cases.

1t1001 \leq t \leq 100

In the first line of each test case, two positive integer mm and nn is given in order.

1m50001 \leq m \leq 5000 1n10121 \leq n \leq 10^{12}

خروجی🔗

For each test case, print a single integer representing the nn-th smallest number that contains mm as a subsequence.

مثال‌ها🔗

ورودی نمونه ۱🔗

3
21 14
1403 1
2189 100
Plain text

خروجی نمونه ۱🔗

221
1403
201689
Plain text

H - Country's Value


  • محدودیت زمان: ۵ ثانیه
  • محدودیت حافظه: ۵۱۲ مگابایت

We have a country with nn cities. The cities in this country are connected by n1n - 1 two-way roads. We know that from each city, we can reach any other city by traversing some roads, meaning the structure of this country is a tree.

The cities are numbered from 11 to nn. We know the population of city ii is wiw_i. The \textit{difference} between two cities is defined as the XOR of the populations of the cities along the path between the two cities (including the start and end). The \textit{value} of the country is the sum of the differences between every pair of distinct cities.

Calculating the initial value of the country is not a hard task for the mayor. Therefore, we ask you to design a dynamic system. You will receive qq queries.

In each query, the population of city vv changes to xx. We want you to write a program that calculates the initial value of the country and the value of the country after each change.

ورودی🔗

The first line of input contains an integer nn, representing the number of cities in the country.

1n1051 \leq n\leq 10^5

The second line contains nn integers w1,w2,,wnw_1, w_2, \dots, w_n, representing the initial population of each city.

0wi1080 \leq w_i \leq 10^8

Each of the next n1n - 1 lines contain two integers uu and vv, representing a road between cities uu and vv in the country.

1u,vn1 \leq u, v\leq n

The following line contains an integer qq, representing the number of queries.

1q1051 \leq q \leq 10^5

The next qq lines each contain two integers vv and xx, representing an operation that changes the population wvw_v of city vv to xx. It is guarantied that the roads form a tree.

0x1080 \leq x \leq 10^8

1vn1 \leq v\leq n

خروجی🔗

The output consists of q+1q + 1 lines, where each line contains an integer representing the current value of the country.

مثال‌ها🔗

ورودی نمونه ۱🔗

5
10 11 8 3 17
1 2
1 3
2 4
2 5
3
4 16
1 5
5 5
Plain text

خروجی نمونه ۱🔗

123
157
202
170
Plain text

I - Hospital Lines


  • محدودیت زمان: ۱ ثانیه
  • محدودیت حافظه: ۲۵۶ مگابایت

In hospitals, lines are often drawn on the floor in various directions to help guide visitors to different areas. One day, while Amin was waiting outside the operating room, he started thinking about this:

Suppose there are nn departments in a hospital connected by corridors, forming a layout that can be represented as an undirected, connected graph with nn vertices and mm edges.

Amin wants to color each edge in this graph with one of two colors, red or blue, so that for any two departments (vertices), one can follow a continuous path of edges of same color to travel between them.

This means that if someone wants to travel from department vv to department uu, first we could guide them “follow red” or “follow blue”.

The question Amin faces is: can we color edges of the graph in such a way that any two vertices can be reached using a guide like "follow red/blue"?

ورودی🔗

The first line of input contains an integer tt representing the number of test cases.

1t1051 \leq t \leq 10^5

For each test case, the first line contains two positive integers nn and mm, representing the number of vertices and edges.

2n1052 \leq n \leq 10^5 1m1051 \leq m \leq 10^5 m106 \sum m \leq 10^6

In the next mm lines, each line contains two integers uu and vv, indicating the existence of an edge uvuv in the graph.

1u,vn1 \leq u, v \leq n

It is guaranteed that the given graphs are simple and connected.

خروجی🔗

If there is such coloring print YES; otherwise print NO.

مثال‌ها🔗

ورودی نمونه ۱🔗

3
4 3
1 2
1 3
1 4
2 1
1 2
3 3
1 2
2 3
1 3
Plain text

خروجی نمونه ۱🔗

NO
YES
NO
Plain text

J - Cave's Walls


  • محدودیت زمان: ۱ ثانیه
  • محدودیت حافظه: ۲۵۶ مگابایت

There are many caves near Tehran, and Amin, after finishing this competition, wants to pick one of them to visit and leave competitions behind. The entrance to this cave looks like the following:

توضیح تصویر

The cave entrance is shaped as an nn-sided polygon in two dimensions. The entrance consists of two walls, left and right, each forming a concave curve.

The tops of both walls meet at a single point, while the bottoms connect with an straight line.

Amin wants to place a sensor on each vertex of this polygon. Each sensor can detect surroundings unless a wall blocks its view.

The entrance of the cave, as shown in an image, consists of two concave walls and a flat floor at the entrance.

There are nn sensors installed along the cave entrance, numbered from 11 to nn, starting from the bottom left and going to the bottom right.

Two sensors ii and jj can "see" each other if every point along the line between them lies within the cave space or along the boundary of the walls.

Based on this, we can construct an n×nn \times n table, where we write 1 in row ii, column jj if sensor ii can see sensor jj, and 0 otherwise.

Now, the problem reverses this process. Given an n×nn \times n table of 0s and 1s, determine if it's possible to find an arrangement of sensors along the cave entrance that matches the visibility pattern shown in the table.

ورودی🔗

The first line of input contains a positive integer tt, representing the number of tables.

1t1051 \leq t \leq 10^5

For each test case, The first line contains an integer nn, representing the number of sensors.

3n1003 \leq n \leq 100

The next nn lines each contain a string of length nn, consisting of 0s and 1s, representing the table values.

n105\sum n \leq 10^5

خروجی🔗

For each test case, print YES if such an arrangement of sensors is possible, and NO otherwise.

مثال‌ها🔗

ورودی نمونه ۱🔗

5
5
01011
10111
01010
11101
11010
4
0111
1011
1101
1110
3
000
000
000
4
0101
0101
1010
1010
3
111
101
110
Plain text

خروجی نمونه ۱🔗

YES
NO
NO
NO
NO
Plain text

K - Time Complexity


  • محدودیت زمان: ۱ ثانیه
  • محدودیت حافظه: ۲۵۶ مگابایت

Consider the following algorithm:

function gcd(a, b):
    if b is 0:
        return a
    return gcd(b, a % b)
Plain text

We want to create a test to evaluate the performance of this program. For each test, you need to provide two integers aa and bb such that 1a,bn1 \le a, b \le n and this algorithm has the maximum number of gcd function calls.

ورودی🔗

The first line contains an integer tt, representing the number of test cases.

1t1051 \leq t \leq 10^5

In each test case, there is a single integer nn.

1n10181 \leq n \leq 10^{18}

خروجی🔗

For each test case, print the maximum number of function calls.

مثال‌ها🔗

ورودی نمونه ۱🔗

5
1
2
3
4
5
Plain text

خروجی نمونه ۱🔗

2
3
4
4
5
Plain text

L - Chemistry Class


  • محدودیت زمان: ۱ ثانیه
  • محدودیت حافظه: ۲۵۶ مگابایت

Chemistry

Amin hates his chemistry class. Niloufar, who has not finished her chemistry assignments yet, turns to Amin for help with balancing chemical reactions.

Balancing a reaction means adding coefficients in front of the reactants and products so that the number of each element on both sides of the reaction is equal.

Amin has no idea what chemistry is, but he knows that chemical element names start with an uppercase letter, followed by lowercase letters.

Help Amin to check whether the reaction is balanced or not. Note that the coefficients must be integers and should be in their simplest form.

ورودی🔗

The first line of input contains a positive integer tt, representing the number of chemical reactions.

1t1001 \leq t \leq 100

In the next tt lines, each line contains a chemical reaction.

1s1001 \leq |s| \leq 100

It is guaranteed that the coefficients are less than 100100.

خروجی🔗

For each test, if the reaction is balanced, print YES, otherwise print NO

مثال‌ها🔗

ورودی نمونه ۱🔗

6
3H_2 + N_2 => 2NH_3
6CO_2 + 6H_2O => C_6H_{12}O_6 + 6O_2
3Ca(OH)_2 + 2H_3PO_4 => Ca_3(PO_4)_2 + 6H_2O
4H_2 + N_2 => 2NH_3
3CO_2 + 3H_2O => C_6H_{12}O_6 + O_2
Ca(OH)_2 + 2H_3PO_4 => Ca_3(PO_4)_2 + 2H_2O
Plain text

خروجی نمونه ۱🔗

YES
YES
YES
NO
NO
NO
Plain text