Inverse AI Intra Team Contest 2022: 11 Problems 5 Hours

Download as pdf or txt
Download as pdf or txt
You are on page 1of 15

Inverse AI Intra

Team Contest 2022


11 Problems
5 Hours
Mini Battleship
Battleship is a game played by two players. Each
player has their own grid, which is hidden from their opponent.
Each player
secretly places some ships on their grid. Each ship
covers a horizontal or vertical straight line of one or more
contiguous
squares. Ships cannot overlap. All ships are
considered distinct, even if they have the same size. The
orientation of each ship is
not important to the game, only the
squares they occupy.

After placing their ships, the players then take turns


taking shots at their opponent’s ships by calling out a
coordinate of their
opponent’s grid. The opponent must honestly
say whether the shot was a hit or a miss. When all of a ship’s
squares are hit, that
ship sinks (“You sunk my battleship!!”).
A player loses when all of their ships are sunk.

Bob is playing a game of Mini Battleship against Alice.


Regular Battleship is played on a 10 × 10 grid with
5 ships. Mini
Battleship
is much smaller, with a grid no larger than 5 × 5 and possibly fewer
than 5 ships.

Bob wonders how many ship placements are possible on Alice’s


board given what he knows so far. The answer will be
0 if Alice
is cheating!
(Or, if the game setup isn’t possible.)

Input
The first line of input contains two space-separated
integers n (1 ≤ n ≤ 5) and k (1 ≤ k ≤ 5), which represent a game of
Mini Battleship
played on an n × n grid with k
ships.

Each of the next n


lines contains a string s (|s| = n). This is what Bob sees of
Alice’s grid so far.

A character ‘X’ represents one of


Bob’s shots that missed.

A character ‘O’ (Letter O, not zero)


represents one of Bob’s shots that hit.

A dot (‘.’) represents a square


where Bob has not yet taken a shot.

Each of the next k


lines contains a single integer x (1 ≤ x ≤ n ). These are the sizes of the ships.

Output
Output a single integer, which is the number of ways the
k distinct ships could
be placed on Alice’s grid and be consistent with
what Bob
sees.

Sample Input 1 Sample Output 1


4 3
132

....

.OX.

....

O..X

Sample Input 2 Sample Output 2


4 4
6

.X.X

.XX.

...X

....

Sample Input 3 Sample Output 3


2 2
4

..

..

Cave Exploration
Alice is a soccer coach who occasionally bring her soccer
team to explore Caveland (that can be modeled as an undirected
unweighted connected graph) for special event, e.g. for
initiation ceremony, to celebrate birthdays, etc. Caveland has
N
junctions and
M tunnels.

Caveland is quite prone to flooding, but that does not stop


Alice and her soccer team from doing what they enjoy. You are
Bob,
Alice’s good friend. You want to ensure Alice and her
soccer team are as safe as possible by letting her know which
junction(s)
is/are safe(r) than the rest. You decide that a
junction v is considered
to be safe(r) if any one tunnel is
flooded, Alice and her
soccer team can still go out from
junction v to the
entrance of Caveland (which is always junction 0) via a non-flooded path.

For this example, junctions {0, 1, 2, 3} are considered


safe(r). If tunnel 0– 2
is flooded for example, Alice and her soccer team
can detour
via path 2 → 3 → 1 → 0 to reach Caveland entrance.
However junctions {4, 5, 6, 7, 8} are quite dangerous. If
tunnel 2– 8 (or tunnel 3– 4) is flooded, Alice and her
soccer team will be trapped (cannot reach safety/junction
0).

Input
The first line of input consists of 2 integers: N and M . (2 ≤ N ≤ 10 000, 1 ≤ M ≤ min(N (N − 1)/2, 100 000)). The next
M lines contains
M pairs of integers
u and v that describe 0 -based indices of the two junctions
that are connected with a
tunnel in Caveland (0 ≤ u, v < N , and u ≠ v). No two junctions are
directly connected with more than one tunnel. You are
guaranteed that junction 0 can reach all the other
N − 1 junctions (if there
is no flood).

Output
Print an integer in one line: The total number of
junction(s) in Caveland that is/are safe(r) for Alice and her
soccer team to
explore. The actual junction numbers are not
needed.

