A – Cosmic Twins


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

Have you ever heard the phrase “Cosmic Twins”? The people who are born on the same day of the year, are called cosmic twins. he problem-setter thinks that the probability of existence of cosmic twins in group of PP people is relatively high, based on the number of cosmic twins found in SBU. Your task is to calculate this probability and verify his conjecture.

ورودی🔗

The input contains two space separated integers P,DP, D – the number of people and the number of days in a year respectively.

1P,D4001 \leq P, D \leq 400

خروجی🔗

Print a number in a line indicating the probability of existence of at least one pair of cosmic twins in the group.

The result should be printed with exactly 6 digits after decimal point.

مثال‌ها🔗

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

2 2
Plain text

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

0.500000
Plain text

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

22 365
Plain text

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

0.475695
Plain text

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

23 365
Plain text

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

0.507297
Plain text

B – Yet Another Paper


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

Farshid loves to write scientific papers, even if there is nothing to write paper about. In one of his latest papers he introduced a new sorting algorithm called FS-Sort. In this sort only one operation is available and that is you can swap two adjacent numbers. If you think for a while, you will see that it is always possible to sort any array of numbers in this way. An array of integers will be given. Now using the above approach he wants to sort the numbers in the ascending order. You have to help him find out the minimum number of operations required. For example, to sort ‘1 2 3’ we need no operation. However sorting ‘2 3 1’ requires at least 2 operations.

ورودی🔗

The first line of the input contains a positive integer NN. In next line there will be NN space separated integers denoting the elements of the array.

1N20001 \leq N \leq 2000

خروجی🔗

Print the minimum swap operations required to perform sorting.

مثال‌ها🔗

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

3
1 2 3
Plain text

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

0
Plain text

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

3
2 3 1
Plain text

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

2
Plain text
  • First you should swap 1 with 3.
  • Then you should swap 1 with 2.

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

3
2 2 2
Plain text

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

0
Plain text

C – Jack-Jack and Matrices


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

Do you know Jack-Jack? He is the youngest child of Parr family. The Parr family consists of Bob, Helen, Violet, Dashiell, and Jack-Jack. They are all characters of the “The Incredibles Movie”. Jack-Jack has many superpowers. Some of them are still not discovered yet. Recently, Helen saw Jack-Jack playing with matrices.

Jack-Jack thinks each cell of a matrix is powerful if it has three neighbors, which sum of their values is a power of two. Two cells are neighbors if and only if they share a side or corner. Jack-Jack is superhero, he is not a programmer. Can you count the number of powerful cells of the matrix for him?

ورودی🔗

The first line of input contains two space separated integers NN, MM – the number of rows and the number of columns of the matrix.

1N,M10001 \leq N, M \leq 1000

In each of the next NN lines, there are MM space-separated integers each representing a cell of the matrix. The value each cell is a non-negative integer that does not exceed 10610^6.

خروجی🔗

For each test, print a single integer, the number of powerful cells in the matrix.

مثال‌ها🔗

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

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

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

3
Plain text

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

1 1
3
Plain text

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

0
Plain text

D – Champions League


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

UEFA Champions League is the most prestigious annual sport competition in the world. In this competition, European clubs are divided into 8 groups of 4 teams.

In group stage, teams are ranked according to some specific rules. For example Team A will be ranked above Team B if:

  • Team A has more points than Team B.
  • If their points are equal, Team A has higher goal difference than Team B.
  • In case of tie in all previous cases, Team A has scored more goals than Team B.
  • In case of tie in all previous cases, Team A has scored more Away Scored Goals than Team B.

It is guaranteed that there won’t be a tie after these rules.

Your task is to write a program to determine the group leaders (top 2 teams of each group) based on their results.

ورودی🔗

The input is describing 12 games which are played between teams of a group. Each line describes a game and its result. The length of each line doesn’t exceed 25.

The format of each line is: HomeTeamName HomeTeamGoals vs. AwayTeamGoals AwayTeamName

Teams’ names consist of English letters and space character.

The team with more scored goals wins the game and receives 3 points. In case of draw, both teams receive 1 point.

  • GoalDifference=ScoredGoalsRecievedGoalsGoalDifference = ScoredGoals - RecievedGoals
  • Away Goals = Number of goals scored (being away team)

