Inverse AI Intra Team Contest 2022: 11 Problems 5 Hours
Inverse AI Intra Team Contest 2022: 11 Problems 5 Hours
Inverse AI Intra Team Contest 2022: 11 Problems 5 Hours
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.
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.
....
.OX.
....
O..X
.X.X
.XX.
...X
....
..
..
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.
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.
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
Sample Input 1
1
Sample Output 1
6
Explanation
The first 2k + 1 terms of the sequence A are 2, 5, 10 .
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).
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].
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
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.
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.
7 17 120
Copy
119
Copy
5 20 100
Copy
100
Copy
6 18 100
Copy
100
Copy
C - Half and Half
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.
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.
8500
Copy
Copy
1500 2000 500 90000 100000
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
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:
Hack Format
5
The first line of the hack should contain a single integer n (1 ≤ n ≤ 10 ).
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
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.
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
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:
Hacks
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.
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]
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
output Copy
10
input Copy
4 3
output Copy
30
input Copy
5 787787787
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.