Sample Input 1 Sample Output 1


9 10
4

0 1

0 2

1 3

2 3

2 8

3 4

4 5

4 6

5 7

6 7

Once upon a time, Sand King used to rule the sand country. In the sand country, there was sand
everywhere, but there were a very few sources of oxygen. In the sand country, trees were worshipped,
as without them sand people would not live. But the Sand King was very much fond of football. So
he ordered to make a very BIG football stadium (rectangular in shape) in his kingdom. The news was
very alarming to all the citizens of the sand kingdom. As cutting the very few trees would cause death
to the living creatures. So they requested the king to not make the football stadium.
The king was so stubborn in his idea that he would not give up making a football stadium. But he
was also sympathetic to his citizens. So he has hired you to help him out.
You are given the width and height of the sand kingdom. Fortunately, sand king wants the football
stadium to be parallel to the borders of the sand kingdom (he does not like bad orientation of the
football stadium). You are also given the positions of the valuable trees. Your task is to maximize the
area of the football stadium so that no trees are harmed. You can consider the trees as points. The
trees may stand at the boundary of the football stadium, but cannot be strictly inside it. Note that
the Kingdom is a rectangle with axis parallel sides and the lower left point of the kingdom is (0, 0) and
upper right point is (L, W ).

Input
The first line of the input contains a number T (1 ≤ T ≤ 100), denoting the total number of test cases.
Next, T test cases follow. Each test case begins with two numbers, L and W (1 ≤ L, W ≤ 10000),
denoting the length and width of the sand kingdom respectively. Then a number N (1 ≤ N ≤ 200).
Followed by, there are N integer co-ordinates of trees, xi , yi , where (0 ≤ xi ≤ L, 0 ≤ yi ≤ W ).

Output
For each input set, first print the case number, followed by the maximum possible area of the football
stadium. See the sample input/output for more details.

Sample Input
3
4 8
3
1 2
3 4
3 7
12 10
2
3 6
8 9
5 5
2
0 0
5 5

Sample Output
Case 1: 18
Case 2: 81
Case 3: 25
An Interesting Sequence

Zanka finds fun in everyday simple things. One fine day he got interested in a very
trivial sequence. Given a natural number k , he considers the sequence
Ai = k + i
2
so that the terms are

k + 1, k + 4, k + 9, k + 16, …

Now to make things more interesting, he considers the gcd of consecutive terms
in the sequence, then takes the sum of the first 2k values. More formally, he wants
to compute

2k

∑ gcd(A i , A i+1 )

i=1

Denote this sum by S . Zanka wants you to print the number S for each k .

Input
The first line contains an integer T , the number of test cases. Descriptions of
test cases follow.
The only line of each test case contains a single integer k .

Output
For each test case, output a single line containing the sum S for the given k .

Constraints
6
1 ≤ T ≤ 10
6
1 ≤ k ≤ 10

Subtasks
Subtask #1 (20 points): t ≤ 10
3
,k 3
≤ 10

Subtask #2 (80 points): Original Constraints

Sample Input 1

1

Sample Output 1

6

Explanation
The first 2k + 1 terms of the sequence A are 2, 5, 10 .

So the answer is gcd(2, 5) + gcd(5, 10) = 1 + 5 = 6


C. DZY Loves Fibonacci Numbers
time limit per test: 4 seconds
memory limit per test: 256 megabytes
input: standard input
output: standard output

In mathematical terms, the sequence Fn of Fibonacci numbers is defined by the recurrence relation

F1 = 1; F2 = 1; Fn = Fn - 1 + Fn - 2 (n > 2).
DZY loves Fibonacci numbers very much. Today DZY gives you an array consisting of n integers: a1, a2, ..., an. Moreover, there are m queries,
each query has one of the two types:

1. Format of the query "1 l r". In reply to the query, you need to add Fi - l + 1 to each element ai, where l ≤ i ≤ r.
2. Format of the query "2 l r". In reply to the query you should output the value of modulo 1000000009 (109 + 9).

Help DZY reply to all the queries.

Input
The first line of the input contains two integers n and m (1 ≤ n, m ≤ 300000). The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109)
— initial array a.