خروجی🔗

Print the names of first team and the second team in the appropriate order. They must be printed in separate lines.

مثال‌ها🔗

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

Man Utd 8 vs. 2 Arsenal
Lyon 1 vs. 2 Man Utd
FC Barca 0 vs. 0 Lyon
FC Barca 5 vs. 1 Arsenal
Man Utd 3 vs. 1 FC Barca
Arsenal 6 vs. 0 Lyon
Arsenal 0 vs. 0 Man Utd
Man Utd 4 vs. 2 Lyon
Arsenal 2 vs. 2 FC Barca
Lyon 0 vs. 3 FC Barca
Lyon 1 vs. 0 Arsenal
FC Barca 0 vs. 1 Man Utd
Plain text

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

Man Utd
FC Barca
Plain text

E – Tic-Tac-Toe


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

Every contest needs a judge. You are assigned to judge the Tic-Tac-Toe (aka XO) contest in the SBU. Tic-Tac-Toe judges usually have two major responsibilities. Their responsibilities consist of verifying movements’ correction and declaring the result when the game is over.

In this problem you are going to determine the state of a Tic-Tac-Toe game. Player ‘X’ always plays first and the players play in turns.

ورودی🔗

The input consists of three lines. Each line describes a row by 3 characters. The characters can be X, O, and ?. Character ? denotes an empty cell.

خروجی🔗

  • Print X if player X has won the game in the given input.
  • Print O if player O has won the game in the given input.
  • Print Unfinished if the game is not finished yet.
  • Print Draw, if the game is finished and none of them won the game.
  • Print Invalid if the description is not valid.

مثال‌ها🔗

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

XOX
OXO
??X
Plain text

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

X
Plain text

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

O??
???
???
Plain text

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

Invalid
Plain text

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

OOO
??X
XX?
Plain text

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

O
Plain text

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

???
???
???
Plain text

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

Unfinished
Plain text

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

OXO
XXO
XOX
Plain text

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

Draw
Plain text

F – Circles


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

There are NN circles which their centers lie on the Ox axis. Given the coordinates of their centers and their radius, find the number of regions they create on the plane.

ورودی🔗

The input starts with a line containing a single integer NN – the number of circles. Each of the following NN lines

1N20001 \leq N \leq 2000

consist of two space separated integers cic_i and rir_i which denote the ii’th circle’s xx-coordinate and its radius

106ci106-10^6 \leq c_i \leq 10^6 1ri1061 \leq r_i \leq 10^6

respectively.

خروجی🔗

Print a single integer in a line, denoting the number of regions on the plane.

مثال‌ها🔗

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

1
0 3
Plain text

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

2
Plain text

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

2
0 3
0 3
Plain text

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

2
Plain text

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

2
0 3
2 2
Plain text

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

4
Plain text

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

2
-3 3
3 3
Plain text

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

3
Plain text

G – Network Connections


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

SBU Newbies Contest is the oldest annual contest in Computer Science and Engineering department. It is usually held on the second week of “Esfand”. There are many dedicated students who work voluntarily to prepare the contest. These students are divided into 3 groups and they take the tasks based on their group.

The first group of the students are the executive staff. They prepare the contest area, provide meals, do the registration, and many more things!! The second group of the students are the technical staff. They provide a secure network for the contest and setup the online judge. The last group is the scientific group. They write and prepare the problems, generate test cases, and verify the solutions.

This problem is written based on one of the challenges that technical staff faced during the preparation of the contest this year. As you may know, an official contest should strictly prohibit the use of internet during the contest. Trying to solve this problem, the technical staff should gather the cellphones before the contest or use mobile phone jammers in the contest area. Additionally, they should provide a network which restricts contestants’ internet access. After many hours of discussion and crowd-testing, they decided to configure a single router in the SBU and disconnect it from the internet.

The routers structure in the SBU network can be described as a rooted tree. Each router receives the internet from its parent router and will provide the internet for its children. When the technical team configure a router, all the routers in its subtree (including itself) would be disconnected from the internet, but the local connection is still available.

Given the routers which should be disconnected from internet and the structure of the SBU network, find the router which should be configured, in order to disconnect all the given routers from the internet.

