Thứ Ba, 21 tháng 6, 2016

Wet Shark and Bishops

Today, Wet Shark is given n bishops on a 1000 by 1000 grid. Both rows and columns of the grid are numbered from 1 to 1000. Rows are numbered from top to bottom, while columns are numbered from left to right.
Wet Shark thinks that two bishops attack each other if they share the same diagonal. Note, that this is the only criteria, so two bishops may attack each other (according to Wet Shark) even if there is another bishop located between them. Now Wet Shark wants to count the number of pairs of bishops that attack each other.
Input
The first line of the input contains n (1 ≤ n ≤ 200 000) — the number of bishops.
Each of next n lines contains two space separated integers xi and yi (1 ≤ xi, yi ≤ 1000) — the number of row and the number of column where i-th bishop is positioned. It's guaranteed that no two bishops share the same position.
Output
Output one integer — the number of pairs of bishops which attack each other.
Examples
Input
Output

Input
Output
5
1 1
1 5
3 3
5 1
5 5
6

3
1 1
2 3
3 5
0

Note
In the first sample following pairs of bishops attack each other: (1, 3)(1, 5)(2, 3)(2, 4)(3, 4) and (3, 5). Pairs (1, 2)(1, 4)(2, 5)and (4, 5) do not attack each other because they do not share the same diagonal.
Tóm tắt đề:

Cho N con tượng đứng trên bàn cờ kích thước 1000 x 1000. Hỏi có bao nhiêu cặp
tượng ăn nhau ?

Chocolate Bar

You have a rectangular chocolate bar consisting of n × m single squares. You want to eat exactly k squares, so you may need to break the chocolate bar.
In one move you can break any single rectangular piece of chocolate in two rectangular pieces. You can break only by lines between squares: horizontally or vertically. The cost of breaking is equal to square of the break length.
For example, if you have a chocolate bar consisting of 2 × 3 unit squares then you can break it horizontally and get two 1 × 3 pieces (the cost of such breaking is 32 = 9), or you can break it vertically in two ways and get two pieces: 2 × 1 and 2 × 2 (the cost of such breaking is 22 = 4).
For several given values nm and k find the minimum total cost of breaking. You can eat exactly k squares of chocolate if after all operations of breaking there is a set of rectangular pieces of chocolate with the total size equal to k squares. The remaining n·m - ksquares are not necessarily form a single rectangular piece.
Input
The first line of the input contains a single integer t (1 ≤ t ≤ 40910) — the number of values nm and k to process.
Each of the next t lines contains three integers nm and k (1 ≤ n, m ≤ 30, 1 ≤ k ≤ min(n·m, 50)) — the dimensions of the chocolate bar and the number of squares you want to eat respectively.
Output
For each nm and k print the minimum total cost needed to break the chocolate bar, in order to make it possible to eat exactly ksquares.
Examples
Input
Output
4
2 2 1
2 2 3
2 2 2
2 2 4
5
5
4
0
ote
In the first query of the sample one needs to perform two breaks:
·         to split 2 × 2 bar into two pieces of 2 × 1 (cost is 22 = 4),
·         to split the resulting 2 × 1 into two 1 × 1 pieces (cost is 12 = 1).
In the second query of the sample one wants to eat 3 unit squares. One can use exactly the same strategy as in the first query of the sample.
Tóm tắt đề

Cho một thanh sô cô la kích thước n x m. Bạn cần cắt miếng sô cô la này sao cho đúng k ô vuông mà chi phí cắt là nhỏ nhất. Biết rằng miếng sô cô la kích thước i x j, thì khi cắt theo chiều dọc, tức cắt thành 2 miếng u x j và v x j thì chi phí được cộng thêm một lượng là i x i. Còn nếu cắt theo chiều ngang thì chi phí được cộng thêm một lượng là j x j.

Expression

Petya studies in a school and he adores Maths. His class has been studying arithmetic expressions. On the last class the teacher wrote three positive integers abc on the blackboard. The task was to insert signs of operations '+' and '*', and probably brackets between the numbers so that the value of the resulting expression is as large as possible. Let's consider an example: assume that the teacher wrote numbers 1, 2 and 3 on the blackboard. Here are some ways of placing signs and brackets:
·                     1+2*3=7
·                     1*(2+3)=5
·                     1*2*3=6
·                     (1+2)*3=9
Note that you can insert operation signs only between a and b, and between b and c, that is, you cannot swap integers. For instance, in the given sample you cannot get expression (1+3)*2.
It's easy to see that the maximum value that you can obtain is 9.
Your task is: given ab and c print the maximum value that you can get.
Input: The input contains three integers ab and c, each on a single line (1 ≤ a, b, c ≤ 10).
Output:  Print the maximum value of the expression that you can obtain.
Sample Input:
Input
Output