Then, m lines follow. A single line describes a single query in the format given in the statement. It is guaranteed that for each query inequality
1 ≤ l ≤ r ≤ n holds.

Output
For each query of the second type, print the value of the sum on a single line.

Examples
input Copy

4 4

1 2 3 4

1 1 4

2 1 4

1 2 4

2 1 3

output Copy

17

12

Note
After the first query, a = [2, 3, 5, 7].

For the second query, sum = 2 + 3 + 5 + 7 = 17.

After the third query, a = [2, 4, 6, 9].

For the fourth query, sum = 2 + 4 + 6 = 12.


Petrozavodsk Winter Training Camp 2016
Day 4: Moscow SU Trinity Contest, Tuesday, February 2, 2016

Problem F. Frank Sinatra


Input file: standard input
Output file: standard output
Time limit: 6 seconds
Memory limit: 256 mebibytes

You are given a bidirectional graph T which is a tree consisting of n vertices and n − 1 edges.
Each edge of the tree is associated with some non-negative integer xi .
Your task has a very simple description. You are given q queries. In j-th, query you have to find
the smallest non-negative integer y that is not present in the set of all integers associated with
edges of the simple path between vertices aj and bj .

Input
The first line of input contains two integers n and q (2 ≤ n ≤ 105 , 1 ≤ q ≤ 105 ), the number of
vertices of the tree and the number of queries.
The following n − 1 lines contain triples of integers ui , vi , xi (1 ≤ ui , vi ≤ n, ui ̸= vi , 0 ≤ xi ≤ 109 ),
each denoting an edge (ui , vi ) associated with an integer xi .
The following q lines contain pairs of integers aj , bj (1 ≤ aj , bj ≤ n), each denoting a query about
the path between vertices aj and bj .

Output
For each query, output one line containing the smallest non-negative y such that there is no edge
associated with y lying on the corresponding simple path.

Example
standard input standard output
7 6 0
2 1 1 1
3 1 2 2
1 4 0 2
4 5 1 3
5 6 3 3
5 7 4
1 3
4 1
2 4
2 5
3 5
3 7

Page 9 of 15
A - Parking

Time Limit: 2 sec / Memory Limit: 256 MB

Score : 100 points

Problem Statement
You are parking at a parking lot. You can choose from the following two fee plans:

Plan 1: The fee will be A × T yen (the currency of Japan) when you park for T hours.
Plan 2: The fee will be B yen, regardless of the duration.

Find the minimum fee when you park for N hours.

Constraints
1 ≤ N ≤ 20
1 ≤ A ≤ 100
1 ≤ B ≤ 2000
All input values are integers.

Input
Input is given from Standard Input in the following format:

N A B

Output
When the minimum fee is x yen, print the value of x.

Sample Input 1 Copy

7 17 120
Copy

Sample Output 1 Copy

119
Copy

If you choose Plan 1, the fee will be 7 × 17 = 119 yen.


If you choose Plan 2, the fee will be 120 yen.

Thus, the minimum fee is 119 yen.

Sample Input 2 Copy

5 20 100
Copy

Sample Output 2 Copy

100
Copy

The fee might be the same in the two plans.

Sample Input 3 Copy

6 18 100
Copy

Sample Output 3 Copy

100
Copy
C - Half and Half

Time Limit: 2 sec / Memory Limit: 256 MB

Score : 300 points

Problem Statement
"Pizza At", a fast food chain, offers three kinds of pizza: "A-pizza", "B-pizza" and "AB-pizza". A-pizza
and B-pizza are completely different pizzas, and AB-pizza is one half of A-pizza and one half of B-
pizza combined together. The prices of one A-pizza, B-pizza and AB-pizza are A yen, B yen and C
yen (yen is the currency of Japan), respectively.

Nakahashi needs to prepare X A-pizzas and Y B-pizzas for a party tonight. He can only obtain
these pizzas by directly buying A-pizzas and B-pizzas, or buying two AB-pizzas and then rearrange
them into one A-pizza and one B-pizza. At least how much money does he need for this? It is fine to
have more pizzas than necessary by rearranging pizzas.