If multiple answers exist, print the one which is the farthest from the root of the tree. We don’t want the whole university to lose internet access. You can assume that Router 1 is the root of the network.

ورودی🔗

The first line of the input contains two space-separated integers NN and KK indicating the number of routers in the network and the number of routers which should be disconnected.

1N,K1051 \leq N, K \leq 10^5

Each of the next N1N - 1 lines describe a connection by giving two space separated integers ui,viu_i, v_i – the number of two routers operating as end-points of the connection.

The last line of the input contains KK space-separated distinc integer aia_i denoting the routers which should be disconnected from the internet.

1ai,ui,vi1051 \leq a_i, u_i, v_i \leq 10^5

خروجی🔗

Print the lowest router which should be disconnected from the internet in order to avoid internet usage during the contest.

مثال‌ها🔗

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

7 2
1 2
2 7
3 4
2 4
4 5
6 4
6 7
Plain text

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

2
Plain text

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

4 1
1 3
2 3
2 4
3
Plain text

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

3
Plain text

H – Music Shopping


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

As everybody may know, Reza Shiri is our favorite artist. SBU students love his songs and they share them in their telegram groups all the time. In the Newbies 2018, the hardest problem of the contest was about Reza Shiri’s concert.

This year you are going to help us with purchasing Reza Shiri’s songs. Since we love Reza Shiri and his songs, we never download his songs for free. Reza Shiri’s songs must be purchased from the authorized stores.

Going to the iTunes store, each song can be purchased in one of the following ways:

  • Buying the whole album
  • Buying a single track

Well, everybody wants to purchase all of the available songs and albums. Unfortunately, having a limited budget, one may not be able to afford buying all of the songs. There are NN songs and MM albums available. Each song belongs to exactly one Album. Songs are numbered from 11 to NN. Albums are numbered from 11 to MM.

ورودی🔗

The first line of input contains three space-separated integers N,M,N, M, and PP – denoting the number of songs, the number of albums, and our budget respectively. The description of songs is given in the following NN lines.

1N,M,P10001 \leq N, M, P \leq 1000

Each line consists of 2 space-separated integers aia_i and pip_i – indicating the album containing this song and the price of this song.

In the next line of each test, there are MM space separated integers b1,b2,,bmb_1, b_2, \dots, b_m – denoting the price of the albums.

1bi,pi1091 \leq b_i, p_i \leq 10^9 1aiM1 \leq a_i \leq M

خروجی🔗

Print the maximum number of songs that we can afford to buy.

مثال‌ها🔗

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

5 2 10
1 3
1 4
1 2
2 1
2 2
7 4
Plain text

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

5
Plain text

In the first test case, it is the best to buy the first album and two songs from the second album.

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

5 2 10
1 3
1 4
1 2
2 1
2 2
8 4
Plain text

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

4
Plain text

In the second test case, it is the best to buy the first album and one song from the second album.

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

5 3 7
1 2
1 2
1 2
1 2
2 2
6 1 3
Plain text

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

5
Plain text

In the third test case, it is the best to buy the first and the second album.

I – Morning Run


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

Everyday Space Runners go to the Enghelab Sports Complex for a morning run in the “Health Road”. They love to exercise and workout. The only thing bothering them is that some people are not punctual and they do not arrive on-time. After days of discussion, they decided to run individually.

They run along on the axis Ox. For every person there are three parameters characterizing their behavior: ti,si,fit_i, s_i, f_i — the moment of time when the ii-th person starts running, the start point and the end point of the run respectively. Each person moves in a straight line along the road from sis_i to fif_i with a constant speed of either 11 or 1-1 depending on the direction.

If two or more persons meet at the health road (they are at the same point at the same time, no matter which directions they are going) they all greet each other. Like in the normal life, every pair of people greet each other at most once.

You task is to calculate for every person how many people she greets while running along the health road. Please, pay attention to the fact that ii-th person may meet and greet any other person at points sis_i and fif_i.

After a person achieves the destination point fif_i she moves out of the road and cannot greet anyone else. The same rule applies to the start of the run: a person cannot greet anyone until she appears on the road.

ورودی🔗

At the first line of the input, integer NN is given – number of groups. In the each of next NN lines, there are three space-separated integers ti,si,fit_i, s_i, f_i.