Input
Output
1
2
3
9

2
10
3
60
Tóm tắt đề:

Cho 3 số a , b , c. Yêu cầu đặt các dấu ngoặc , dấu + và dấu * vào sao cho kết quả thu được là lớn nhất.

Game With Sticks

After winning gold and silver in IOI 2014, Akshat and Malvika want to have some fun. Now they are playing a game on a grid made of n horizontal and m vertical sticks.
An intersection point is any point on the grid which is formed by the intersection of one horizontal stick and one vertical stick.
In the grid shown below, n = 3 and m = 3. There are n + m = 6 sticks in total (horizontal sticks are shown in red and vertical sticks are shown in green). There are n·m = 9 intersection points, numbered from 1 to 9.

The rules of the game are very simple. The players move in turns. Akshat won gold, so he makes the first move. During his/her move, a player must choose any remaining intersection point and remove from the grid all sticks which pass through this point. A player will lose the game if he/she cannot make a move (i.e. there are no intersection points remaining on the grid at his/her move).
Assume that both players play optimally. Who will win the game?
Input
The first line of input contains two space-separated integers, n and m (1 ≤ n, m ≤ 100).
Output
Print a single line containing "Akshat" or "Malvika" (without the quotes), depending on the winner of the game.
Sample Input
Input
Output

Input
Output

Input
Output
2 2
Malvika

2 3
Malvika

3 3
Akshat
Hint
Explanation of the first sample:
The grid has four intersection points, numbered from 1 to 4.

If Akshat chooses intersection point 1, then he will remove two sticks (1 - 2 and 1 - 3). The resulting grid will look like this.
Now there is only one remaining intersection point (i.e. 4). Malvika must choose it and remove both remaining sticks. After her move the grid will be empty.
In the empty grid, Akshat cannot make any move, hence he will lose.
Since all 4 intersection points of the grid are equivalent, Akshat will lose no matter which one he picks.



Tóm tắt đề

Akshat và Malvika chơi trò rút que. N que đặt ngang và M que đặt dọc tạo thành một lưới ô vuông gồm ác điểm. Mỗi đợt chơi, mỗi người sẽ chọn một điểm và họ sẽ rút những que đi qua điểm đó. Trò chơi kết thúc khi không còn điểm nào và người không chọn được điểm đó là người thua. Akshat đi đầu tiên.

Chủ Nhật, 19 tháng 6, 2016

TRUYỀN TIN


Một lớp gồm N học sinh, mỗi học sinh cho biết những bạn mà học sinh đó có thể liên lạc được (chú ý liên lạc này là liên lạc một chiều: u có thể gửi tin tới v nhưng v thì chưa chắc đã có thể gửi tin tới u).
Thầy chủ nhiệm đang có một thông tin rất quan trọng cần thông báo tới tất cả các học sinh. Để tiết kiệm thời gian, thầy chỉ nhắn tin tới 1 số học sinh rồi sau đó nhờ các học sinh này nhắn lại cho tất cả các bạn mà các học sinh đó có thể liên lạc được, và cứ lần lượt như thế làm sao cho tất cả các học sinh trong lớp đều nhận được tin .
Hãy tìm một số ít nhất các học sinh mà thầy chủ nhiệm cần nhắn.
Dữ liệu vào:
- Dòng đầu là N, M (N <= 5000, M là số lượng liên lạc 1 chiều)
- Một số dòng tiếp theo mỗi dòng gồm 2 số u , v cho biết học sinh u có thể gửi tin tới học sinh v
Dữ liệu ra:  Gồm 1 dòng ghi số học sinh cần thầy nhắn tin.
Ví dụ:

MESSAGE.INP

MESSAGE.OUT

12 15
1 3
3 6
6 1
6 8
8 12
12 9
9 6
2 4
4 5
5 2
4 6
7 10
10 11
11 7
10 9
2
 


THÀNH PHẦN LIÊN THÔNG MẠNH

Cho đồ thị G(V,E) có hướng n (1<=n<=10^4)  đỉnh m (1<=m<=10^5) cung, Hãy đếm số thành phần liên thông mạnh của G.
Dữ liệu vào:
+Dòng đầu tiên là n,m.
+M dòng tiếp theo mô tả một cung của G.
Dữ liệu ra:
Gồm một dòng duy nhất là số TPLT mạnh.                  
Ví dụ:
Input
Output

Input
Output
3 2
1 2
2 3
3

3 3
1 2
2 3
3 1
1