Constraints
1 ≤ A, B, C ≤ 5000
1 ≤ X, Y ≤ 105
All values in input are integers.

Input
Input is given from Standard Input in the following format:

A B C X Y

Output
Print the minimum amount of money required to prepare X A-pizzas and Y B-pizzas.

Sample Input 1 Copy

1500 2000 1600 3 2


Copy

Sample Output 1 Copy


Copy
7900

It is optimal to buy four AB-pizzas and rearrange them into two A-pizzas and two B-pizzas, then buy
additional one A-pizza.

Sample Input 2 Copy

1500 2000 1900 3 2


Copy

Sample Output 2 Copy

8500
Copy

It is optimal to directly buy three A-pizzas and two B-pizzas.

Sample Input 3 Copy

Copy
1500 2000 500 90000 100000

Sample Output 3 Copy

Copy
100000000

It is optimal to buy 200000 AB-pizzas and rearrange them into 100000 A-pizzas and 100000 B-
pizzas. We will have 10000 more A-pizzas than necessary, but that is fine.
C. Searching Local Minimum
time limit per test: 2 seconds
memory limit per test: 512 megabytes
input: standard input
output: standard output

This is an interactive problem.

Homer likes arrays a lot and he wants to play a game with you.

Homer has hidden from you a permutation a1 , a2 , … , an of integers 1 to n . You are asked to find any index k (1 ≤ k ≤ n ) which is a local
minimum.

For an array a1 , a2 , … , an , an index i (1 ≤ i ≤ n) is said to be a local minimum if ai < min{ai−1 , ai+1 } , where a0 = an+1 = +∞ . An
array is said to be a permutation of integers 1 to n , if it contains all integers from 1 to n exactly once.

Initially, you are only given the value of n without any other information about this permutation.

At each interactive step, you are allowed to choose any i (1 ≤ i ≤ n ) and make a query with it. As a response, you will be given the value of ai .

You are asked to find any index k which is a local minimum after at most 100 queries.

Interaction
5
You begin the interaction by reading an integer n (1 ≤ n ≤ 10 ) on a separate line.

To make a query on index i (1 ≤ i ≤ n ), you should output "? i " in a separate line. Then read the value of ai in a separate line. The number of
the "?" queries is limited within 100 .

When you find an index k (1 ≤ k ≤ n ) which is a local minimum, output "! k" in a separate line and terminate your program.

In case your query format is invalid, or you have made more than 100 "?" queries, you will receive Wrong Answer verdict.

After printing a query do not forget to output end of line and flush the output. Otherwise, you will get Idleness limit exceeded. To do this,
use:

fflush(stdout) or cout.flush() in C++;


System.out.flush() in Java;
flush(output) in Pascal;
stdout.flush() in Python;
see documentation for other languages.

Hack Format

5
The first line of the hack should contain a single integer n (1 ≤ n ≤ 10 ).

The second line should contain n distinct integers a1 , a2 , … , an (1 ≤ ai ≤ n ).

Example
input Copy

output Copy

? 1

? 2

? 3

? 4

? 5

! 3

Note
In the example, the first line contains an integer 5 indicating that the length of the array is n = 5 .

The example makes five "?" queries, after which we conclude that the array is a = [3, 2, 1, 4, 5] and k = 3 is local minimum.
D. Lost Tree
time limit per test: 3 seconds
memory limit per test: 256 megabytes
input: standard input
output: standard output

This is an interactive problem.

Little Dormi was faced with an awkward problem at the carnival: he has to guess the edges of an unweighted tree of n nodes! The nodes of the
tree are numbered from 1 to n .

The game master only allows him to ask one type of question:

Little Dormi picks a node r (1 ≤ r ≤ n), and the game master will reply with an array d1 , d2 , … , dn , where di is the length of the shortest
path from node r to i , for all 1 ≤ i ≤ n.

Additionally, to make the game unfair challenge Little Dormi the game master will allow at most ⌈ n2 ⌉ questions, where ⌈x⌉ denotes the smallest
integer greater than or equal to x .

Faced with the stomach-churning possibility of not being able to guess the tree, Little Dormi needs your help to devise a winning strategy!