1N501 \leq N \leq 50 1ti,si,fi1091 \leq t_i, s_i, f_i \leq 10^9sitis_i \neq t_i

خروجی🔗

The single line of the output should contain a sequence of nn integers r1,r2,,rnr_1, r_2, \dots, r_n separated by a space, where rir_i denotes the number of times which the ii-th person greets other people while running along the health road.

مثال‌ها🔗

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

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

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

2 2 2 0
Plain text

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

4
10 1 1000000000
10 1000000000 1
5 1 3
8 5 3
Plain text

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

1 1 0 0
Plain text

J – Seasons


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

Our planet Earth has got two significant movements which are called Earth’s rotation and Earth’s revolution. Earth rotates along its own axis every day, which is why this movement is called the Earth’s Rotation. Furthermore, the movement of earth around the sun is called the Earth’s revolution which takes one year to complete.

The Earth’s movement trajectory around the Sun, which is called orbit, is not a perfect circle. According to the observations, the Earth’s orbit is elliptical which means that our distance from the Sun varies by day.

توضیح تصویر

There is a common misbelief that the seasons appear as a consequence of the Earth’s varying distance from the Sun. This hypothesis can be simply rejected by the fact that we can observe different seasons in different countries at the same time. For example, people in France celebrate the Christmas in the cold and snowy weather, but people in Australia celebrate the Christmas by the beach!

Earth’s rotation movement is the reason to the climate change and the seasons. It will affect the northern and southern hemisphere differently. For example, in Australia spring consists of September, October, and November. Summer which is hottest season of the year includes December, January, and February. Autumn comprises March, April, and May. And finally, winter consists of June, July, and August. In this problem you are asked to write a program to calculate the season in Australia based on the given month.

ورودی🔗

The input contains a string which is the name of the given month written in lowercase English letters.

خروجی🔗

Print the season which contains the given month in lowercase English letters.

مثال‌ها🔗

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

january
Plain text

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

summer
Plain text

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

september
Plain text

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

spring
Plain text

K – The Sacred Number


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

After burning down their own house, Pat & Mat decided to play a game in order to forget their misery. The rules of the game are simple. There are NN stones on the table. Pat & Mat play alternatively. Pat plays first. At each turn, they can either remove 11 or 22 or 33 or 44 or 55 or 66 stones from the table. The player who can't perform any legal move in his turn, will lose the game. Given the number NN, determine which of the brothers will win the game if they play optimally.

ورودی🔗

The input consists of a single integer NN denoting the number of stones. 1N1061 \leq N \leq 10^6

خروجی🔗

Print the name of the winner in a single line.

مثال‌ها🔗

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

6
Plain text

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

Pat
Plain text

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

2233
Plain text

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

Mat
Plain text

L – N-Bonacci Numbers


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

A sequence is called NN-Bonnaci if it is calculated from its previous NN elements. An XOR NN-Bonacci sequence can be formulated in the following format:

F(K)=F(K1)F(K2)F(KN+1)F(KN)F(K) = F(K - 1) \oplus F(K - 2) \oplus \dots \oplus F(K - N + 1) \oplus F(K - N)

Here \oplus denotes bitwise XOR operation. Newbies jury have recently found an interesting sequence which is obtained from the XOR operation on the first KK elements of the XOR NN-Bonacci sequence:

S(K)=F(1)F(2)F(K1)F(K)S(K) = F(1) \oplus F(2) \oplus \dots \oplus F(K - 1) \oplus F(K)

It can be proven that for calculating all of the NN-Bonacci Numbers, only the first NN elements are need to be known. Given the first NN numbers of the XOR NN-Bonacci sequence, your task is to calculate S(K)S(K) for QQ queries.

ورودی🔗

The first line of the input contains two space separated integers NN QQ. The second line contains NN space separated integers denoting F(1),F(2),,F(N)F(1), F(2), \dots , F(N) respectively. Each of the next QQ lines, describes a query by giving an integer KK.

1N,Q1051 \leq N, Q \leq 10^5

خروجی🔗

For each query, you should print the S(K)S(K) in a line.

مثال‌ها🔗

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

3 4
0 1 2
7
2
5
1000000000
Plain text

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

3
1
0
0
Plain text