Note that the game master creates the tree before the game starts, and does not change it during the game.

Input
The first line of input contains the integer n (2 ≤ n ≤ 2 000 ), the number of nodes in the tree.

You will then begin interaction.

Output
When your program has found the tree, first output a line consisting of a single "!" followed by n − 1 lines each with two space separated
integers a and b, denoting an edge connecting nodes a and b (1 ≤ a, b ≤ n ). Once you are done, terminate your program normally immediately
after flushing the output stream.

You may output the edges in any order and an edge (a, b) is considered the same as an edge (b, a). Answering is not considered as a query.

Interaction
After taking input, you may make at most ⌈ n ⌉ queries. Each query is made in the format "? r", where r is an integer 1 ≤ r ≤ n that denotes
2

the node you want to pick for that query.

You will then receive n space separated integers d1 , d2 , … , dn , where di is the length of the shortest path from node r to i , followed by a
newline.

After printing a query do not forget to output end of line and flush the output. Otherwise, you will get Idleness limit exceeded. To do this,
use:

fflush(stdout) or cout.flush() in C++;


System.out.flush() in Java;
flush(output) in Pascal;
stdout.flush() in Python;
see documentation for other languages.
n
If at any point you make an invalid query or try to make more than ⌈ 2 ⌉ queries, the interaction will terminate immediately and you will
receive a Wrong Answer verdict.

Hacks

To hack a solution, use the following format.

The first line contains the integer n (2 ≤ n ≤ 2 000 ).

The next n − 1 lines contain two integers u and v (1 ≤ u, v ≤ n ) denoting an edge between u and v (u ≠ v ). These n − 1 edges must form a
tree.

Examples
input Copy

0 1 2 2

1 0 1 1
output Copy

? 1

? 2

4 2

1 2

2 3

input Copy

2 2 1 1 0

output Copy

? 5

4 5

3 5

2 4

1 3

Note
Here is the tree from the first example.

Notice that the edges can be output in any order.

Additionally, here are the answers for querying every single node in example 1 :

:
1 [0, 1, 2, 2]

:
2 [1, 0, 1, 1]

3 : [2, 1, 0, 2]

4 : [2, 1, 2, 0]

Below is the tree from the second example interaction.

Lastly, here are the answers for querying every single node in example 2 :

:
1 [0, 4, 1, 3, 2]

:
2 [4, 0, 3, 1, 2]

3 : [1, 3, 0, 2, 1]

4 : [3, 1, 2, 0, 1]

5 : [2, 2, 1, 1, 0]
C. Party Lemonade
time limit per test: 1 second
memory limit per test: 256 megabytes
input: standard input
output: standard output

A New Year party is not a New Year party without lemonade! As usual, you are expecting a lot of guests, and buying lemonade has already
become a pleasant necessity.

Your favorite store sells lemonade in bottles of n different volumes at different costs. A single bottle of type i has volume 2i - 1 liters and costs ci
roubles. The number of bottles of each type in the store can be considered infinite.

You want to buy at least L liters of lemonade. How many roubles do you have to spend?

Input
The first line contains two integers n and L (1 ≤ n ≤ 30; 1 ≤ L ≤ 109) — the number of types of bottles in the store and the required amount of
lemonade in liters, respectively.

The second line contains n integers c1, c2, ..., cn (1 ≤ ci ≤ 109) — the costs of bottles of different types.

Output
Output a single integer — the smallest number of roubles you have to pay in order to buy at least L liters of lemonade.

Examples
input Copy

4 12

20 30 70 90

output Copy

150

input Copy

4 3

10000 1000 100 10

output Copy

10

input Copy

4 3

10 100 1000 10000

output Copy

30

input Copy

5 787787787

123456789 234567890 345678901 456789012 987654321

output Copy

44981600785557577

Note
In the first example you should buy one 8-liter bottle for 90 roubles and two 2-liter bottles for 30 roubles each. In total you'll get 12 liters of
lemonade for just 150 roubles.

In the second example, even though you need only 3 liters, it's cheaper to buy a single 8-liter bottle for 10 roubles.

In the third example it's best to buy three 1-liter bottles for 10 roubles each, getting three liters for 30 roubles.

You might also like