task stringlengths 0 154k | __index_level_0__ int64 0 39.2k |
|---|---|
Clickbait (CLCKBAIT)
While surfing online, Slavko came across an ad displaying a system of containers and pipes.
The system consists of K containers denoted with numbers from 1 to K, and we can describe it using a matrix of characters with N rows and M columns.
The containers are in the shape of a rectangle, and the outlines of the containers and pipes are shown with the following characters:
‘-’ if it’s a horizontal part of the outline,
‘|’ if it’s a vertical part of the outline, and
‘+’ if it’s a spot where the horizontal and vertical parts of the outline connect. An exception is where the containers and pipes connect. In that case, the container outline dominates (see sample test).
In an arbitrary place within each container, there is a string of digits that represent the label of the container, and all the other fields in the matrix are equal to ‘.’ (dot).
All containers except the one labeled with 1 have exactly one supply pipe that enters the container in its upper side. The container labeled with 1 does not have a supply pipe.
The containers can have multiple (also possible, zero) discharge pipes that leave the container out of its lateral side. The places where discharge pipes leave a container will be in mutually distinct rows in the matrix.
The pipes directly connect two containers, which means that it is not possible to split the pipes or connect multiple pipes into one, and no two pipes will intersect. On their way, looking from the source to the destination container, the pipes always descend to the following row or stay in the same row. In other words, they never go back to the previous row, so the water can flow freely from one container to another.
The water enters a container until it is full. If the water level reaches the level of the discharge pipe, the water will flow through that pipe until the container the pipe leads into is filled up.
Help Slavko and determine the order in which the containers will fill up.
Please note:
The test data is such that each character ‘+’ is surrounded with exactly one character ‘-’ to the left or the right side and exactly one character ‘|’ to the upper or lower side, and all other adjacent characters in directions up, down, left and right will be equal to ‘.’ (dot).
The only places where the pipe in the matrix is in a field adjacent to the container outline are the places where the pipe enters or exits the container. In other words, a pipe will never run right next to a container (except where it connects with the container). The entry for the supply pipe is labeled with the character ‘|’ above a container, whereas the exit of the discharge pipe is labeled with the character ‘-’ on the lateral side of a container.
Input
The first line contains two integers N and M (1 ≤ N, M ≤ 1000), matrix dimensions. The following N lines contain M characters describing the container system.
Output
You must output K lines. The i th line contains the label of the container that fills up i th . A solution will always exist and will be unique.
Example
Input:
12 13
..+--+.......
+-|..|.......
|.|.1|--+....
|.+--+..|....
|......+----+
+---+..|..2.|
....|..+----+
.+--+........
.|...........
+---+........
|.3.|........
+---+........
Output:
2
3
1 | 34,400 |
Samvel and Boxes (SAMBOX)
King of Boxes Robert wants to test Samvel. He has given Samvel n boxes indexed from 1 to n. All the boxes except the first one are located in another box. Robert wants to fill the boxes with apples (At beginning boxes contain no apples). Robert can ask Samvel queries of 4 types.
The queries are:
Robert says two integers x, y to Samvel. Samvel needs to add x (1 ≤ x ≤ 10
9
) apples to the y-th box. (1 ≤ y ≤ n)
Robert says two integers x, y to Samvel. Samvel needs to swap the indexes of the x-th and y-th boxes. (1 ≤ x, y ≤ n)
Robert says an integer x to Samvel. Samvel needs to say the number of apples located in the box x (1 ≤ x ≤ n) and in the boxes that are in the box x (directly or indirectly).
Robert says an integer x to Samvel. Samvel needs to answer the query of the third type for the box that has minimum index from the boxes that are located directly in the box x. (1 ≤ x ≤ n)
Samvel needs your help. Help him and write a program to answer this queries.
Input
The first line of input contains an integer n (2 ≤ n ≤ 10
5
) and q (1 ≤ q ≤ 10
5
), number of boxes and number of queries.
Second line contains n-1 integers a
1
... a
n-1
. a
i-1
is the index of the box that contains box i. (1 ≤ a
i
≤ n)
The lines from 3 to q+2 contain an integer k (type of the query). if k=1 or k=2 then goes the two integer x, y from the task, else there is only one integer x.
Output
For each query of type 3 or 4 you need to output one integer the answer of query in a separate line. If query type is 4 and given box has no other boxes in it print -1.
Example
Input:
4 6
1 1 2
1 5 1
3 1
2 1 3
3 3
1 60 1
4 3
Output:
5
5
60 | 34,401 |
Difference One Swaps (SWAPDIFF1)
You are given an array of size $N$ containing the integers $1, 2 \ldots N$ in some order.
A
move
consists of swapping the integers $k$ and $k+1$ for some $1 \le k \lt N$. In other words, you may swap any pair of integers that has a difference of one.
Find the minimum number of moves required to sort the given array in ascending order.
Input
The first line contains $T$ ($1 \le T \le 1000$), the number of test cases.
Each test case contains $N$ ($2 \le N \le 10^5$) followed by $N$ distinct integers ($1 \le x_i \le N$).
The sum of $N$ over all test cases will not exceed $10^5$.
Output
For each test case, output the number of moves required to sort the array.
Example
Input:
5
2 1 2
2 2 1
3 3 2 1
4 4 2 3 1
6 2 1 4 3 6 5
Output:
0
1
3
5
3
Note
Below is one optimal sequence of moves that sorts [4,2,3,1].
Swap 1 and 2: [4,
2
,3,
1
] → [4,
1
,3,
2
].
Swap 2 and 3: [4,1,
3
,
2
] → [4,1,
2
,
3
].
Swap 3 and 4: [
4
,1,2,
3
] → [
3
,1,2,
4
].
Swap 2 and 3: [
3
,1,
2
,4] → [
2
,1,
3
,4].
Swap 1 and 2: [
2
,
1
,3,4] → [
1
,
2
,3,4]. | 34,402 |
Digit Subsequence (YOSEQ)
Given a string consisting of digits from '0' to '9', find the smallest
non-negative
integer that does not occur as a
contiguous subsequence
in the given string.
Input
Input only contains a string S (|S| ≤ 100000), which consists of digits from '0' to '9'.
Output
Output the smallest
non-negative
integer that does not occur as a
contiguous subsequence
in the given string.
Example
Input 1
0123456789
Output 1
10
Input 2
21698921085321984125
Output 2
7
Input 3
01234567891011121314151617181920
Output 3
22 | 34,403 |
Another Travelling Salesman Problem (YOSALES)
Ariel is a travelling salesman in his own world. Ariel wants to buy some toys at city
X
and sells them at city
Y
(
X
can be equal to
Y
).
His own world forms a weighted tree.
The cost to travel from city
X
to city
Y
is the sum of weight of edge between city
X
and city
Y
.
You are given
A[i]
,
B[i]
.
A[i]
denote the maximum number of toy that Ariel can buy at city
i
.
B[i]
denote the price of buying / selling a toy in city
i
.
Ariel can choose two city. Let it be
X
and
Y
. such that he can buy some toy at city
X
, travel between
X
and
Y
, and sell some toy at city
Y
.
Ariel can only buy some toy at no more than one city, and can only sell some toy at no more than one city.
Help Ariel to maximize his profit.
Input
First line contains an integer N.
Second line contains N integer A[i].
Third line contains N integer B[i].
The next N-1 lines contains U V and W, there is an edge between U and V with weight W.
Output
One integer that denote maximum profit Ariel can get.
Constraint
2 <= N <= 1e5.
1 <= A[i], B[i], W<= 1e9.
1 <= U, V <= N
Example
Input:
4
10 10 10 10
5 5 5 5
1 2 1
2 3 1
3 4 1
Output:
0
Input:
5
1 9 4 16 5
20 5 10 15 19
2 1 6
2 3 6
2 4 7
3 5 4
Output:
129
Input:
10
9 7 5 19 7 2 10 7 2 4
16 17 13 2 2 19 14 18 4 1
9 1 4
1 2 7
10 3 5
2 4 6
7 5 5
7 6 1
5 8 7
5 9 10
7 10 1
Output:
290 | 34,404 |
Even Semiprime Runs (EVENSEMIP)
A
semiprime
is a product of two primes such as $9 = 3\times3$ or $580519 = 41\times14159$.
The first ten semiprimes are $4$, $6$, $9$, $10$, $14$, $15$, $21$, $22$, $25$, and $26$.
A
run
of semiprimes is a contiguous subsequence of semiprimes. For example, $15$, $21$, $22$ is a run but $9$, $14$, $15$ is not because it skips over $10$.
An
even run
of semiprimes is a run that contains only even numbers. The first even run of length $3$ is $454$, $458$, $466$.
Your task is to find the longest even run of semiprimes between $N$ and $M$, inclusive.
Input
The first line contains $T$ ($1 \le T \le 20$), the number of test cases.
Each test case is one line containing the integers $N$ and $M$ ($1 \le N \le M \le 10^{14}$, $M - N \le 10^7$).
The sum of the differences $M - N$ over all test cases is at most $10^7$.
Output
Print two lines per test case. On the first line, print the length of the longest even run of semiprimes. On the second line, print the numbers in the run.
If there are multiple solutions, print the one with the smallest first number.
It is guaranteed that there is at least one even semiprime between $N$ and $M$.
Example
Input:
16
4 4
4 6
4 8
4 14
6 10
58 62
1 100
1 1000
1 10000
1 100000
1 1000000
5000000 10000000
247425787142 247425787222
247425000000 247425787221
466937174866 466937199999
99999999000000 100000000000000
Output:
1
4
2
4 6
2
4 6
2
4 6
1
6
2
58 62
2
4 6
3
454 458 466
3
454 458 466
3
454 458 466
5
111614 111626 111634 111638 111646
5
5161318 5161322 5161334 5161342 5161346
8
247425787142 247425787162 247425787166 247425787174 247425787178 247425787198 247425787214 247425787222
7
247425787142 247425787162 247425787166 247425787174 247425787178 247425787198 247425787214
4
466937176054 466937176058 466937176066 466937176078
4
99999999199498 99999999199502 99999999199522 99999999199558
Note
This problem was inspired by
Zak Seidov's post
in the SeqFan mailing list. | 34,405 |
Pripyat (PRIPYAT)
Pripyat is an abandoned city in Ukraine. On April 27, 1986, the whole population of Pripyat was evacuated because of the Chernobyl nuclear power plant accident.
Today (April 26) is the 32nd anniversary of the Chernobyl nuclear disaster, and Duck plans to visit Chernobyl to know more about this disaster. He has a list of
N
must-visit places in Pripyat, with its excitation level
EXC
, time required to visit
VT
and radiation level
RL
. Of course, sometimes it is impossible to visit all places because of limited visiting time and the danger of radiation. Given his maximum of available time to visit
MVT
and tolerance to radiation level
TRL
, can you help him to select some places to visit so that the total EXC is maximized and the VT and RL are less than or equal to the given limit.
Based on your finding, given a matrix
MX
showing some barriers that are not allowed to pass through, Duck's hotel location and all must-visited places' location, determine the minimum distance to be moved to visit all the selected places starting from his hotel location. Duck can only move to adjacent cells that share one edge horizontally or vertically, and he can only visit the selected place once. That is, he cannot pass through the selected place twice or more. Also, he cannot pass through the unselected places.
Input
The first line is the number of test cases
T
. (1 ≤ T ≤ 25)
For each test case, it starts with two integers: number of must-visit places
N
(1 ≤ N ≤ 20) and his maximum of available time to visit
MVT
(1 ≤ MVT ≤ 100) , and one real number: tolerance to radiation level
TRL
. (0.01 ≤ TRL ≤ 10)
Following N lines, each showing ith place's information, consisting of two integers: excitation level
EXC
(1 ≤ EXC ≤ 100) and time required to visit
VT
(1 ≤ VT ≤ 100), and one real number: radiation level
RL
. (0.01 ≤ RL ≤ 10)
After that, a line consists of two integers: number of rows of the matrix
R
, and number of columns of the matrix
C
. (1 ≤ R, C ≤ 50)
Next R lines, each has C characters, representing the map of Pripyat
MX
. '+' means hotel location, '.' means available cells Duck can walk, '#' means barriers Duck cannot pass through, and N consecutive upper letters counting from A corresponding to ith place, eg. A = 1 (N
1
), B = 2 (N
2
) and so on up to T = 20 (N
20
).
It is guaranteed that R × C ≥ N + 1, and all real numbers are with at most two digits after the decimal point.
Output
One integer indicating the minimum distance to be moved to visit all the selected places starting from his hotel location. If there are multiple combinations, choose the first in alphabetical order.
If no places are selected, output '0'; if it is impossible to visit all selected places, output '-1' (without quotes).
Example
Input:
2
5 8 0.8
3 1 0.04
9 9 0.1
4 2 0.12
10 5 0.2
7 2 0.02
8 10
.B...#....
...#.+.#.C
..A.....#.
..##.##.##
....#..E..
#.........
#.....##..
#..#..D...
5 18 1.6
8 6 0.04
9 9 0.1
4 5 0.12
10 5 0.2
3 1 0.02
8 10
.B...#....
...#.+.#..
........#.
..########
....#...DE
#.......##
#.##..###A
...#..C...
Output:
17
-1
Explanation
In test case 1, A, D, E are selected and the minimum distance to visit all of them from '+' is 17.
In test case 2, A, C, D, E are selected. But D blocks E and C blocks A, it is impossible to visit all selected places once only. Duck has to visit C twice or D twice. | 34,406 |
Disconnected country (DISGRAPH)
Cities of the ancient country GRAPH connected by two-way (bidirectional) roads so that from any city you can get to any other city. The Sultan wants to destroy some roads and divide the country into two separate (disconnected) areas, so that from the city of one area it was impossible to get to the city of another area. You are the Minister of transportation and your task is to minimize the number of roads that need to be destroyed. You have a lot of time and the Sultan hopes :-) that you will solve this task.
Input
The first line of input will contain one integer number
8 ≤ N ≤ 1400
, number of cities in GRAPH. Follow
N
lines. Each line represents cities (direct neighbors) connected to the city number
i
(cities numbering is zero based) by one road. There will be 7 input files.
Output
One integer number. The minimum number of roads that need to be destroyed.
Example
Input:
8
1 2
0 2 3
0 1 3
1 2 4
3 5 6
4 6 7
4 5 7
5 6
Output:
1
Example explanation
Destroy only one road from the third city to the fourth city and Sultan will be happy. | 34,407 |
Profiling (PROFILING)
Profiling: In software engineering, profiling ("program profiling", "software profiling") is a form of dynamic program analysis that measures, for example, the space (memory) or time complexity of a program, the usage of particular instructions, or the frequency and duration of function calls. Most commonly, profiling information serves to aid program optimization.
- Wikipedia
Arthur and Jacob are newly introduced to the programming world and they are trying hard for Newbies contest. Yesterday, they learned about recursive functions and Fibonacci sequence. They tried to implement the function themselves so they wrote the following code:
int fibonacci (int N)
{
if(N < 2)
return N;
return fibonacci(N - 1) + fibonacci(N - 2);
}
But this program works slowly when N is a large number. They traced the program and found the cause of this problem. Take a look at the following picture:
If you want to calculate Fibonacci(6), Fibonacci(3) will be calculated multiple times!
They want to know how serious this problem can be, so they need a profiler to calculate such a thing for them.
Your task is to provide the profiler which receives two integers N, K and tells them if they call Fibonacci(N) how many times Fibonacci(K) will be calculated (according to their code).
Input
The first line of input indicates the number of test cases (There will be at most 100 test cases)
For each test case, there are two space-separated integers N, K in a single line. (0≤N,K≤10
5
)
Output
For each test case, print the number of times Fibonacci(K) will be calculated, if we call Fibonacci(N). Since the result can be very large, print the result modulo (Mod %) 1000000007.
Example
Input:
5
6 6
6 3
6 2
100000 3
5 10
Output:
1
3
5
855252772
0 | 34,408 |
Galactic Division (GALACDIV)
The scientists of UEA (Universal Environment Association) are currently researching the outer space, they found out that a plane in the space had some interesting properties on a set of planes based on its
balance factor
. The
balance factor
is equal to the absolute value of the differente between the sum of distances of the planets which are on one side of the plane and the sum of the distances of the planets which are on the other side, the distance of a planet to the plane is calculated as the distance from the center of the planet to the plane.
The scientists are marveled with the results they found when the
balance factor
of a plane is minimal, but as they are manually calculating the
balance factor
, when the set of planets become larger, they find it harder to calculate. So they hired you to build a program that given a set of
N
planets and a 3D vector
(vx, vy,
vz)
returns the coefficients of the general equation of the plane that has minimal
balance factor
and is normal to the vector given.
Input
The input contains several test cases. The first line of a test case contains an integer
N
(1 ≤
N
≤ 10
6
), indicating the number of planets. The second line contains three integers which are the components
vx, vy
e
vz
of the vector (-10
3
≤
v
x
,
v
y
,
v
z
≤ 10
3
). Each of the following
N
lines contains three integers
X
,
Y
and
Z
(−10
4
≤
X
,
Y
,
Z
≤ 10
4
), representing the position (
X
,
Y
,
Z
) of the center of a planet. There won't be a vector which components are
vx = vy
=
vz
= 0.
Output
For each test case in the input your program must produce a single line containing four integer which are the coefficients
a
,
b
,
c
and
d
of the general plane equation (
a
x+
b
y+
c
z=
d
). As there may be several planes that meet the specifications, print the coefficients which absolute values are the smallest possible and
a
,
b
and
c
have the same sign as
v
x
, v
y
and
v
z
, respectively.
Example
Input:
1
1 2 3
0 0 0
2
0 1 0
0 0 0
2 2 2
Output:
1 2 3 0
0 1 0 1 | 34,409 |
Allowed Factors (ALWFACT)
Chico told his students that now they would be referenced by numerical codes of up to 12 digits in official communications (e-mails and tasks). And then he gave to each one of his students a card containing a unique number written on it. Quickly the students assumed that this would be their code, but to the surprise of the students and Levi's despair, Professor Chico explained that these were not their code.
A student's code was the term of an ordered sequence
S
that was in the position (indexed from 1) specified by the number on the card of each student. This sequence has a special feature: each term, when decomposed into prime factors, can only have numbers contained in a set of
N
elements written on the board by the teacher. And to make life even harder for Levi, those numbers would change every week in such a way that he will always have to recalculate his code if he doesn't want to delay his tasks.
Your task is to make a program to help Levi, so that given the prime numbers written on the board during the week by Professor Chico and number on the card, tell his weekly code.
Input
The input consists of several test cases. The first line of a test case contains two integers
N
(1 ≤
N
≤ 10
2
) and
M
(1 ≤
M
≤ 10
5
) representing respectively the number of numbers written by Professor Chico and the number written on the card. The second line contains
N
prime numbers
P
i
(2 ≤
P
i
< 10
6
), where
P
i
(1 ≤
i
≤
N
) is a number written in the board. The entry ends when
N
=
M
= 0.
Output
The output consists of one line per test case containing the Levi's weekly code.
Example
Input:
2 1
2 3
2 10
2 3
3 10
2 3 5
3 10
3 7 13
0 0
Output:
2
24
15
81 | 34,410 |
Candy pair (CANPR)
A candy seller is selling N candies. Each candy is labelled with a positive integer ai from 1 to N.
He is a little orthodox and considers some positive integer L as his Lucky number. Apart from that, he sells candies only in pairs. He would sell two candies ai and aj only if the greatest number which divides both ai and aj is his lovely number L. To promote his candy business, he has come up with an offer to customers. He would give some candies for free to the first customer who can exactly tell the total possible number of ways W in which candy seller can sell one pair of candies among the N available. The customer is required to adhere to his selling policy as mentioned above.
We want you to grab the opportunity to win the free candies by telling the number of ways W.
Input
First line of input contains a positive integer T(1<=T<=100000) i.e. the number of test cases to follow.
Then T lines follow, containing two positive integers N (1<=N<=1000000) and L (1<=L<=1000000) i.e. number of candies and lucky number respectively for each test case.
Output
For every test case, output a single non-negative integer W.
Note: Pairs (x, y) and (y, x) are considered different and both will contribute to W.
Example
Input:
2
5 1
3 2
Output:
19
1
Test case 2: (2, 2) is the only possible pair. | 34,411 |
So Many Squares (SQRPERF)
Juliany is a young programmer in love with math. And as such, it has your favorite numbers. She loves perfect squares, is completely fascinated by them and their gorgeous properties. So she invents several games and hobbies related to them.
One of the games Juliany invented was the insane "So Many Squares" which she plays every day in the intervals of job with her colleague Felipe. The game consists of Felipe choosing
N
natural numbers, and from these numbers, Juliany has to say how many different ways she can choose some, or possibly all, of these numbers in such a way that their multiplication is a perfect square. Obviously the job of thinking about so many numbers and still wondering if your colleague's answer is correct is not as exciting for Felipe as for Juliany. For this reason and to facilitate his work, he simply chooses a small group of prime numbers, at most 50, and generates the
N
numbers for the game from those primes. The generated numbers have only prime factors belonging to this group.
Your task is to help Felipe. Since he has already generated the numbers, you should make a program that checks whether Juliany's answer is correct. Since this can be a very large number, your answer should be only the modulo of that number by 10
9
+ 7.
Input
The input is composed of several test cases. The first line of a test case contains an integer
N
(1 ≤
N
≤ 10
4
) as described above. The next line contains
N
integers
Ai
(1 ≤
Ai
≤ 10
6
) representing the numbers generated by Felipe. The input ends when
N
= 0.
Output
The output consists of one line per test case containing a single integer representing Juliany's answer modulo 10
9
+ 7.
Example
Input:
3
2 4 8
4
14 15 35 7
5
6 42 105 63 20
0
Output:
3
0
1 | 34,412 |
Divisible Strings (DIVSTR)
Mathematicians have always loved generalizing mathematics to everything. They even contributed lots of optimizations and valuable formulas to Computer Science. Have you heard about String Multiplication? What do you think will happen if we write the following code in python?
print (3 * “abc”);
As you might have guessed, it prints “abcabcabc”. It is equal to
print(“abc” + “abc” + “abc”);
We define string S is divisible by string T, if there is some non-negative integer k, which satisfies the equation S=k*T .
Your task is simple. Given two strings S and T. What is the minimum number of characters which should be removed from S, so S is divisible by T?
Input
The first line of the input contains Q the number of the test cases. (1 ≤ Q ≤ 100)
Each test case consists of two lines.
The first line contains string S consisting of lowercase English letters. (0 ≤ |S| ≤ 10
4
)
The second line contains string T consisting of lowercase English letters. (0 < |T| ≤ 10
4
)
Output
For each test case print a single integer, the minimum number of characters which should be removed.
Example
Input:
4
babbaba
ab
dictate
acid
abc
p
q
Output:
3
7
0
1 | 34,413 |
ZERO TRIPLET (THRSUM)
The general elections are coming and so Rahul and Modi have started the preparations. An astrologer predicted that whoever solves the given problem first will win the elections.
The problem is as follows: Given an array
nums
of
n
integers, are there elements
a
,
b
,
c
in
nums
such that
a
+
b
+
c
= 0? Find all unique triplets in the array which gives the sum of zero.
As I want Modi to win, so you have to solve this problem for Modi before Rahul does... Good Luck
Input
In the first line, you will be given t the number of test cases. For each test case you will be given n, the size of the array. In the next line you will be given n space separated elements of the array.
Input constraints: t <= 10000, n <= 10000 (sum of n over all test cases won't exceed 10^8)
Output
Output the number of such triplets and subsequently print each of the triplets in a separate line in lexicographic order.
Example
Input:
2
5
-1 0 1 2 -1
3
0 0 0
Output:
2
-1 -1 2
-1 0 1
1
0 0 0 | 34,414 |
MultiSort (MULSORT)
Members of the famous ENSI Competitive Programming Club try to gather a huge database of problem sets. They need to sort all the problems according to their difficulty levels. Each of the M club members takes N problems and brings back the sorted list. The score (difficulty level) is a real value evaluated as a combination of many criterions. Write a program that outputs the global sorted list of N×M problems.
Input
First line of input consists of an integer T denoting the number of test cases. Then T test cases follow. Each case begins with two lines containing two integers M and N (N×M ≤ 10
6
). Each of the M next lines contains N space separated real numbers (scores) in descending order. All the scores are guaranteed to be in [0, 10].
Output
A single line for each test case consisting of N×M space separated problems. Each problem is represented by i,j where i is the input member position (from 1 to M) and j is the position in the original list (from 1 to N). Problems are sorted by score (descending order) then by member's position (ascending order) and finally by problem's position in the original member’s list (ascending order).
Input File is large. Use fast I/O methods.
Example
Input:
1
3
4
9.195 4.17 2.532 0.03
8.28 5.5 4 2.69
8.8320 7.9 2.18 0
Output:
1,1 3,1 2,1 3,2 2,2 1,2 2,3 2,4 1,3 3,3 1,4 3,4 | 34,415 |
Prime Lover Finding (PLOVER)
A number is called prime-lover, if sum of its digits in base-3 is a prime number. For example 2, 6, and 31 are prime-lovers, sum of digits of all these numbers are prime.
2=(2)
3
, 6=(20)
3
, 31=(1011)
3
Checking whether a number is prime-lover or not is too easy for this contest, so solve this problem:
Given two integers N, K. Can you calculate K’th smallest prime-lover which is not greater than N?
Input
The first line of input indicates the number of test cases (There will be at most 1000 test cases)
Each test case consists of two space-separated integers N, K. (1 ≤ N, K ≤ 10
13
)
Output
For each test case, print the answer to the problem. If there is no such number, print -1.
Example
Input:
3
10 3
10 6
10 7
Output:
5
10
-1
Prime-Lover Numbers not greater than 10 are:
2, 4, 5, 6, 7, 10 | 34,416 |
Lord of Light (PKPLOL)
Nobel the Littlefinger is a follower of the Lord of Light. So he likes
7-segment display
so much because that works with LED lights.
This is a model of
7-segment display
.
And this is how it works.
There are 7 LED light in a 7-segment Display. These are
a, b, c, d, e, f, g
.
We can display any digit from 0 to 9 with these lights.
For example, when
a, b, g, c, d
lights are on that means
3
is displayed.
When
a, f, e, d, c, g
lights are on that means
6
is displayed.
This way
0, 1, 2, 3, 4, 5, 6, 7, 8, 9
can be displayed with a 7-segment Display.
This is a very easy problem. You will be given a digit from 0 to 9 and you just have to tell the list of which lights should be on to display that digit.
Input
Input starts with an integer
T
denoting the number of test cases. Each case contains an integer
N
.
Constraints
T
≤ 1000
0 ≤
N
≤ 9
Output
For each case, print the case number and the list of the lights.
You must print the list in
lexicographical order
in other words
dictionary order
. That means if
a
and
b
are a list,
a
must come before
b
. For digit '
6
' the list will be
acdefg
, not
afedcg
. For digit '
9
' the list will be
abcdfg
, not
acbfdg
.
See the samples for further details.
Example
Input:
3
1
2
3
Output:
Case 1: bc
Case 2: abdeg
Case 3: abcdg
[ Original setter of this problem Pritom Kumar Paul, RUET ] | 34,417 |
Tama Starks Prime Apprenticeship (NASPRIM)
Tama Stark loves to learn new things. He took the apprenticeship of Shakil Tarly who is a master of CP3. His master recently taught him about prime numbers.
A prime number is a number that is only divisible by 1 and itself (i.e. 2, 3, 5, 7, 11 ...).
Then Tama Stark started learning about many different properties of prime numbers. But after a while he began to think that he had learned everything about prime numbers. So his master decided to test him with the following problem:
Given the boundaries
L
and
R
of a range, answer how many
k-pairs
are there? A k-pair is defined as a pair of numbers
(p, p+k)
where
p
and
p+k
are both prime numbers.
In this problem, (p, p+k) is a k-pair between the boundary of L and R if both p and p+k are prime numbers and inside the boundary of L and R (inclusive).
For example, if the L = 10 and R = 20 and k = 2 then there are two k-pairs (11, 13) and (17, 19) and if k = 4 in the same range then there is only one k-pair (13, 17). Note that (7, 11) and (19, 23) pairs are not within the boundary of the range and thus not k-pairs in this case.
Tama Stark is currently busy with his new interest python charming! But he also wants to continue his apprenticeship to Shakil Tarly. Can you help him solve the problem?
Input
First line of the input will contain an integer
T
, the number of test case. T lines will follow. Each line will have three space separated integers
L R k
where L and R denotes the boundary of the range and k is the difference between the numbers of k-pair.
Constraints
T ≤ 300
1 ≤ L, R, k ≤ 10000000
Output
For each test case, output a single integer N in a line where N is the number of k-pairs as described in the problem stated above.
Example
Input:
2
10 20 4
10 20 2
Output:
1
2
[ Original setter of this problem is Nasif Mahbub, RUET ] | 34,418 |
Mysteriousness of a Logical Expression (MYLOGEXP)
Cho decided that it is time to find the love of his life. After conducting intensive online research, he found out that to stand a chance, he has to be intelligent, handsome, kind, charismatic, confident, funny, responsible, reliable, straightforward, mysterious, a gentleman.....
Cho was puzzled. He thought he already had all of these characteristics. After taking a definitely legitimate online personality test, he realized he lacked just one of them - he was not mysterious enough.
He decided he will only say statements with as many interpretations as possible; that way it will always be unclear what he actually means, and that should grant him an aura of unpredictability and mysteriousness.
Cho prepared some pickup lines, and now he wants to know how mysterious they sound - that is, how many ways they can be interpreted to still make sense.
Input
Each pickup line can be represented by a logical expression. A valid logical expression
exp
can be
exp = X
exp = OR(exp,exp)
exp = AND(exp,exp)
exp = NOT(exp)
where
X
is a boolean variable, and
OR
,
AND
,
NOT
are
basic boolean operations
.
The first line contains an integer
1 ≤ T
≤ 6
, the number of expressions.
Each of the
T
subsequent lines contains one valid expression as described above (i.e. it is correctly parenthesized, with no additional spaces).
|exp|
≤ 700,000
and the sum of
|exp|
in an input file
≤ 1,850,000
Output
For each expression, find the number of ways that True/False values can be (independently) assigned to the variables
X
in the expression so that the whole expression evaluates to True.
Since this value could be huge, output it modulo
10
9
+7
.
Example
Input:
2
AND(X,OR(X,X))
NOT(OR(X,X))
Output:
3
1 | 34,419 |
Stopping-off Cities (STOPCITY)
You work for a tour operator which plans to commercialize different visiting tours in a country. A tour is a sequence of cities that are visited during the trip. A city cannot be visited more than once in one tour. Each city is represented as a node in a non-oriented graph where edges are possible connections. Given a departure city A and a destination city B, we call stopping-off-city a city that is part of at least one possible tour between A and B. Your mission is to select all possible stopping-off-cities between A and B. In the example of the figure bellow, we have a graph of 20 cities. If we consider the departure as node 10 and the arrival as node 16 the stopping-off-cities are {8, 9, 10, 11, 12, 13, 15, 16}.
Input
First line of input consists of an integer V denoting the number of nodes or cities (V ≤ 10000). Then, each line contains an edge definition as two space separated integers (link between two cities). Edges description ends with the line "-1 -1" (without quotes). The last line contains two space separated integers "
s d
" where
s
is the departure city and
d
the destination city.
Output
A space separated sorted list of stopping-off-cities including
s
and
d
. It is guaranteed that at least one path exists between
s
and
d
.
Example
Input:
20
0 1
1 2
2 3
3 4
4 5
5 6
6 7
1 8
8 11
8 9
9 10
11 12
9 12
12 13
12 15
12 14
14 19
14 18
14 17
17 18
18 19
13 15
15 16
-1 -1
10 16
Output:
8 9 10 11 12 13 15 16 | 34,420 |
Apoorv Loves Primes (SAS003)
Given two arrays A and B of size n and x. Apoorv is given an empty array P. He has to fill the array according to the following conditions:
for each i in range (0 to x-1) {
if b[i] is negative
(insert the subarray from A[abs(B[i]] to A[n-1] in P at the end)
else
(insert the subarray from A[0]to A[B[i]] in P at the end)
}
Since Apoorv loves Prime numbers He wants to know the Kth prime number in P after the above operation is completed.
So given q queries Apoorv has to report the kth prime number in it. If kth prime doesn't exist print -1.
Note: Both A and B are 0 indexed. abs stands for absolute value.
Constraints:
1 ≤ n ≤ 100000
1 ≤ x ≤ 100000
1 ≤ A[i] ≤ 1000000
0 ≤ abs(B[i]) ≤ n-1
1 ≤ q ≤ 10000
1 ≤ k ≤ 10000000000
Input
First line will contain n size of A.
Second line will contain n space separated integers denoting A[i].
Third line will contain x denoting size of B.
Fourth line will contain x space separated integers denoting B[i].
Fifth line will contain q denoting number of queries.
Sixth line will contain q space separated integers denoting k.
Output
Print q lines denoting output for each query.
Example
Input:
3
2 3 4
1
2
3
1 2 3
Output:
2
3
-1
Explanation
P is [2, 3, 4] so for k=1 answer is 2, for k=2 answer is 3, for k=3 answer=-1 because 3rd prime number doesn't exist. | 34,421 |
Tetrahedrons in the country (TTRGRAPH)
Today we continue examine topology of the ancient country GRAPH. It was said that any four cities form a
tetrahedron
(or 4-vertex
clique
) if from every city of the tetrahedron there is a road to another tetrahedron city. In the picture below is an example of tetrahedron.
Theoretical note: all test cases are
Erdős–Rényi
connected low density graphs.
Your task is to find the number of tetrahedrons in the country.
Input
The first line of input will contain one integer number
4 ≤ N ≤ 900
, number of cities in GRAPH. Follow
N
lines. Each line represents cities (direct neighbors) connected to the city number
i
(cities numbering is zero based) by one road.
Output
Print number of tetrahedrons in the GRAPH.
Example
Input:
4
1 2 3
0 2 3
0 1 3
0 1 2
Output:
1 | 34,422 |
A New Task for Ibrahim (IBIGAME)
One day Rashad and Mahmud were talking about Nim and other mathematical games while teaching game theory to their students. Ibrahim, as usual, solved all problems after the lecture, so teachers decided to give a hard task to him:
Ziya the ProGamer (yes, you know him from my
this problem
) and Rafael a.k.a. Farael are playing a game with two piles that have
A
coins in first one, and
B
coins in the latter. Of course, Ziya starts first and they take alternating turns making the following moves: The player whose turn is; picks one pile with
at least two
coins, removes from that pile
x
coins for some
2 ≤ x ≤ 4
, and adds one coin to another pile. Players can choose different
x
in each move, and who cannot make move loses. As you know, Ziya is ProGamer (don't confuse with Programmer!!), and Rafael is Farael (that special name helps him to make the best move possible!!)
they both play perfectly.
Ibrahim thought about a dynamic programming solution for this task but the constraints for the task baffled him. The only idea in his mind is observing the answer, however, he thinks that his solution is not enough for this task, so your help is needed here. Please, answer some queries that Ibrahim wants to know.
Input
The first line of the input consisting of the number of the queries Ibrahim will ask -
T (1 ≤ T ≤ 5000).
For each query, there are given two numbers,
A
and
B
(1 ≤ A, B ≤ 10
18
; a little long game, huh?)
in a separate line.
Output
For each query of Ibrahim, print "Ziya" if Ziya will win the game, "Farael" otherwise, in separate line.
Example
Input:
3
1 5
2 3
4 12
Output:
Ziya
Farael
Farael
Note: Please, read the statement again (23.06.2018). | 34,423 |
Sharmeen Loves Mozahid Loves Sharmeen [ HARD ] (MOZHSLM)
Mozahid and Sharmeen loves each other and spend a lots of time together. One day they found a string which contains only lowercase English letters. As Mozahid loves Sharmeen very much he likes all ‘s’ in the string. On the other hand as Sharmeen loves Mozahid very much she likes all the ‘m’ in the string. Sharmeen always want to stay on both sides (left and right) of Mozahid so that no other girl can take him away from her :P. So, this time Mozahid gives her a task. Mozahid told her, if she can tell him how many different subsequence of ‘
sms
’ exist in that string, he will ensure her that no girl will take him away from her. As Sharmeen is not a programmer, she needs your help to perform this task. Can you do this for her?
Input
In the first line given an integer T (1 ≤ T ≤ 10), the number of test cases.
In each test case given a string S of size N (1 ≤ N ≤ 10
5
) of lowercase English letters.
Output
For each test case print the number of different subsequence of ‘sms’ exist on that string in one line. For clearance ‘skmjssm’ has 2 different subsequence of ‘sms’. {1, 3, 5} and {1, 3, 6} (1 based position). For better understanding see the sample input output.
Example
Input:
2
asdfmnsmdsms
ssmssmjm
Output:
10
4
[ This problem originally written by MD. Mozahidul Islam Bhuiyan (kissu_pari_na) ] | 34,424 |
Can Sharmeen Solve it? [ HARD ] (MOZHCAN)
Somehow Sharmeen solved the last problem “
Sharmeen loves substring
” and Mozahid became impressed on her performance. Now Mozahid wants to test her programming skill and gives her the hardest problem of today’s problem set. He will give her a string of lowercase English letters of size N (1 <= N <= 10^5) and an integer X (0 <= X <= 10^12). Sharmeen has to find the largest substring of that string, which has exactly X subsequences of ‘
sms
’. If multiple solutions exists, she has to select the leftmost one. If no solution exists, she has to print “-1” (without quotes); otherwise, she has to print the starting and ending position of the substring separated by a space in one line. For exact output format, see the example.
N.B.
Substring is a consecutive sequence of characters of a string, whereas subsequence does not necessarily need to be consecutive. But for both, you have to maintain the order. For clearance, ‘skmjssm’ has 2 different subsequences of ‘sms’. {1, 3, 5} and {1, 3, 6} (1 based position).
Input
In first line given number of test cases T (1 <= T <= 10).
For each test case, a string of lowercase English letters of size N (1 <= N <= 10^5) and an integer X (0 <= X <= 10^12) are given, separated by a space.
Output
For each test case, if solution exists, print the starting and ending position of the substring (1 based position) separated by a space, otherwise print “-1” (without quote) in one line .
Example
Input:
2
smsmmsms 1
mmsm 1
Output:
1 5
-1
[ This problem originally written by MD. Mozahidul Islam Bhuiyan(kissu_pari_na) ] | 34,425 |
Sharmeen Loves Substring [ HARD ] (MOZHSLS)
After solving the problem “
MOZHSLM
” Sharmeen become familiar with subsequence and somehow become interested in substring. Instead of learning more about substring she started asking some ludicrous questions to Mozahid about substring. Become tired of answering Sharmeen’s ludicrous questions Mozahid gives Sharmeen another problem which is slightly harder than the previous one. Mozahid will give Sharmeen a string of lowercase English letters and some queries. In each query he will give her a substring of that string. Sharmeen has to answer how many different subsequence of ‘
sms
’ exists in that substring. Can you help Sharmeen solving this problem?
Input
Given a string of lowercase English letters of length N (1 ≤ N ≤ 10
5
) in first line. In the second line given an integer Q (1 ≤ Q ≤ 10
5
), which is the number of queries. In each query you will be given two integer L, R (1 ≤ L ≤ R ≤ N). L is the starting position of the substring and R is the ending position of the substring (1 based position).
Output
For each query you have to output an integer in one line which is the number of different subsequence of ‘sms’ in that substring.
N.B.
Substring is a consecutive sequence of characters of a string. Where subsequence not necessarily need to be consecutive. But for both you have to maintain the order. For clearance ‘skmjssm’ has 2 different subsequence of ‘sms’. {1, 3, 5} and {1, 3, 6} (1 based position). For better understanding see the sample input output.
Example
Input:
sasmasamsas
3
1 6
3 9
8 11
Output:
2
4
0
[This problem originally written by MD. Mozahidul Islam Bhuiyan(kissu_pari_na)] | 34,426 |
Taming the Dragon (DRAGKING)
The King of DragonStone is known for his dragons and magic. It can be very hard and dangerous to tame a dragon. So, in order to control his dragons The King speaks random but powerful words. The strength of his words is determined by a strange rule. Rule : Strength of a word is sum of cube of length of palindromic substrings in the word. To recall the definition of palindrome, palindrome is a word which reads same when read forward and backward. For example, word "abaa" has 6 palindromic substrings. "aba", "aa", "a", "b", "a", "a" with sizes 3, 2, 1, 1, 1, 1 respectively. Their cubes are 27, 8, 1, 1, 1, 1. Hence total strength is 39. Given a King's word, find its strength.
Input
First line of input contains an integer T, the number of test cases. Followed by T lines, each line containing a word (say S). (only lowercase letters are allowed).
Constraints:
1 ≤ length of word ≤ 1000
1 ≤ T ≤ 100
Output
For every word (S), output a single line containing the strength of the word.
Example
Input:
2
abaa
dbd
Output:
39
30
Note: There is no space between lines | 34,427 |
Palindrome Lover (PL)
Asad is a 10 years old boy. He loves to learn new logic from computer science.
Palindrome
is an interesting topic to him. So, He starts practicing to finding palindrome in his everyday life.
A palindrome is a word, phrase, number, or other sequence of characters which reads the same backward as forward. such as – madam, 0001000 etc.
He faces a problem and asks for your help. You are a great programmer in our country.Now, you have given a string
S
. You have to do a permutation of string
S
through many times (can be 0 times) such that you will get a palindromic string
P
. You have to check whether it is possible to form a palindrome after any permutation or not.
Input
Every line of the input contains a single string
S
.
Constraints:
S
will consist only of lowercase English letters (i.e. characters 'a' through 'z').
1 ≤
|S|
≤ 10
5
Output
For each test case, print a single line. Print 1, if it is possible to find at least one valid permutation
P
of string
S
which is a palindrome. Otherwise print -1.
Example
Input:
abc
abab
Output:
-1
1
Note:
In, Case 2: ‘baab’ is a valid permutations of string ‘abab’ which is also a palindrome. So output will be 1. | 34,428 |
Pizza Prize (PPR)
Asad, Foyj, Juwel, Mijan, Tanmay
are friends from group "Mission Starts". They are in the queue for pizza in a bakery. No other people is in the queue. A person who takes one pizza can get two chances to get pizza at the end of queue.
Explanation:
Initial Chances queue: Asad, Foyj, Juwel, Mijan, Tanmay.
After taking 1st pizza the Chances queue: Foyj, Juwel, Mijan, Tanmay, Asad, Asad.
After taking 2nd pizza the Chances queue: Juwel, Mijan, Tanmay, Asad, Asad, Foyj, Foyj.
And so on……..
But the shopkeeper wants to offer a special prize to a person who have the
N
th pizza. In this case, He asks for your help. You are a great programmer in our country. Your task is to find the name of the lucky winner who have the
N
th pizza (the queue only contain these 5 peoples and their order should be maintained strictly).
Input
Every line of the input contains a single integer
N
denoting the lucky pizza number.
Constraints
1 ≤
N
≤ 10
9
Output
For each test case, print a single line and print the name of the lucky winner.
Example
Input:
1
2
3
Output:
Asad
Foyj
Juwel | 34,429 |
NARUTO AND HILLS (NARHIL)
There are
n
hills in Konohagakure, the Hidden Village of the Land of Fire.
Naruto is standing on the peak of 1
st
hill and wants to see the peak of the n
th
hill. He can see the peak of hills ahead of him which have a height smaller than or equal to the one he is standing on, up to the next strictly taller hill. Naruto has the ability to jump from one hill to another. He can also use his chakra to cut a hill and reduce its height.
You are given two arrays
A
and
H
. Array
H
represents the height of hills.
The energy used in jumping from i
th
hill to j
th
hill is
2 times
the absolute difference of
A[i]
and
A[j].
If Naruto is standing on i
th
hill, he can cut any hill from (i+1)
th
to n
th
which has a height greater than i
th
hill. He can only reduce the height of a hill up to the height of i
th
hill. The energy used in cutting j
th
(i < j ≤ n) hill the absolute difference of
H[i]
and
H[j]
.
You have to tell the minimum energy which will be used in order for Naruto to see the n
th
hill.
Note
:
Naruto can’t jump on the n
th
hill nor can he cut the n
th
hill.
Constraints
1 ≤ t ≤ 20
2 ≤ n ≤ 10
5
1 ≤ A[i] ≤ 100000
1 ≤ H[i] ≤ 100000
Input
The first line of the input contains a single integer
t
denoting the number of test cases. The description of
t
test cases follows.
The first line of each test case contains a single integer
n
denoting the number of hills.
The second line contains n space-separated integers
A
1
, A
2
, A
3
... A
n
.
The third line contain n space-separated integers
H
1
, H
2
, H
3
... H
n
representing the height of i
th
hill.
Output
Your program should print one line of output for each test case.Output minimum energy which will be used in order for Naruto to see the n
th
hill. If it is not possible for Naruto to see the peak of n
th
hill then output -1.
Example
Input:
3
5
1 7 6 12 8
2 9 4 3 1
7
3 8 6 7 19 18 9
1 3 5 2 10 4 5
6
2 7 4 5 9 3
1 2 3 4 5 6
Output:
10
11
-1
Explanation:
Test case 1: Naruto can jump to 3
rd
hill from where he can see the peak of 5
th
hill. Energy= 2 * |1-6| = 10.
OR
Naruto can cut 2
nd
, 3
rd
, and 4
th
hill without jumping anywhere. Energy = |2-9| + |2-4| + |2-3| = 10.
Test case 3: Naruto cannot see the peak of 6
th
hill whatever he may do. | 34,430 |
NICE SEQUENCES (NICESEQ)
A nice sequence is a sequence of digits in which a digit d is placed at any index iff d is 0 or any divisor of d (except 1) has been placed already. First digit can be anything from 1 to 9.
Find the number of nice sequences of length n.
Input
Input consists of number of test cases t
Following t lines have a single line containing n as described in the problem statement.
Output
Print the number of nice sequences of length n modulo 1000000007 in a separate line.
Example
Input
2
1
2
Output
9
23
Explanation
For n=2 nice sequences are: 10, 20, 22, 24, 26, 28, 30, 33, 36, 39, 40, 44, 48 and so on !
Constraints
1<=n<=1000 | 34,431 |
Jeremías y sus loros (JBIRDS)
The pirate Jeremiah plans to offer a show to his crew. For this, Jeremiah has N parrots, which he plans to balance on his shoulders. Each of the N parrots has a weight, in grams, which Jeremiah knows beforehand.
Jeremiah has only two shoulders, and therefore he must separate the parrots into two groups. The imbalance of Jeremiah will be equal to the (positive) difference in weight between the group that leads on the left shoulder and the one on the right shoulder.
What is the lowest value that the pirate's imbalance can take?
Input
The entry begins with a line containing a single integer, N (0 <= N <= 10000)
The following N lines describe the weights of the parrots. Each line contains a single integer wi (0 <= wi <= 1000)
Output
A line with a single integer, the minimum imbalance (in absolute value).
Example
Input:
4
2 1 5 3
Output:
1 | 34,432 |
Longest palindrome with no adjacent duplicates (LNGPALN)
We are given a string . Determine the longest palindromic substring without any adjacent duplicates.
For example: S="ABBCBBA", longest palindromic substring is "ABBCBBBA" but it contains adjacent duplicates, so the required string is "BCB".
EDIT: If there are multiple such strings then print the lexicographical smallest string.
Input
The first line of input contains a t, the number of test cases and the following line of each test case a string S (1 <= S <= 5000)
Output
Print the required string.
Example
Input:
1
MBBCDCBBM
Output:
BCDCB
NOTE : String will be in uppercase only | 34,433 |
Richie Rich and Keen Bean (ATMCMXNG)
Dr. Keen Bean has great love for chemistry. He has 100 elements with atomic number from 0-99. Dr. Kean Bean wants to react(mix) all elements together. At each turn he mix two elements together placed next to each other and replace the resulting element in their place. When mixing two elements with atomic number a and b, the resulting element atomic number will get modified and will be (a + b) % 100. The dangerous point is while mixing two elements there is production of toxic gas, whose toxicity level is a × b.
Dr. Keen Bean would like to produce very less amount of toxic gas. So he wants you to help him find minimum amount of toxic gas he can produce when mixing all elements together.
Input
The first line of input contains integer N (1 ≤ N ≤ 100), the number of elements. The second line will contain atomic numbers between 0-99. The initial atomic numbers of elements.
Output
Output the minimum amount of toxic gas Dr. Keen bean can produce.
Example
Input:
3
40 60 20
Output:
2400 | 34,434 |
Seven (SE7EN)
David Mills and William Somerset have teamed up again to catch a criminal. This criminal is a bit different from others, he challenges the detectives by leaving a puzzle at the crime scene which is the clue to his next crime. Both the detectives work hard to crack the puzzle, but the criminal is always one step ahead. For his 7
th
and last crime, he left a problem for the detectives. David wants your help for this one to catch the criminal.
The criminal gave you a tree having
n
nodes with
1
as the root. Each node is numbered from 1 to n. Every node has a value represented by the array
A.
You are given another array
B.
You have to convert the node values from A to B by performing the minimum number of operations. In one operation you can select any node
i
and add or subtract any value. And the same value will get added to or subtracted from the special node in the subtree of the node
i
.
Special node
q
in the subtree of
p
is defined as node such that:
(Number of set bits in p) mod 2 = (Number of set bits in q) mod 2
You have to tell the minimum operations to convert node values from A to B and the sum of values added or subtracted in these operations.
Constraints
1 ≤ t ≤ 11
1 ≤ n ≤ 100000
1 ≤ A[i] ≤ 100000
1 ≤ B[i] ≤ 100000
Input
The first line of the input contains a single integer
t
denoting the number of test cases. The description of
t
test cases follows.
The first line of each test case contains a single integer
n
denoting the number of nodes.
Each of the next n - 1 lines contains two integers
u
i
and
v
i
(1 ≤
u
i
,
v
i
≤
n
;
u
i
≠
v
i
) meaning there is an edge between nodes
u
i
and
v
i
.
Next line has n space-separated integers
A
1
, A
2
, A
3
, ... A
n
denoting the initial value of nodes.
Next line has n space-separated integers
B
1
, B
2
, B
3
, ... B
n
denoting the final values of nodes.
Output
Output the minimum number of operations required and the sum of values added or subtracted in these operations.
Example
Input:
1
8
1 3
3 2
3 7
6 2
4 1
8 4
4 5
9 3 5 6 2 3 8 10
9 3 3 5 2 1 8 10
Output:
3 -2
Explanation
You can select node 3 with value 5 and subtract 2, it will also get subtracted from the special node 6 in its subtree.
Values after 1
st
operation: 9 3 3 6 2 1 8 10
Now select node 4 with value 6 and subtract 1, it will also get subtracted from the special node 8 in its subtree.
Values after 2
nd
operation: 9 3 3 5 2 1 8 9
Now select node 8 and add 1. There is no special node in its subtree.
Values after 3
rd
operation: 9 3 3 5 2 1 8 10
Sum of values added/subtracted = -2-1+1 = -2 | 34,435 |
TWISTED ARRAY (ARRTWIST)
There are two integer arrays
A
and
B
. The length of array
A
is
n
and length of array
B
is
k
. Array
A
=
[ a
1
, a
2
... a
i
... a
n
]
and
B
=
[ b
1
, b
2
... b
j
... b
k
]
where
1 ≤ a
i
≤ k
and
1 ≤ b
j
≤ n
and
1 ≤ i ≤ n
and
1 ≤ j ≤ k
and
1 ≤ k ≤ n ≤ 10
7
. If there exists a subarray of
A
which has the same sum as some subarray of
B
then
B and A are said to be twisted arrays
.
More mathematically, if there exists
p
,
q
,
r
and
s
such that
sum
(
A
,
p
,
q
) =
sum
(
B
,
r
,
s
), where
1 ≤ p ≤ q ≤ n
and
1 ≤ r ≤ s ≤ k
and
sum(A, p, q)
=
a
p
+ a
p+1
+
a
p+2
...
+
a
q-1
+ a
q
and
sum(B, r, s)
=
b
r
+ b
r+1
+
b
r+2
...
+
b
s-1
+ b
s
then the two arrays
A and B
are said to be
twisted arrays
.
Input
Input contains
n + k + 1
lines. The first line has values for
n
and
k
separated by space.
Then next
n
lines specify the elements of array
A
. The next
k
lines specify the elements of array
B
.
Output
One line containing
Yes
if the arrays are
twisted
or
No
otherwise (Note:
Yes
and
No
are case sensitive.)
Example
Input:
4 3
1
2
3
1
2
1
1
Output:
Yes
Explanation:
Here A = [1, 2, 3, 1] and B = [2, 1, 1]. Clearly a
1
+ a
2
= b
1
+ b
2
, and so A and B are twisted | 34,436 |
Lovely Kitty (AVLVKT)
Kitty is my sweet sister. Once in a morning I saw she is watching towards the clock. The clock is an analogue clock with two hands. The first one is an hour hand and the other one is a minute hand. While asking about her such attentive mode she replied “Brother can you tell me what will be the angle between the hour hand and the minute hand ?” I was like “huh!!”. Anyway I solved the problem after a while.
Now here is the task for you. I will give you the angle created between the hour hand and the minute hand. You need to find out the how many organizations of hour and minute hands are there that create such angle. For example if I say 90
o
then the answer will be – 2. The first one is 03:00 and 9:00.
Note that, the angle calculation should be done in clockwise direction but if you figure out an angle greater than 180
o
then deduct 180
o
from it. Because kitty do not understand the calculations of angle more than 180
o
.
Input
Input starts with an integer
T
that denotes number of test cases. Each of the next T lines contains a real number with one decimal place that denotes angle
A
.
Constraints
1 <=
T
<= 2000
1 <=
A
<= 180.0
Output
For each test case print the case number followed by the result according to the following format
Case X: R
where
X
denotes the case number and
R
denotes the result. See the sample for further clarification.
Example
Input:
2
90.0
180.0
Output:
Case 1: 2
Case 2: 2 | 34,437 |
Good Elements (OVGDEL)
You are given a sequence consisting of integers
a
1
, a
2
, a
3
… a
n
. Any element
a
i
is called
good
if there exists another element
a
j
in the sequence (
i ≠ j
) such that
a
j
is a
non-negative integral power
of
a
i
. In other words
a
i
is called
good
if there exists an element
a
j
where
i ≠ j
and
a
j
=a
i
k
for some integer
k ≥ 0
.
For example, consider the following sequence:
[2, 4, 4, 6, 3, 8]
. This sequence contains
3
good elements. The
1st
,
2nd
and
3rd
elements are good.
1st element “
2
” is good because there exists “
4
” and “
8
” in the different positions of the sequence which are non-negative power of “
2
” (
2
2
= 4
,
2
3
= 8
). 2nd element “
4
” is good because there exists another “
4
” in a different position of the sequence which is a non-negative power of “
4
” (
4
1
= 4
). Same applies for the 3rd element.
Given the sequence, now you have to find out
total number of good elements
in the sequence.
Input
The first line contains an integer
t
denoting the number of test cases.
For every test case the first line contains the integer
n
the length of the given sequence. The second line contains the sequence of integers
a[1], a[2], a[3] … a[n]
.
Constraints
1 ≤ t ≤ 10
1 ≤ n ≤ 10
4
1 ≤ a
i
≤ 10
6
Output
For each test case print the case number followed by the result in a single line according to the following format "
Case X: R
" (without quotes), where
X
denotes the case number and
R
denotes the result. See the sample for further clarification.
Example
Input:
3
6
2 4 4 6 3 8
2
1 2
2
10 100
Output:
Case 1: 3
Case 2: 1
Case 3: 1
[ This problem originally contributed by
Hafiz Al Masud Ovi
] | 34,438 |
Billing Issue (NABILISU)
Everyday morning Nabil and Muzahid go to the famous hotel “Ruchi Bilash” and complete their breakfast. But they have a system in it, everyday don’t know how, always Nabil makes a bigger bill. For example: – Nabil made 65 tk and Muzahid made 55 tk. But there is a Funny thing.
Muzahid always gives the waiter tips at least 1 taka
. And for that tips, they both pay equal. If we consider previous example, than Nabil will pay 65 and Muzahid will also pay 65, His bill is 55 and 10 tk tip for waiter.
Note that
Muzahid’s bill never crosses Nabil’s bill
.
Input
You will get an integer
T
, the number of test cases. After that line, there will be T lines as input. You will get
3
integers in every line
a, b, k
as Nabil’s bill, Muzahid’s bill and waiter’s tip.
Constraints
1 ≤
T
≤ 10
1 ≤
a, b
≤ 1000
0 ≤
k
≤ 50
Output
Print “
YES
” (Without Quotes) if it is possible to fill such condition given above. Otherwise, print “
NO
” (Without Quotes). Follow the sample input output formation.
Example
Input:
5
65 55 10
75 55 20
75 55 10
55 75 20
50 50 0
Output:
Case 1: YES
Case 2: YES
Case 3: NO
Case 4: NO
Case 5: NO
[ This problem originally contributed by
MD. Yasir Uddin Nabil
] | 34,439 |
Secret Service Agent (RZSCSRVC)
You all know about famous secret service agent
Masud Rana
. Masud Rana is a fictional character created in 1966 by writer
Qazi Anwar Hussain
. Today he is going to send a message to his colleague
Shohana Chowdhury
in encrypted format.
He likes letter
R
very much, so he encrypt a massage s in following pattern with height
h
(always odd).
If his massage is s = “ABCDEFGHIJKLMNOPQRSTUVWXYZ” and h = 5
Then his message will be like this:
EFG#RST
D#H#Q#U
CJI#PWV
BK##OX
A#LMN#YZ
That’s mean result format have height h=5, and all letter will be in sequentially make big
R
. All other space will be fill up with
#
. Please see the following figure for better understanding.
(Line has drawn for explanatory purpose )
Input
In first there is two integer
n
(1 <= n <= 1000) and
h
(1 <= h <=n, h is
odd
), length of the message and height of output. In next line there is a message
s
, message is a non-empty string consist with only
capital letter
.
Output
Print h line with corresponding answer with describe format.
Note that, there should not have any extra # after last letter of each line.
Example 1
Input:
13 3
PHQGHUMEAYLNA
Output:
QG#YL
HH#AN
PUMEA
Example 2
Input:
22 5
MXWTPTTTYKDUYVXJBZHQUP
Output:
PTT#ZHQ
T#T#B#U
WKY#J#P
XD##X
M#UYV
Example 3
Input:
22 7
MXWTPTTTYKDUYVXJBZHQUP
Output:
TTYK
T##D
P##U
TXVY
WJ###P
X#B##U
M##ZHQ
[ This problem originally contributed by
Reajul Haque Reayz, CSE, CoU
] | 34,440 |
Playing With Subarray (MOZPWS)
Alice loves to play with array of integers. He has an array A[ ] of integers. Bob, friend of Alice is a smart guy. Seeing Alice’s curiousness about array Bob decided to give Alice a task. Before giving the task Bob ask Alice if he knows about K_min_subarray? A K_min_subarray is the minimum value of a K length subarray of array A[ ]. After Bob knows about K_min_subarray, Alice gives Bob Q queries about array A[ ]. In each query she will give two integers L, R. Alice have to answer the largest value of all possible K_min_subarray in between L to R. Here each subarray’s starting position must be >= L, ending position must be <= R and the length of the subarray must be K.
Input
In the first line given t, the number of test cases.
For each test case there will be the following:
In the first line given two integers n (Size of the array) and K (Length of the subarray).
In the second line given the elements (A[i]) of the array.
In the next line given an integer Q (the number of queries).
In the next Q lines are given two integers L, R
Constraints
1 <= t <= 10
1 <= n <= 10^5
1 <= K <= n
-10^18 <= A[i] <= 10^18
1 <= Q <= 10^5
1 <= L <= R <= n
t * max(n, Q) <= 10^5
Here all positions are 1 based.
Output
For each test case you have to print the test case number in one line in the format “Case x:” without quote, where x is the case number.
For each query output the largest value of all possible K_min_subarray in between L to R in each line. If answer is not possible print “Impossible” without quote.
For better understanding see the sample input output and the explanation of sample.
Example
Input:
2
7 3
10 5 15 -5 3 11 2
4
1 4
2 3
3 6
5 7
5 1
1 2 3 4 5
3
1 3
2 5
4 4
Output:
Case 1:
5
Impossible
-5
2
Case 2:
3
5
4
Explanation
Test Case 1:
Query 1:
Here 2 subarray possible of length 3 between positions 1 to 4: {10, 5, 15}, {5, 15, -5}.
Minimum value in {10, 5, 15} is 5 Minimum value in {5, 15, -5} is -5 Maximum of 5 and -5 is 5.
So answer is 5.
Query 2:
Here no subarray is possible of length 3 between positions 2 to 3.
So you have to print “Impossible”.
Query 3:
Here 2 subarray possible of length 3 between positions 3 and 6: {15, -5, 3}, {-5, 3, 11}.
Minimum value in {15, -5, 3} is -5 Minimum value in {-5, 3, 11} is -5 Maximum of -5 and -5 is -5.
So the answer is -5.
This problem originally contributed by
Md. Mozahidul Islam (kissu_pari_na), ICT, CoU | 34,441 |
Meet Her Fast! (ASHMHF)
Dreamerash Lives in Wonderland. He loves a girl named Tania. They live in two different cities. Today Tania told him that she is coming to Dreamerash’s city for a work. She told him that she will stay in a hotel. But unfortunately as their communication link broke up she couldn’t tell him the exact hotel. After her arriving in the city they can again communicate with each other. Since they’ve not seen each other for a long time he wants to meet her as soon as possible after her arrival in the city. So he decides to stay in one of the hotels until he meets her.
There are
n
hotels in the city, all are arranged in one straight line. Dreamerash know the position of every hotel. Positions of every hotel are
distinct
. He wants to stay in a hotel from where,
sum of distance
among all the other hotels is
minimum
. As he is busy dreaming, he wants you to solve this problem for him.
Your task is to find the
index
(starting from one) of a hotel from where sum of distance among all the other hotels is minimum. If there are multiple hotels, print the index of the hotel that appeared in the input first.
Input
First line contains an integer
T
(1 ≤ T ≤ 10) denoting the number of test cases.
For
each
test case there will be following lines:
The first line has an integer
N
(1 ≤ N ≤ 10
5
) denoting the number of hotels.
Next line contain array of N space separated distinct integers
A
[i] (1 ≤ A[i] ≤ 10
9
) denoting the positions of the hotels
indexed from One
.
Output
For each test case print the case number followed by the index of the hotel according to the following format “
Case X: I”
without quote, where
X
denotes the case number and
I
denotes the index of the desired hotel.
For better understanding see the sample input output.
Example
Input:
2
4
1 12 17 9
5
8 20 60 14 32
Output:
Case 1: 2
Case 2: 2
[ This problem originally contributed by
MD. Ashrafuzzaman Khan
] | 34,442 |
Secret Recipe (SKS001)
Harsh and Vishal are besties. Vishal has a secret recipe which would land both of them a job. But he his unwilling to share his recipe.
Both of them are standing on the positive side of x-axis. Harsh is on coordinate i and Vishal on j (i ≤ j). Harsh can make two kinds of moves,
if he is standing on coordinate m he could either move to m+1 or a coordinate n such that n is prime. The cost of jumping is the value of the coordinate on which Harsh jumps.
Vishal would give his recipe only if Harsh reaches him in minimum total cost. Help Harsh out.
Input
The first line of input contains two integers i and j (i ≤ j) as mentioned above.
0 ≤ i, j ≤ 2×10
9
Output
Output a single line containing minimum cost to reach j from i.
Example
Input:
2 4
Output:
7 | 34,443 |
Gift Arrangement (GIFTARNG)
Duck's birthday is coming soon! Unfortunately, this year nobody will come to Duck's birthday party. To make himself feel better, he has decided to display all birthday gifts received last year, pretending many people celebrate with him this year.
There are
N
gifts received last year, each in a rectangular gift box with dimensions {
W
i
(width),
H
i
(height),
D
i
(depth)}. Gift box is very colorful which makes people happy, so Duck will place all of them in a straight line without changing the order and without any gap between two adjacent boxes, while the total visible area is maximized. To achieve this goal, Duck can rotate each gift box in any direction but the box must stand upright on the ground. That is, it cannot be placed obliquely. Please help him figure out the largest visible area.
Input
The first line is the number of test cases
T
. (1 ≤ T ≤ 30)
For each test case, it starts with the number of gifts received last year
N
. (1 ≤ N ≤ 10
3
)
Following N lines, each consisting of three integers
W
i
(width)
H
i
(height)
D
i
(depth), representing the dimensions of the i gift box. (1 ≤ Wi, Hi, Di ≤ 10
5
)
Output
Output the largest visible area.
Example
Input:
2
2
100 100 20
3 4 2
3
1 1 1
5 4 3
8 6 8
Output:
26032
330
Explanation
In case 1, one optimal strategy is to rotate the first box to {100, 20, 100} and the second box to {4, 2, 3}.
In case 2, no rotation for the first box and third box, and to rotate the second box to {5, 3, 4}. | 34,444 |
Beautiful Girl (BGIRL)
KNIT is an engineering college specially famous for CODING. This college has great reputation among many other colleges in our country.
Every boy in KNIT has a crush on a girl who is famous for her beauty and intelligence. So everyone wants to go with that girl for date.
Girl has opportunity to select a single boy. So she decides to go with that boy by which her demand would be fulfilled.
She will go with that guy who is the leader of that group which has maximum number of members and also size of the group must a prime number.
Group is represented as all the members in that particular group are in friendship with each other.
There are N boys in the college represented by 1, 2, 3 ... N.
M shows the number of relation of friendship among N boys. Every relation is in format of A and B i.e. A is friend of B.
Note: If A is friend of B and B is friend of C then A is also friend of C.
Input
First line of input contains a positive integer T (1 ≤ T ≤ 5) i.e. the number of test cases to follow.
Then T test cases follow. The first line of each test case contains two positive integers N (1 ≤ N ≤ 100000) and M (1 ≤ M ≤ 100000) i.e. number of boys and number of relations respectively for each test case.
Next M lines contains two positive integer A and B (1 ≤ A, B ≤ N).
Output
For every test case, output a single integer W in a single line where W is the size of that group which fulfilled the demand of that beautiful girl.
If there is no any group that fulfilled the demand of that girl then print -1.
Example
Input:
1
10 5
1 2
2 3
4 5
5 6
6 7
Output:
3
Explanation: Boy 1, 2, 3 makes a group which fulfilled the demand of that beautiful girl. | 34,445 |
Help Robin!! (CHUNK1)
Ra's al Ghul has attacked Gotham city and wants to kill Batman.
Now you, being Robin, want to help Batman by killing Ra's al Ghul and his army.
Ra's al Ghul has army of soldiers commanded by certain chiefs amongst them. The soldiers are motivated by them and are geared up to follow their instructions on one say.
You have to identify chief soldiers and use them to misdirect maximum number of soldiers. This will reduce the chance of Ra's al Ghul to kill Batman.
Chief soldiers are those who are followed by the maximum number of soldiers.
Once, identified you can play trick (magic) on them and misdirect soldiers following them.
You are given two integers N (denoting number of soldiers) and M (number of relation between 'N' soldiers).
Further, M lines contains two integers l1 and l2 meaning soldier l1 follows soldier l2.
Constraints
1 <= T <= 100
1 <= N <= 9999
1 <= M <= 100000
1 <= l1, l2 <= N
Input
The first line of the input contains a single integer T denoting number of test cases. Description of each test case are as follows:
First line contain integers N (denoting the number of soldiers) and M (number of pairs).
Further, M lines contains two integers l1 and l2, meaning soldier l1 follows soldier l2.
Output
Print the chief soldiers in ascending order.
Example
Input:
1
5 4
1 5
1 2
3 1
4 1
Output:
2 5
Explanation:
1st soldier follows 5th soldier
1st soldier follows 2nd soldier
3rd soldier follows 1st soldier
4th soldier follows 1st soldier
Number of soldiers following 4 = 0
Number of soldiers following 3 = 0
Number of soldiers following 1 = 2 (4th and 3rd soldiers)
Number of soldiers following 2 = 3 (4th, 3rd and 1st soldiers)
Number of soldiers following 5 = 3 (4th, 3rd and 1st soldiers)
Answer is soldiers 2 and 5.
Contributed by: Paras Jain | 34,446 |
Popatlal ki shaadi (CHUNK2)
Popatlal from Gokuldham Society is still not married. He approaches a marriage bureau and ask them to hurry the process. The bureau checks the list of eligible girls (n) and hands it over to Popatlal. Popatlal being conscious about his marriage, determined to find a girl with maximum connections so that he can gather more information about her.
Accordingly, he looks to figure out the maximum number of girls (from list) who know each other to achieve above purpose. In order to finalise the girl, he needs to find the Kth prime. Where k = largest group of girls who know each other.
Considering Popat's poor knowledge in Maths, he seeks for Jethalal's help for the answer. Now you, being fan of Jethalal, take this prestigious opportunity to solve Popat's marriage issue.
In case number of connections are zero, print "-1".
Note: Suppose girl "a" knows girl "b" and girl "b" knows girl "c", then girl "a" also knows girl "c" - transitivity holds.
Consider 1 to be a composite number.
Input
First line of the input contains t, the number of test cases.
Each line of the test case contains a number n specifying the number of girls and m specifying number of connections.
Each 'm' lines contain u and v denoting that girl u and v know each other.
Output
Each new line of the output contains Kth prime number, or -1 if there are no connections.
Constraints
1 <= t <= 100
1 <= n <= 100000
0 <= m <= n
1 <= u, v <= n
Example
Input:
1
10 6
1 2
2 3
3 4
4 5
6 7
9 10
Output:
11
Contributed by: Paras Jain | 34,447 |
Kth Power Summation (KPOWERSUM)
Leeana Learned Few New Things Few Days Ago, Like:
Find The Summation Of Divisors
.
Modular Arithmetic
.
So Now Her Uncle Gave Her A Task.
Task Is: You Will Be Given A Number(N) And Another Number(K).
Now You Have To Find K
th
Power Summation Of Divisors Of N.
Summation Of All Divisors Of
N
Will Be Huge, So You Have To Print The Summation Module (M=1000000007).
Like: Divisors Of 6 is: ( 1 2 3 6 ) And K = 2.
so, summation is: 1^K+2^K+3^K+6^K = 1^2 + 2^2 + 3^2 + 6^2 = 1+4+9+36 = 50 % 1000000007 = 50
Leeana Thinks That You Are A Great Programmer, So She Needs Your Help. Can You Help Her??? :D :D :D
Input
Input Starts With An Integer T (≤ 500), Denoting The Number Of Test Cases. Each Case Contains An Integer N (1 ≤ N ≤ 10
15
) And An Integer K (1 ≤ K ≤ 10
5
) Denoting The Power Of Divisors.
Output
For Each Test Cases, Print The Case Number And The Kth Power Summation Of Divisors Of N Module 1000000007. After Each Case Print A New Line. See Sample Input And Output For Better Explanation.
Example
Input:
4
6 2
6 1
6 4
6 3
Output:
Case 1: 50
Case 2: 12
Case 3: 1394
Case 4: 252
#Extra_Challenge: N<=10^18, T<=1000 TL: 1s | 34,448 |
Shinchan and Magic Card (SHINCARD)
An alien group has attacked Kasukabe so whole city is in big trouble! All people (count is N) in Kasukabe try to run away so is Shinchan! They came across a bridge over a river, and that bridge has been cursed by aliens. Bridge will collapse when someone passes over it.
Shinchan as a legend calls an ultra legend Buri Buri Zaemon by his magic card. Buri Buri appears and suggests Shinchan that whenever a person having magic card in his hand passes over the bridge then it will not collapse but since bridge is fragile only maximum 2 persons at a time can pass over it (and one of them should have magic card in his hand). All citizens of Kasukabe have to go to other side of the river using bridge as quickly as possible. When two persons A and B pass over bridge, it takes MAX(time(A), time(B)) to get them on the other side of river (where time(i) is the time taken by ith person to pass over the bridge). Shinchan has an array of all time[] values of all citizens.
Find the minimum time in which they escape the bridge. (There is only one magic card)
Input
First line contains N, 1 ≤ N ≤ 100000.
Next line contains N integers, 1 ≤ time[i] ≤ 1000000000 for each 1 ≤ i ≤ N.
Output
Output the minimum time to get all persons over the other side of the river.
Example
Input:
4
1 2 5 10
Output:
17
Explanation
1 and 2 cross,
1 comes back,
5 and 10 cross,
2 comes back,
1 and 2 cross.
Total = 2 + 1 + 10 + 2 + 2 = 17. | 34,449 |
A Cumulative Sum Problem (OVICUMSUM)
Given an array a
0
of size
n
(1
<
n
<
10
5
). Find the array a
k
modulo (
7340033
)
where a
k
= cumulative summation array of the array
a
k-1
.
Means,
a
k
[1] =
a
k-1
[1]
for i > 1 ,
a
k
[i] =
a
k
[i-1] +
a
k-1
[i]
Given
k
(0
<
k
<
10
5
)
Can you find the array a
k
efficiently?
For example
,
If a
0
= {1, 2, 1, 3},
a
1
= {1, 3, 4, 7}
a
2
= {1, 4, 8, 15}
a
3
= {1, 5, 13, 28}
Input
First line will contain two integer
n
,
k
(size of the array and
k
from the problem description)
Following n positive integers separated by spaces denoting array a
0
.
All integers are smaller than 10
5
.
Output
Output n integers of the array
a
k
with spaces in between.
Example
Input:
4 2
1 2 1 3
Output:
1 4 8 15 | 34,450 |
Large Sum (OVISLARSUM)
Problem Courtesy: Ovishek Paul
Department of CSE, SUST
Sylhet, Bangladesh.
.........................................................................................................
Bodi is a "hotash" programmer. Sometimes he writes some code that takes 2777778 hours!!!
Good programmers said Bodi to become efficient. But Bodi just can't be. So he writes another code of 2777778 hours! which is below -
long long sum = 0;
for(long long i = L; i<=R; i++){
sum += i % mod;
sum %= (10
9
+ 7);
}
where, 1 <= L <= R <= 10
18
, 1 <= mod <= 10
18
Now Bodi is here to you because Bodi recently have known that you are so efficient!!!
Input
First line will contain 3 integers L, R, and mod.
Output
Just print the final sum.
Example
Input:
4 5 6
Output:
9
Input:
4 10 9
Output:
31 | 34,451 |
Scary Secret Diary (TAHSINREC)
While going through her friend’s secret diary, one-day Poga came upon a function. The function f was defined as
f(n, k) - f(n-1, k) = f(n-1, k-1),
f(n, 0) = 1
and f(n, k) = 0 when n < k
Seeing how this is a recursive function, Poga got very scared. To find courage, she remembered 2 of her favorite numbers, N and K. Now she wants to find the value of f(N, K). Being a genius, it was very easy for her. Now she has challenged you to do the same too. As the answer can become very big, you should print the answer modulo M.
Input
Input starts with an integer
T
, denoting the number of test cases.
Then each of the next T lines contains three integers
N
,
K
, and
M
.
Constraints
1 <= T <= 100
1 <= N <= 10
5
0 <= K <= 10
5
1 <= M <= 10
12
Output
For each test case, print the answer, value of
f(
N
,
K
)
modulo
M
.
Example
Input:
5
7 4 100
6 3 2
6 3 7
2 2 200
57217 10661734081
Output:
35
0
6
1
0 | 34,452 |
Coding Test (REAYZCODETST)
Great programmer “Tourist” is attending a coding test. In this test he is asked to solve an easy problem. The problem description is:
There is an array
a
consists of
n
integers and another nonnegative integer
x
. He need to find the number of pair (i, j) where
i != j
and
a
i
– a
j
= x.
As it is very easy for him, he gave you this problem and start trying another hard problem. Can you solve this for him?
Input
Input starts with an integer
t (1 ≤ t ≤ 10),
number of test case.
Each case contains two integer
n (1 ≤ n ≤ 10
5
),
and
x (0 ≤ x ≤ 10
9
).
Next line contains n separated integers
a
i
(1 ≤ a
i
≤ 10
9
).
Output
For each case, print the case number and the number of pairs which meet the above condition.
Example
Input:
2
5 3
5 1 4 2 3
10 2
12 17 19 13 17 11 17 12 15 14
Output:
Case 1: 2
Case 2: 10 | 34,453 |
Minimum Stocks (MINSTOCK)
Himanshu wants to invest into stock market and his friend Navneet helps him by providing him instruction for next
N
days.
Navneet gives Himanshu 3 types of instruction,
1
X
Y
There is a stock X available at price Y. Here X is a string and Y is an integer.
2
X
Z
The price of stock X has changed to Z. Here X is a string and Z is an integer.
3 BUY
Buy the stock which has the lowest price.
You as a programmer, are given all the instructions of
N
days. Can you tell,
which stock did Himanshu buy on which day
. Print the output in same order as Himanshu bought the stock. See sample input and output for clarification.
At any point of time
, there is
atmost one stock of
X
. However,
X
can be made available to market again through another instruction of type 1.
All instructions are valid.
i.e. There is always some stock to buy having the minimum price of all. Also if the price of
X
has changed, then
X
is already known and hasn't been bought yet.
Input
First line contains
N
. (1 ≤
N
≤ 10
6
)
Next
N
lines, each of them contains an instruction of any of 3 types. (Look at instruction format above.)
In any instruction, (X is a string of length upto 10 characters. All characters are from English alphabet, both small and capital), and (0 ≤
Y
≤ 10
9
) and (0 ≤
Z
≤ 10
9
).
Output
For each instruction of type 3, output two values
X
and
Y
. Where
X
is the name of Stock having minimum price and
Y
is the day on which it was bought.
Example
Input:
7
1 ABC 32
1 XDC 54
3 BUY
1 XCD 32
1 ABC 12
2 XDC 10
3 BUY
Output:
ABC 3
XDC 7
Explanation
On day 3, there is instruction to buy. There are two stocks available "XDC" and "ABC", since price of "ABC" is less, he buys it. After this "ABC" is not available in market anymore.
On day 7, there is instruction to buy. Of all stocks available, "XDC" has the least price and hence he buys "XDC". | 34,454 |
Impress zing (ZING01)
Zing was his senior crush. In order to get impressed, she gave him the following problem:
A string is given as input in lowercase. There are two types of queries:
Type 1: 0 l r c (update the string with character 'c' from 'l' to 'r').
Type 2: 1 k c (Print the index of kth character 'c').
If the k'th character doesn't exist print "-1".
Input
Line 1: string input (1<=length<=100000)
Line 2: number of queries (<=100000)
In next q lines, the queries of type 1 and type 2 are given.
Output
For each print query, print the integer index or "-1".
Example
Input:
zazzxeffah
3
1 3 z
0 2 4 a
1 3 z
Output:
4
-1 | 34,455 |
Cheesy line (ZING02)
"Why me?", Zing asked.
"If you're a star then I'm the darkness. You complete me", he replied.
Being impressed by him, she gave him an another task.
Build a tree with following rules:
If a node with (l, r) is given
Break it into left node with (l, mid)
Break it into right node with (mid + 1, r)
stop when leaf node appears (l = r)
where mid=(l + r) / 2.
So count the number of leaf nodes which do not appear in pairs.
For more clarification, see examples below.
Input
Line 1: Number of queries (≤ 100000)
Line 2: In next q lines, l r is given (0 ≤ l ≤ r ≤ 10
18
)
Output
For every query, print the answer.
Example
Input:
2
2 4
4 5
Output:
1
0
Explanation : In (2, 4) → (2, 3) and (4, 4).
(2, 3) → (2, 2) and (3, 3).
Here (4, 4) is the only leaf node which didn't appear in pair. | 34,456 |
Keyword Finder (SHAKILKEYWORD)
Nahid bhai is a principal software engineer of a renowned software company. Rumman is an associate software engineer of that company. One day Nahid bhai assigned a task to Rumman. The task is to find all keywords from a given string. A keyword is a word that contains at least one “#”. A word may contain other letters and punctuations. Given string will be split based on some delimiters. Rumman is so busy learning cutting edge technology for developing a project. So he assigned this task to you. You as a programmer will do the same thing.
For example: Given string is “Here are some fruit name: #apple, #banana, #orange.”. Delimiters are “|$ *@.&\"!^,?”. So, the words containing “#” will be: “#apple”, “#banana”, “#orange”.
Delimiters are, fixed for all string: “|$ *@.&\"!^,?”.
Input
Input starts with the number of test cases, T (1 <= T <= 10).
Each line contains a string consisting letters and punctuations. (1 <= |string| <= 100).
Output
For each test case, print the keywords in a new line. If no keywords found, then print “No keywords.”
Example
Input:
3
ab#c def#.
Abcde fghij
abc.efg#ijk #lMn.
Output:
ab#c
def#
No keywords.
efg#ijk
#lMn | 34,457 |
Easy Math (PRADIPSUM)
Zoro is
a student of a primary school. He likes to solve mathematical problem. One day he tries to solve a math, but he is unable to solve the problem efficiently because of being a student of primary school. As you are a programmer he wants your help to solve this problem. The problem is to find the sum of some consecutive numbers.
Example
, if the first number is 2 and the last number is 5, then the result would be
2 + 3 + 4 + 5 = 14
Input
Every line contains two integers a and b. Input is terminated by the
end of file.
Output
Output the sum the of all numbers between a and b (inclusively).
Constraints
-10
8
≤ (a , b) ≤ 10
8
Example
Input:
2 5
5 10
Output:
14
45 | 34,458 |
Hack the Password (NABILHACKER)
Credit:
Problem Set and Data set: Yasir Uddin Ahamed (Nabil)
Alternate Solution: Iqbal Hossain Bappy
Many people asked me what actually contest programming do in real life projects. That does not feel so interesting to me. What I found interesting is, one day a hacker friend came to me. He asked me to solve one of his hacking problems.
When you try to steal someone's password, you may set a keylogger in his/her computer. Keylogger will give you a string that is typed as the password. But there is a problem, it will give you everything victim typed such as left key, right key, backspace everything. (Left key and right key means the keys you use to play car games, and backspace is the key you use to remove a letter, the button on the top of "Enter" button.)
Suppose, the victim typed "generio312" as password, but he follows these criteria:
he typed generio1.
Then he pressed the left key, and press 3. So the password will be generio31.
Then he pressed the right key and press 2. So the password will be generio312.
Now he typed ghj and then press backspace three times and remove these three letters. So the final password is generio312.
But, as I said, keylogger gives you all the typed key. You will get "generio1<3>2ghj---". (Here, < for left key, > for right key and - for backspace.)
Input
At first input T, the number of test cases.
Then input T strings. 1 ≤ |s| ≤ 10
6
. The strings will include uppercase, lowercase, <, >, - and digits 0 - 9.
Output
The output should contain a string each line, the password.
Example
Input:
2
<<BP<A>>Cd-
ThisIsS3Cr3t
Output:
BAPC
ThisIsS3Cr3t | 34,459 |
x-Xor It! (XORX)
Given an array of n integers and a number x. Your task is very simple. You have to find the subarray (length > 0) whose xor is maximum with x. Let's say the subarray as maxsubarray. You have to print the xor value of maxsubarray.
Input
First line of input consists of t test cases.
Second line of input contains two integers n and x.
Third line contains n space separated integers denoting the elements of array.
Output
First and only line of output is xor value of maxsubarray.
Constraints
1 ≤ t ≤ 10
1 ≤ n ≤ 200000
1 ≤ x ≤ 2×10
9
1 ≤ arr[i] ≤ 2×10
9
where arr[i] is any integer of array.
Example
Input:
1
3 7
1 2 3
Output:
0
taking 1^2^3 is 0 when taken xor with 7 gives us the maximum xor value. | 34,460 |
Euler Puzzle (CIRCLEDIV)
Given some set of points on a circle, you connect every pair of them with a line, find the maximum number of sections do these lines cut the circle into?
Input
First line for each test case file contains T denoting the number of test queries followed by T numbers N, denoting the number of point on a circle.
Constraints
1 ≤ T ≤ 100000 (10
5
)
1 ≤ N ≤ 100000 (10
5
)
Note: Use fast I/O methods.
Output
For each test query, output the result in given format. As the result can be large answer the result modulus 1000000007 (10
9
+7).
Case <test_query_i>: <max_section_circle_cuts_into>
Example
Input:
3
1
2
6
Output:
Case 1: 1
Case 2: 2
Case 3: 31 | 34,461 |
Yet Another Subset Sum Problem (SUBSUMP)
You are given a
N × N
grid, where each cell in the grid is either blocked or free. You can place any number between
1
to
N
in the free cells. You are required to assign numbers to the free cells. No free cells should be left empty after the assignment, i.e. each free cell must contain exactly one number from the range
[1-N].
You are also given a target value
V
. Find out how many ways we can assign numbers to the free cells such that their sum equals
V
. It is also required to satisfy the following constraints:
For any row, all the numbers present in that row should be distinct.
For any column, all the numbers present in that column should be distinct.
Two ways are considered different if
any
particular cell has different values assigned.
Let
M
denote the total number of free cells. It is guaranteed that there will be at least
1
and no more than
12
free cells.
Input
The first line contains an integer
T
, denoting the number of test cases. The next line contains two integers,
N
and
V
. The next
N
lines contains the grid, each line contains the corresponding row of the grid.
Cells marked with a
'.'
indicates free cells, and cells marked as
'X'
denotes blocked cells. Each cell can be either blocked or free so the grid will not contain any other extra characters.
Constraints
1 < T ≤ 120
1 < N ≤ 12
1 < V ≤ 144
1 < M ≤ 12
For
60%
of the test cases, the following constraints will hold:
1 < N ≤ 8
1 < M ≤ 8
Output
For each test case, output the case number followed by the number of ways to create the assignment. Please check the sample input/output for more clarity.
Example
Input:
5
1 1
.
2 2
.X
.X
2 3
.X
X.
3 7
.X.
X.X
XX.
5 15
XX.XX
XX.XX
.XXX.
XX.XX
XX.XX
Output:
Case 1: 1
Case 2: 0
Case 3: 2
Case 4: 9
Case 5: 192 | 34,462 |
Prime Friendly Numbers (PRMFN)
Given
N
, find the largest number
X
not greater than
N
such that
X
is prime friendly. A number is called prime friendly when it satisfies both of the following conditions:
The number itself is a prime.
All its digits in base
10
are also primes. In other words, the number consists of only the digits
2, 3, 5, 7.
Input
The first line contains an integer
T
, denoting the number of test cases. Each test case contains a single positive integer
N
.
Constraints
1 < T ≤ 1000
1 < N ≤ 10
18
Output
For each test case, output the case number followed by the largest number
X
not greater than
N
. Please refer to the sample input/output section for more clarity of the format.
Example
Input:
5
10
100
1000
10000
100000
Output:
Case 1: 7
Case 2: 73
Case 3: 773
Case 4: 7757
Case 5: 77773 | 34,463 |
Long Tiling (LNTILING)
There is a long gap with fixed width of 1 unit in the ground with
N + 1
vertices, which is composed of
N
segments with same width. A segment connects to at most one segment on its head and tail vertically or horizontally, that is, it can connect to at most two segments. The long gap formed by those segments is simply a open polyline. Duck doesn't like the gap, he is given a set of tiles and wants to know if the long gap can be tiled by the limited number of tiles. There are
M
distinct tiles, each with
K
i
and has
C
i
segments. It is not necessary to use all tiles, and Duck can rotate the tile to fill the gap. It is guaranteed that both long gap and tiles are open polyline with fixed width of 1 unit. Can you help him check if he is possible to do so.
Input
The first line is the number of test cases
T
. (1 ≤ T ≤ 25)
For each test case, it starts with the number of segments of the long gap
N
. (1 ≤ N ≤ 20)
Following N lines, each consisting of one uppercase character
W1
i
, either up (U), down(D), left(L) or right(R), and one integer
F1
i
, indicating the direction to turn to and the length of that segment. (1 ≤ ∑
n
i=1
F1
i
≤ 100)
Next line is the number of distinct tiles
M
. (1 ≤ M ≤ 15)
For each distinct tile, it starts with two integers, the available amount of that tile
K
i
and number of segments
C
i
. (1 ≤ K
i
, ∑
M
i=1
K
i
≤ 15, 1 ≤ C
i
≤ 20)
Following C
i
lines, same as above, one uppercase character
W2
i
and one integer
F2
i
indicating the direction and the length of that segment.
Output
If it is possible to tile the gap with given tiles, print "YES", else "NO". (without quotes)
Example
Input:
2
16
L 4
U 2
L 7
U 2
L 4
D 4
R 2
D 2
L 3
D 2
R 6
U 1
R 2
D 4
L 3
U 1
7
2 5
D 6
L 2
U 3
L 2
D 2
1 2
D 7
L 2
2 2
D 2
R 2
1 1
R 8
3 1
U 3
4 1
D 4
2 2
R 3
U 1
2
R 6
U 2
2
1 2
D 3
L 2
1 1
U 2
Output:
YES
NO
Explanation | 34,464 |
Teleporters and Gems (TLPNGEM)
Duck is playing "Teleporters and Gems". On a 1 x
N
map, there may be multiple teleporters (or no) and at least one gem. At the beginning, Duck is standing at the most left position. He has to collect all the gems with the help of teleporters. Below are the rules:
1. Moving 1 unit is counted as 1 move.
2. When Duck reaches a teleporter, he can instantly reach any of the next 3 teleporters (eg. 1 to 2, 3 or 4 teleporter, but not 1 to 5, 6, 7 teleporter, and previous not allowed) by 3 moves; Or he can ignore that teleporter and keep moving to next cell which is counted as 1 move.
Let '@' = Teleporter, '.' = empty cell, there are 4 teleporters: @.@..@@, if Duck is at position 0 (the 1st teleporter), then he can instantly reach 2nd, 3rd or 4th teleporter by 3 moves. But ignoring the 1st teleporter makes him reach 2nd teleporter by 2 moves.
3. Duck has to collect all gems.
4. Once he collects the last gem, the game ends and no more move needed.
5. Duck can only move right.
Can you help him find out the minimum number of moves in order to collect all gems?
Input
The first line is the number of test cases
T
. (1 ≤ T ≤ 50)
For each test case, there is a string
S
consisting of '.' (≥ 0), '@' (≥ 0) and '*' (≥ 1), representing empty cell, teleporter and gem. (1 ≤ |S| ≤ 10
4
)
Output
Print one integer x where x is the minimum number of moves in order to collect all gems starting from the most left cell.
Example
Input:
3
.@@...@.@...@@..*@.@.*...@..
....@...@@.**.@....@@@*@.
.*....*@@@@*..@@*..@
Output:
14
16
16
Explanation
Bracket means cells that Duck will pass through and minimum number of moves to that cell
Case 1: (.@@)...@.@...(@@..*@.@.*)...@.. = 0 1 2 -> 5 6 7 8 9 10 11 12 13 14
Case 2: (....@)...@(@.**.@)....@@(@*)@. = 0 1 2 3 4 -> 7 8 9 10 11 12 -> 15 16
Case 3: (.*....*@)@@(@*..@@*)..@ = 0 1 2 3 4 5 6 7 -> 10 11 12 13 14 15 16 | 34,465 |
A Complex Cone (ACC)
A cone with radius of base 'r' and height 'h', is stretched to the left and right by length 'a' and 'b' respectively such that height of the stretched cones equal the height of the original cone and apexes of all the three cones are colinear.
Write a program to calculate the volume of the union of the two stretched cones (colored blue in image).
Input
The first line of input is the number of test cases T. (1 ≤ T ≤ 100)
For each test case, there is a single line containing 4 integers 'r', 'h', 'a' and 'b' respectively. (0 < r, h, a, b ≤ 10)
Output
Output a single line for each test case, containing the volume with 1 decimal place only. Answer should be correct to 1 decimal place without rounding off.
Example
Input:
2
1 1 1 1
10 10 10 10
Output:
1.4
1491.6 | 34,466 |
Count Pairs (ILD18ACP)
Given a undirected graph with n vertices and m edges. Your task is count the number of distinct pairs (u, v) that there is exist a path with length exactly 2 from u to v. Another mean, with each pair (u, v), we could find a vertex t that we have an edge (u, t) and (t, v). The input set may be contains multiple edge between any vertex and not consider to connected.
Input
- First line: n, m (1 <= n, m <= 10^5).
- m following line: u, v (1 <= u, v <= n).
Output
The number of distinct pairs.
Example
Input:
5 4
2 1
1 5
3 1
4 3
Output:
4
Note:
we have (1, 4), (2, 3), (2, 5), (3, 5) | 34,467 |
High CG Boy (CGBOY)
There are some people with high CGPA who rarely codes, and the people who codes regularly doesn't have a handsome CGPA. However in IUT (International University Of Technology) There is a student who has a handsome CGPA but codes too. His name is Atondro Fuad. He is very serious about his CGPA. Though he had some bad results in some of his previous semesters, he is so determined that he will not miss CGPA 4 from now on in any semester.
Now, Your task is to help Atondro Fuad to find what will be his CGPA while he'll be graduating (after 8 semesters) assuming that he'll get GP 4.00 on every semester from now.
Given his semester and current CGPA, find the final CGPA of Atondro Fuad.
Every semester in IUT has same number of credits.
Input
First line will contain an integer t <= 1000, the number of testcases.
Then the next t lines will contain one integers 0 < n <= 8 and one floating point number 0.00 <= c <= 4.00.
Here n is the current semester Atondro Fuad is in, and c is his current CGPA.
Output
For each testcases, print a new line containing his CGPA after graduation rounded up to 2 decimal places.
Example
Input:
5
3 3.95
4 4.00
5 2.56
1 0.00
8 3.98
Output:
3.99
4.00
3.28
4.00
3.98 | 34,468 |
Love Guru (LOVEGURU)
Recently after girls being allowed to get admitted into IUT, the number of couples in IUT (International University Of Technology) is gradually increasing. In last few months a problem has been noticed. More than one boy/girl had crush on same boy/girl. These created conflicts amongst students. To solve this problem, IUTRC (IUT Relationship Community) came up with a solution. They set up rules with whom a student can only get in a relationship. If your name and the other persons name has a common substring of length k, only then can you engage yourselves in a relationship. Now, as a Love Guru, students are coming to you for knowing if his relationship with his crush possible or not. Help them Love Guru.
Input
First line contains two integer 0 < n < 10000 and 1 < k < = 100. It is guaranteed than n*m doesn't exceed 10
6
.
n refers the number of people came for your help and k refers the minimum common substring size.
Then next n lines contains two space separated strings each containing only lower case letters. Name of the boy and name of the girl. No name contains fewer than 2 characters or more than 100 characters.
Output
For each case on each line print "Yes" if possible and "No" otherwise.
Example
Input:
4 3
salman almiza
rizvi krizvia
abid nowmi
atondro iroboton
Output:
Yes
Yes
No
Yes | 34,469 |
Make IUT Great Again (MIUTGA)
Few years back IUT (International University Of Technology) authority decided to increase the number of students. As more students are getting admitted every year but the number of classroom didn't increase, the classrooms became overpopulated. As people are complaining about this, and making new classrooms is not possible in next few years one of the very intelligent person of the authority gave an idea to tackle this situation. It is that, they will not increase the classrooms rather they will distribute the classes into rooms such a way that it doesn't look like overpopulated although all the students come out of the class at the same time. So, It'll create an illusion amongst the visitor and they will think IUT is great as before.
The distribution process is that for every class this class will either contain more students than all the adjacent classes or contain less students than the adjacent classes. All the classes are situated in a line, so each class has two adjacent classroom except the 1st and the last classroom.
Suppose there are n classroom and the number of students of that classrooms are a
1
, a
2
, a
3
, a
4
... a
N
You'll have to distribute the classes as
a
1
> a
2
< a
3
> a
4
< ... a
N
, or
a
1
< a
2
> a
3
< a
4
> ... a
N
However you can only do one type of operation. You can swap the classrooms at position i and n-i+1.
You'll have to determine the minimum number of operations to distribute the classrooms and make IUT great again. If it's impossible print -1.
Input
First line of the input contains t, number of testcases. Then each testcase takes a integer n, number of rooms and then n integers the population of n'th class.
1 < t < 10
0 < n < 10
5
0 < a
i
< 10
9
Output
For each testcase on separate line print the minimum number of operations or -1 if not possible.
Example
Input:
2
6
1 2 3 4 5 6
5
5 2 4 3 5
Output:
1
0 | 34,470 |
Bermuda Love Triangle (BEMINE)
Bermuda triangle is also known as Devil's triangle. Many people disappeared there with their ships or planes. Love triangle is a triangle where when someone gets into its uncertainty he disappears from himself. So Bermuda love triangle is a triangle where there is uncertainty from each angle. Here is a story of Bermuda love triangle, where 3 vertex consists of 2 girls and a boy. Both of the girls love the boy and they are best friends also. The boy feels same for the both girls. So, its a uncertainty-uncertainty-uncertainty triangle. As this can't go like this, they all want a solution. As students of CS department they found a solution.
The boy will be holding two infinite series in two hands, the first girl will pick a series, the 2nd girl gets the other. Then the 2nd girl will tell a number x where both sequence will end. Now, whose result of the series will have a larger prime divisor will get the boy. If the result for both series is equal then they will all be friends.
Given the series, first girl's choice, second girl's chosen x, find who will win the boy, or they will be friends.
Series 1 = 1 + 2 + 3 + 4 + 5 + 6 + ... + x
Series 2 = 0! + 1*1! + 2*2! + 3*3! + 4*4! + ... + x*x!
Use faster I/O.
Input
The first line contains an integer 0 < t < 10000, number of testcases.
Next t lines contain 2 integer 1 <= s <= 2 and 2 <= x <= 10
8
. Here s is the choice of the first girl, and x is the end of the series.
Output
For each test case print "First" if the first girl wins the boy, print "Second" if the second one wins, "Friends" otherwise.
Example
Input:
4
1 2
2 50
2 36
1 20
Output:
Friends
First
Friends
Second | 34,471 |
Line Follower Robot (ILOVEGEO)
In this Years RoboFest in IUT (International University Of Technology) there a segment called LFR contest. An LFR contest is a contest where the robots have to follow a specific line. The track was bound by a inner and a outer polygon. More clearly the space between the two polygon was track. Now, to make the contest easier, the authority created glass walls along with the lines of the polygons so that no robot gets out of the track. There is a rule that all the robots have to be circular. Given the coordinates of the points of both polygon in consecutive order, Find the biggest diameter possible for a robot so that it does not get stuck anywhere in the track. (Does not get stuck between walls.
Input
First line of the input contains 0 < t < 101, number of test cases.
Then in the next t test cases:
Each test case starts with a number 2 < N
i
< 101, number of points of the inner polygon.
Then next N
i
lines contains two integer each, X
i
, Y
i
denoting coordinates of the point in inner polygon.
Then comes a number 2 < N
o
< 101 Number of points in the outer polygon.
Then next N
o
lines contains two integer each, X
i
, Y
i
denoting coordinates of the point in the outer polygon.
All coordinates have absolute value no larger than 1000. The points of the polygons can be given in either clockwise or counterclockwise order and the two polygons do not intersect or touch themselves or each other. The outer polygon encloses the inner polygon.
Output
For each testcase on a new line print a floating point number rounded to 6 digits denoting the maximal diameter possible for the robot.
Example
Input:
2
4
-5 -5
5 -5
5 5
-5 5
4
-10 -10
-10 10
10 10
10 -10
3
0 0
1 0
1 1
5
3 -3
3 3
-4 2
-1 -1
-2 -2
Output:
5.000000
1.414214 | 34,472 |
Beautiful Roll Numbers (NUMBERTH)
The Roll Numbers In IUT (International University Of Technology) got bigger in size time to time. For example , once upon a time it only consisted of 1 digit (e.g. 2), now it consists of 7 digits (e.g. 1741050). There was a reunion for all the IUTians recently where you met many peoples. As you are an introvert, Your seniors have given you a simple task so that you can become more social. The task is You'll have meet n people and ask their roll and then you'll have to talk with him minimum of m minutes. Here m is the beauty value of his roll.
Beauty value of a roll is the number of different prime numbers that can be generated by taking some digits from the roll and shuffling them. For example if the roll is 17, the beauty value is 3, because the only primes can be generated from this are 7, 17, 71.
Input
First line contains an integer 0 < n < 250, number of people you met.
Then the next n lines contains the roll of that person. Note: roll can have leading zeros (e.g. 011)
Output
For each person in a new line print the minimum number of minutes you had to talk to him.
Example
Input:
4
17
5
011
1276543
Output:
3
1
2
1336 | 34,473 |
Best Saved For The Last (HEADBANG)
Mirza is a fresher of IUT (International University Of Technology). Today is his fresher's reception day. His senior brother's arranged an amazing function on that occasion. N best music bands of the country came to perform in his campus. As per sunset rules, they had to shorten the timespan of the program. So, all the N bands are performing in different auditoriums at the same time. After each song of each band there is a break, when if you wish you can go from one auditorium to other, but not in between the songs. And also you want to enjoy the most.
Now, there is a fun value for each song played by each band. When you are listening to a band for the first time, you'll enjoy the most. Then if you hear a song for the second time you'll enjoy less. The value will decrease by times.
More formal: for a specific band i, There is two fun coefficients a
i
and b
i
. While listening a song of a particular band for the k-th time, Mirza gains a enjoyment value of f(i, k) = a
i
− (k − 1)
2
* b
i
. If f(i, k) is non-positive, listening a song to that band is no longer enjoyable. Note: If you enjoyed 1st song of a band then go to other auditorium and miss the second song of this band and come back again for the 3rd song, the 3rd song of this band will count as 2nd for you as you are listening them for the 2nd time.
Given the maximum time Mirza can spend in the auditoriums, can you tell the maximum enjoyment value of Mirza.
Input
The first line contains the integer N, where N is the number of band came to perform (0 < N ≤ 100). The following N lines contain the integers a
i
, b
i
and t
i
where a
i
and b
i
are the fun coefficients as specified above and t
i
is the length of each song played by the i-th band (0 ≤ a
i
≤ 2500; 0 ≤ b
i
≤ 2500; 0 < t
i
≤ 50000). The next line contains a positive integer Q denoting the number of Query (0 ≤ Q ≤ 1000). Each of the following Q lines contains an integral time T that Mirza spends in auditorium his (0 ≤ T ≤ 50000).
Output
For each query, print an integer denoting the maximum enjoyment value Mirza can get.
Example
Input:
2
5 0 5
7 0 7
4
88
5
6
7
Output:
88
5
5
7
Input:
1
100 3 2
5
2
3
4
5
100
Output:
100
100
197
197
435 | 34,474 |
Coder Or NonCoder (CODECODE)
The x% of the students in IUT(International University Of Technology) are Coders. Rest of them are noncoders. A new student got admitted into IUT who has lots of interest in programming. So, he wants to meet some senior coders of IUT. As he does not know who is coder and who is not, but he himself is a coder he has y% possibility of identifying a coder. eg. If he meets Zico_vai, He has a y% possibility that he will identify Zico_vai as coder and (100-y)% possibility that he'll mistake. The similar for non coders is also true. eg. If he meets a noncoder he has y% of possibility to identify him as noncoder and (100-y)% possibility that he'll mistake.
Now, He met a random IUTian, given x and y find the probability that he'll think the guy he met as a coder.
Input
First line of the input contains an integer 0
Next t lines contain two integers each x, y (0<=x, y<=100) described above.
Output
For each testcase in a new line, print the probability (rounded to 2 digits after decimal point) in percentage with a percentage sign.
Example
Input:
2
5 90
12 25
Output:
14.00%
69.00% | 34,475 |
Professional Stalker (BHAGO)
Abid is a professional stalker. He stalks so many people on social media everyday. However he has other things to do, so he do not stalk infinite number of people everyday. To be more precise, he do not stalk more than 90 people in a day. (90 is the number of expected students in each classroom in IUT, Coincidence!). However, he can stalk any number of people between 0 to 90 inclusive in a day. An interesting fact is that there are 10 intervals,
[1, 5], [6, 10], [11, 21], [22, 34], [35, 51], [52, 62], [63, 69], [70, 76], [77, 85], [86, 90].
In this ten intervals each of them has the same possibility that the number of people Abid stalked on that day lies on that interval. Or more clearly, at the beginning of the day each of the interval has 10% of probability that the number of people Abid stalked on that day is inside this limit.
Now, before you woke up in the morning, Abid already stalked x peoples on social media. You'll have to find the expected number of peoples he is going to stalk on that day after you wake up.
Note: Abid can not stalk fraction of a person.
Input
First line contains an integer 0 < t <= 90, number of testcases,
The next t lines contain one integer x (0 <= x <= 90) each, number of people he already stalked before you woke up.
Output
For each testcase on each line print the expected number of people he is going to stalk on that day after you wake up.
Example
Input:
3
0
90
36
Output:
46
0
32 | 34,476 |
Pandas are the best Lovers (PALOVE)
Who doesn't love pandas? But more importantly, which panda doesn't love? They are the best lovers, believe me. Furthermore, the relationship is the strongest when they have love with bears. These are not just facts, also have proven practically. Pandas are somewhat good at mathematics, so they like presenting mathematical gifts to their lovers, bears. Pandagorean the panda proved following theorem and it is called the Pandagorean theorem:
For given prime number
p
, there exist two sequences with the following properties:
- Both sequences have the same length and let it be
n
. Also, let the sequences be (
x
1
, x
2
, ..., x
n
), (y
1
, y
2
, ..., y
n
)
.
- x
1
2
+ y
1
2
≡ x
2
2
(mod p),
- x
2
2
+ y
2
2
≡ x
3
2
(mod p),
....
- x
n
2
+ y
n
2
≡ x
1
2
(mod p),
- 1 ≤ x
i
, y
i
≤ p-1 for each valid i.
- 1 ≤ n ≤ 10*p
However, she couldn't find such two sequences. In the calendar of panda world, all numbers are prime numbers and her lover Bearsh the bear was born in the
p
'th day of the only month. So, she needs to find any two sequences satisfying above conditions with that
p
. Why don't you help her?
Input
The only line consisting of the number
p (7 ≤ p ≤ 100000).
Output
In the first line of the output the number
n
. The second line should contain the numbers
x
1
, x
2
, ..., x
n
, separated by single space. Next and the last line should contain
y
1
, y
2
, ..., y
n
in the format of the previous line.
Example
Input:
7
Output:
6
1 4 2 1 4 2
6 3 5 6 3 5 | 34,477 |
Smallest on the Stack (MINSTACK)
Every Christmas the good old man can go to every house in the world and leave gifts for the children who have been good throughout the year, but this is only possible because of his magic gift bag. It would be impossible for Santa to carry all the presents in his bag, the volume and weight of them all makes this obviously unfeasible. What actually happens is that your bag is a kind of magical portal to your gift factory at the North Pole. Where the presents are stacked by their elves and Noel always takes the gift from the top of that pile when he accesses his magical bag.
Gifts have a numerical measure of the degree of fun they can provide children, and Santa is always concerned with the least fun gift he will deliver throughout the night because he does not want any child to feel bad about it. you receive. However, this can not be done in advance because throughout the night as the good old man takes gifts from the pile to deliver, others are still being made and placed on the pile. So the most he can know is the value of the least fun present on the stack up to that point.
Your task is, given the sequence of operations done on the stack of gifts, answer Santa's queries about the value of the least entertaining gift on the stack thus far.
Input
The first line of the input contains an integer
N
(1 ≤
N
≤ 10
6
) corresponding to the number of operations performed on the present stack. The operations can be of three types: "
PUSH
V" where
V
(1 ≤
V
≤ 10
9
) is an integer representing the degree of fun of the present being placed on the stack; "
POP
" which represents that Santa Claus is taking a gift from the cell to deliver and "
MIN
" representing a Noel query to know the smallest gift value in the stack.
Output
The output consists of a line containing an integer with the smallest present value in the stack for queries of type "MIN" or "EMPTY" for "MIN" and "POP" operations when the stack is empty.
Example
Input:
11
PUSH 5
PUSH 7
PUSH 3
PUSH 8
PUSH 10
MIN
POP
POP
MIN
POP
MIN
Output:
3
3
5
Example
Input:
9
PUSH 100
PUSH 50
MIN
PUSH 45
MIN
POP
MIN
POP
MIN
Output:
50
45
50
100 | 34,478 |
DNA of Elf (DNAOFELF)
Everyone knows that Santa's Elves are non-gendered and reproduce by magic. Every time a new Elf is needed, two other Elves come together, build a snowman, put a hair of each of them on him and then use their magical powers to give life to the snowman who becomes an Elf. This Elf always inherits the magical powers of its creators, unless both creators possess the same type of power, so the new Elf does not inherit such power because there is a magical overhead. Elves, too, never create other elves without magical powers.
Because it was so easy to create new Elves, Santa realized that his subordinates were creating many new helpers, not thinking about the consequences. Simply to lessen your workloads. So he decided to forbid the creation of new Elves who already had the same set of powers as any existing Elf, as this would be redundant given that a single Elf with that set of powers is more than enough for the function that is designated. In addition there may already be more than one Elf of that type because it was created before Good Old Man vetoed the creations.
Now the little magical beings live a dilemma: Given the information of all types of powers that each Elf has, what is the maximum number of new Elves that can still be created?
Input
The first line of the entry contains an integer
T
corresponding to the number of test cases that follow. The first line of a test case contains an integer
N
(1 ≤
N
≤ 10
5
) representing the number of Elves currently in the Santa factory. The following are
N
lines each containing a sequence of max. 64 characters
C
i
.
C
i
is always a lowercase or uppercase letter of the English alphabet or a digit from 0 to 9 and represents a type of magic power. Lowercase letters represent types of magic power distinct from uppercase letters.
Output
The output consists of one line per test case containing the maximum number of Elves that can still be created without contradicting Santa's ban
Example
Input:
3
2
xz
yx
3
xz
yx
zy
5
xazt
ctz
cax
xz
at
Output:
1
0
2 | 34,479 |
Earth Sled Tour (CNURI18H)
So it's Christmas! And Santa Claus needs to perform a series of deliveries of gifts in different locations around the world.
For those who do not know, the reindeers are sick and he will need to use the gas-powered thunder to deliver the presents.
A curious fact is that the roads between cities are perfectly straight and there is a gas station in each city. Santa Claus is a very smart guy and, to avoid problems, he fills the tank with a specific value X is the value of the largest road between the cities that Santa is traveling, so he knows that he will never run out of gas between the two cities and the gifts will not be stolen. In addition, it always selects the path where the largest road is as minimum as possible.
Can you help the Santa Claus determine what X value of gas he should use?
Input
The first line is composed of two integers N (1 ≤ N ≤ 10
5
) and M (N−1 ≤ M ≤ min(2×10
5
, N×(N−1)/2)) is the number of cities and the number of roads. Next come M lines with three integers u, v, w (u ≠ v) (0 ≤ u, v < N) (1 ≤ w ≤ 10
6
), that there is a road connecting it with weight (
you can use the road in any direction
). After M lines, has an integer Q
(1 ≤ Q ≤ 10
5
) is the number of queries that Santa Claus will perform. Each of Q lines is composed of two integers x and y (0 ≤ x, y < N) corresponds to the query: how much X gasoline that Santa Claus will need to supply in every city between the cities x and y.
Output
Print Q lines each with an integer X is the answer of the dilemma what Santa Claus is passed.
Example
Input:
7 11
0 1 15
0 2 53
1 2 40
1 3 46
2 4 31
2 5 29
3 4 3
4 5 29
3 6 11
4 6 8
5 6 40
7
0 1
0 3
0 6
2 4
4 6
5 1
1 1
Output:
15
40
40
29
8
40
0 | 34,480 |
Noel and His Reindeer (DABRI001)
The big Noel is a guy full of habits. This year he put all of his reindeer in a row and decided to select the most of them, following a few rules.
Reindeer can not be changed in order, i.e. a reindeer that is in position
i
in the original row should appear before the reindeer
j
in the chosen list, where
i
<
j
.
Reindeer of two adjacent positions in the final sequence must differ exactly by 1 (right-left=1) unit in their heights.
If that was not enough, Noel realized that this sequence had few reindeer. So she decided to include a new reindeer in the original row. Taking into account that this new reindeer can be inserted in any position and he will always choose a reindeer with the best possible height.
After making the task a little difficult, Noel ended up getting confused and is asking for your help to find out how many reindeer can be selected taking into account the rules imposed.
Input
The first line of the entry contains an integer
N
(1 ≤
N
≤ 10
5
) corresponding to the number of reindeer. In the second line contains
N
integers
X
i
(1 ≤
X
i
≤ 10
6
) which represents the height of the
i
th reindeer.
Output
Print as many reindeer as Noel can select.
Example
Input:
4
1 1 2 2
Output:
3 | 34,481 |
Full Sleigh (TRENOLOT)
It's Christmas Eve, and it's almost time for the sled to leave. Everything is already in Santa's sack and the reindeers in position, there is only one thing left: decide which helpers will work with Noel this year. Yes, contrary to popular belief, the good old man does not do everything himself. He always takes with him a group of elves on his round the world in one night.
However, the elves must be chosen carefully, because their weight will directly affect the aerodynamics of the Sled. If it is very light it will swing very much during the flight and if it is very heavy it will tire the reindeers very early.
As it is in hurry Noel decided to make a try and chose a group of helpers. But the Reindeers soon accused them of being too light. Then Noel made a second attempt, chose another group. But again the Reindeers complained, however, stating that it was now too heavy. The good old man, who has an appointment time, became irritated and gave an ultimatum to his subordinates: "That's enough! Choose
K
elves soon to go in such a way that the sled is neither too light nor too heavy! That is, the sum of the weights can not be less than the sum of first group that I tried and neither greater than the second one. And do it fast!".
Of course the little ones despaired. Apart from the restriction of weights and now the number of Elves that have to be exact, they still have the fact that each Elf weighs twice as much or more than an Elf younger than him. Which obviously only complicates everything.
Knowing that all Elves are of different ages can you help these little ones tell how many ways they can choose a group to go with Santa respecting all the requirements?
Input
The first line of the entry contains an integer
T
that represents the number of test cases. Then there are
T
test cases. The first line of a test case contains two integers
N
(1 ≤
N
≤ 50) and
K
(1 ≤
K
≤ 50) respectively representing the total number of Elves and the determined number of Elves to be boarded on the sled. The second line of a test case contains
N
integers
P
i
(1 ≤
P
i
≤ 10
18
) representing the weight in mg of the Elves. The third and last line of a test case contains two integers
A
and
B
(0 ≤
A
≤
B
≤ 10
19
) representing respectively the weight of the lighter group tested and the weight of the heaviest group tested. Both in mg.
Output
The output is composed of one line per test case containing an integer representing the number of ways to choose a group according to requirements.
Example
Input:
3
3 2
10 1 3
4 13
4 3
20 10 50 1
21 81
6 3
14 70 3 1 6 31
10 74
Output:
3
4
11 | 34,482 |
Digit Root (DIGITROOT)
A
Digit Root
is a feature of numbers used in mathematical recreation, but can also be used to check the results of simple operations such as sum and multiplication. One of its main properties is that the
Digit Root
of a number is always equal to
Digit Root
of the sum of its digits.
For example, the
Digit Root
of 18446744073709551615 is the same as 87 because 1 + 8 + 4 + 4 + 6 + 7 + 4 + 4 + 0 + 7 + 3 + 7 + 0 + 9 + 5 + 5 + 1 + 6 + 1 + 5 = 87, and is the same of 15 because 8 + 7 = 15, which in turn is the same as 6, since 1 + 5 = 6. Therefore the
Digit Root
of all these numbers is 6 since 6 is its own
Digit Root
.
Your task is, given the two integers,
B
and
E
, calculate the
Digit Root
of
B
E
(
B
raised to the
E
-th power).
Input
The input consists of two lines. The first contains the number
B
(1 ≤
B
≤ 10
10
5
). The second contains the number
E
(1 ≤
E
≤ 10
10
5
).
Output
The output consists of a single line containing the
Digit Root
.
Example
Input:
2
7
Output:
2
Input:
25
5
Output:
4
Input:
6
10
Output:
9 | 34,483 |
Magical Matrices (MAGMAT)
A magical matrix I
x
is obtained by right shifting an Identity matrix of size q exactly n times in a circular manner.
q in the above example is 3.
Bob is very fond of such matrices so he embeds them in his m x n matrix where each cell represents the value x of I
x
that he embeds. Now he is wondering if he can find a loop connecting 1's across this new matrix such that: Exactly 6 1s are connected and every time a 1 is selected it must be from the same column or row (in an alternate manner). Initially, we can choose any element having 1 and next can choose any element within same row or column. Next element must be from the same column if previously it was from the same row hence every successive selection must be in an alternate manner.
Two such paths are shown in the above image (q is taken 3). A path is considered unique if it has at least one node different in it.
Input
The first line of each input test case contains 3 integers m (1 < m ≤ 12), n (1 < n ≤ 6) and q (1 < q ≤ 100)
Next lines contain the matrix having m rows and n columns
Output
Print an integer which tells the number of such paths possible
Example
Input:
3 3 3
1 2 3
4 5 6
7 8 9
Output:
18 | 34,484 |
zig-zag on the golden river (GRZZ)
Askar is planning to steal some gold from the golden river of Slovakistan! The river forms at (0,0) and flows in the north east direction - the line
y = x
.
The river is well protected by the king's army, which is sure to give chase after him. As such, he came up with an ingenious plan - he will fly a helicopter, zig-zagging in order to shake off any pursuers. Askar will start at (0,0) and fly
d
x
meters east, then
d
y
meters north, then
d
x
meters east, then
d
y
meters north, ... forever.
Of course, he will only have a small window of opportunity to extract some gold from the river every time he crosses (or touches) it. He has yet to decide the exact values of
d
x
and
d
y
- some might give him better chances at a successful escape, others will allow him to grab more loot.
Help Askar and tell him how much he can get away with for each plan.
Input
The first line contains an integer
1 ≤ T ≤ 1000
- the number of plans.
T
lines follow, each containing two integers
1 ≤ d
x
,
d
y
≤ 10
15
.
Output
Output a single integer - the number of times Askar would cross the golden river.
If Askar crosses the river an infinite number of times, output
-1
instead.
Example
Input:
2
1 1
3 2
Output:
-1
1 | 34,485 |
Evaluate Escape Character (ESCAPE1)
Strings represented in computers often include special characters which are not printed, instead they have some other function, such as the newline character '\n'.
For ease of manipulation, these characters are usually represented by a regular character, preceded by a so called 'escape' - in the above example, the character '\'.
Buj recently bought a string
S
of length
n
on the local market, for (according to him completely legal and within the law) home manipulation.
He forgot to read the online store ratings and customer reviews.
Upong arriving at home and taking the string out of the unbelievably eco-unfriendly packaging, he realized with horror that the characters of the string do not follow any known encoding. The only thing he managed to do was number the characters from
0
to
n-1
in perceived lexicographical order.
Oh well, thought Buj, I'll still manipulate this string to my heart's content... but what if I got scammed, and sold a low-quality string? One which, if it was printed out, would be lexicographically large?
But what would be printed out depends on which character in the string is the escape.
Buj sent us the string by post for analysis, and we uploaded it to this website hoping someone would do the work for us.
Input
The first line contains an integer
1 ≤ T ≤ 10
- the number of test cases.
T
cases follow.
For each case, the first line contains the number
n
- the length of the string - and the second line contains a space-separated permutation of the numbers
0, ... , n-1
- the characters of the string.
The sum of
n
within an input file will not exceed
10
6
.
Output
In this problem, escape characters work as follows: if it is the last character in the string, it has no effect and is simply printed out as usual. Otherwise, that character and the character immediately following it is not printed (and instead together they have some other, printing-unrelated function). Take a look at the sample for clarification.
Let
s
i
be the string which would be printed if we choose character number
i
as the escape character.
Let
p
i
be the 0-based position of
s
i
if we sort all such strings lexicographically.
Output in a single line the numbers
p
0
,
p
1
, ...,
p
n-1
.
In other words, output in order the answer to the questions
"How many strings out of
s
0
, ...,
s
n-1
are lexicographically smaller than
s
0
?"
"How many are smaller than
s
1
?"
...
"How many are smaller than
s
n-1
?"
Example
Input:
2
3
0 1 2
3
0 2 1
Output:
2 0 1
2 1 0
In the first case:
If character 0 was the escape, the printed string would be 2.
If 1 was the escape, the printed string would be 0.
If 2 was the escape, the printed string would be 0 1 2.
Ordering these strings, we get [0, 0 1 2, 2].
So
s
0
is at index 2,
s
1
at index 0, and
s
2
at index 1, so the output is 2 0 1.
Note that the
i
-th number in the output is for the string where character
number i
is the escape, not the
i
-th character in the input. | 34,486 |
Faketorial Hashing (FAKEHASH)
Are you familiar with polynomial hashing? If you are not, all the better! You don’t need to know what polynomial hashing is, the world is better off without it. I hate polynomial hashing so much that I found a new way to hash strings. It is called the
Faketorial Hashing
.
First, let’s define a function,
ord(ch)
= the position of
ch
in the alphabet + 1, where
ch
can be any lowercase letter. So,
ord(a) = 2, ord(b) = 3, ord(c) = 4, … ord(z) = 27
.
Let
fact(x)
be
x!
or the factorial of x. A few examples,
fact(1) = 1, fact(2) = 2, fact(3) = 6, fact(4) = 24, fact(5) = 120
, etc.
Given a string
S
of length
N
, consisting of lowercase letters only, the
Faketorial Hashing
of S, is defined as below:
fake_hash(S) = fact(ord(S[0])) × fact(ord(S[1])) × fact(ord(S[2])) × …… × fact(ord(S[N - 1]))
In other words, it is the product of the factorial of the
ord()
value of all the characters in
S
(That’s right, no modulus! Unlike the lame polynomial hashing).
Not only that we have a new hashing mechanism in place, but we would also like to crack this now. Given a string
S
1
consisting of lowercase letters only, your task is to find a different string
S
2
consisting of lowercase letters, such that,
fake_hash(S
1
) = fake_hash(S
2
)
and
S
1
≠ S
2
.
If there are multiple possible choices for
S
2
, you need to find the
lexicographically smallest
one, or output the word
“Impossible”
without quotes, if it is not possible to find such a string.
Input
The first line contains an integer
T
, denoting the number of test cases. Each test case contains the string
S
1
consisting of lowercase letters (a-z) only.
Constraints
1 ≤ T ≤ 3000
1 ≤
|S
1
|
≤ 30
Except for the sample, the following constraints will hold:
1 ≤ |S
1
| ≤ 5, for 90% of the test cases
1 ≤ |S
1
| ≤ 15, for 99% of the test cases
Output
For each test case, output the case number followed by the required output. Please refer to the sample input/output section for the precise format.
Example
Input:
10
tourist
petr
mnbvmar
bmerry
xellos
sevenkplus
dragoon
zzz
snapdragon
zosovoghisktwnopqrstuvwxyzoos
Output:
Case 1: aaaaabbdnstttu
Case 2: aqst
Case 3: abmmnrv
Case 4: aaabbnrry
Case 5: aaaaaaadddlnuz
Case 6: aaaaaaabbddddnquuuz
Case 7: aaaaaaaaaaaaabdnnnt
Case 8: Impossible
Case 9: aaaaaaaaaabdnnnpst
Case 10: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaffffjnnnnqqttttuuuuxxzzzzzz | 34,487 |
The World of Charges (CHARGY)
In the world of charges there are 3 types of charge. Positive, negative and neutral. As always if one positive charge comes in touch with another negative charge they share charge and both of them vanishes.
Some charges has become sick today in the world of hospital. So they went to the Hospital of charges for their treatment. Unfortunately, There is a long queue in front of the doctor's room. If any charge wants to meet the doctor, he have to stand in the queue. As normal in the queue a charge comes in contact with the charge right before him and the charge right after him.
The doctor is having a tough day as you can guess. You have the appointment list of the doctor. Can you determine what is the number of patient he'll have to prescribe today? Assuming that the charges will come in the queue in the order of their appointment.
Input
First line of input contains a number n, the number of patient. Here 0 < n ≤ 100000;
The next line contains n space separated integers a
1
, a
2
, a
3
... a
n
. where a
i
=1 if the charge is positive, a
i
= -1 if the charge is negative and a
i
= 0 if the charge is neutral.
Output
In a single print the number of patient the doctor will have to prescribe.
Example
Input:
5
1 -1 1 0 1
Output:
3
Input:
6
1 -1 -1 1 1 -1
Output:
0 | 34,488 |
VALENTINE (KUMAR2019)
Kumar was very excited for his Valentine's day but at the same time he was afraid about the demands of his date.
All was going good, but at last they ordered wine, waiter offered x and y unit glasses, here she got him, she said "I will drink only z unit, it's a crime not to eat a rare steak without a good glass of red wine".
Now to get the z unit out of x and y unit of glasses, Kumar starts shuffling wine from one glass to another, as it will impress her, he had to do it quick, he doesn't want to get embarrassed by calling for other glasses, or making other excuses.
Guess how he managed to serve the wine?
Input
First line contains x and y. Next line contains q number of possible values of z his date can demand.
Next q lines contain values of z.
Output
For each z, output the minimum step required to serve z unit of wine with the available glasses even if he needs to waste some or output "Damn!!" if he can't serve.
Format for output:
Case
:
Constraints
0 ≤ x, y ≤ 1000 (Yes, 0 mean waiter doesn't even serve the glasses).
0 ≤ z ≤ 2000 (0 means, she doesn't wanted to drink at all, ordering wine was just the Kumar's choice).
Example
Input:
2 3
6
0
1
2
3
4
6
Output:
Case 1: 0
Case 2: 2
Case 3: 1
Case 4: 1
Case 5: 3
Case 6: Damn!!
Explanation
Case 2: (0, 3) → (2, 1)
Case 5: (2, 0) → (0, 2) → (2, 2)
Case 6: He can't serve. | 34,489 |
love and traffic (LTRAFFIC)
Arjun is very smart, hence has
Q
valentines.
He lives in the state
A
which is a matrix of size
N x M
, where each cell
(i, j)
represents a city. He lives in the city
(S1, S2)
. His
Q
valentines live in different cities. He is supposed to attend all his valentines but he can't do so due to constraints of time. So he decides to celebrate this festival of love with the valentine who lives closest to his city.
In a single step, he can move from any cell
(i, j)
to the
4
neighboring cells i.e.
(i+1, j), (i-1, j), (i, j+1)
and
(i, j-1)
.
But some cities have serious traffic problem. It is impossible to penetrate the traffic in those cities. So Arjun will avoid passing through them while going to his destination. These cities have value
*
. All other cities have value
1
.
He wants to find out the number of steps required to reach his closest valentine.
Since he is more of a philosopher, he struggles with the math. So it's your job now to help him find out.
Input
The first line of input contains
3
space separated integers
N
,
M
and
Q
where
N
and
M
denotes the dimensions of the matrix
A
, and
Q
denotes the number of valentines.
Each of the next
N
lines contain a string of length
M
, where each character is either
1
or
*
as described above.
The next line contains
2
space separated integers
S1
and
S2
denoting coordinates of city Arjun lives in.
then
Q
lines follow, each line containing
2
space separated integers
Di
and
Dj
denoting the coordinates of city of that particular valentine.
Output
Print a single integer
Z
denoting number of steps to reach nearest valentine. If there is no path possible between
(S1, S2)
and any of
(Di, Dj)
, print
-1
.
Example
Input:
5 4 4
11*1
*11*
*111
1111
111*
2 3
5 1
3 4
2 4
5 1
Output:
2
Explanation
To reach the nearest city, he takes the following path:
(2, 3) → (3, 3) → (3, 4)
. | 34,490 |
Bill of Fare (BILFAR)
Given a large polygon dining table (not always a simple polygon) with the following properties :
- there is no intersecting area (ex : area A)
- there is no space inside the polygon (ex : area B)
- there is no 3 edges that are concurrent (ex : point C)
- every nodes are not lying on any edge except 2 edges that connect that node with 2 other nodes (ex : point D)
- every nodes forming a convex corner because a table with concave corner is an uncomfortable table (ex : point E)
Example of invalid table :
Given also M dishes with the following rules :
- placed on the table
- not on the edge of the table
- there is no pair of different food that have the same place
You have to answer Q queries :
- each query identified by L and R
- the query is "what is the minimum moves in order to make some dishes (from L-th dish to R-th dish inclusive) placed in same region ?"
- queries are independent
Notes :
- two dishes are considered in same region if and only if from one dish can be slid to another one without crossing any edge
- one move is to slide a dish to another region through an edge
- every dishes should be still on the table, but they may lie on the edge
Explanation :
- dish A is valid because it placed on the table
- dish C is invalid because it placed on the edge
- dish E is invalid because it placed outside the table
- sliding from dish A to dish B is considered as one move
- dish B and dish D is considered as one region
- dish F is invalid because it placed exactly on dish D
Input and output format :
- An integer T represent the number of test case, each test case :
- First line contains 3 separated integer N, M, and Q
- Next N lines contain Xi and Yi represent the coordinate of i-th node
- Next M lines contain Pi and Qi represent the coordinate of i-th dish
- Next Q lines contain Li and Ri represent the parameter of i-th query
- You should output Q lines contain the answers of those queries
Constraints :
- 1 <= T <= 10
- 3 <= N <= 1000
- 2 <= M <= 1000
- 1 <= Q <= 1000
- 0 <= Xi, Yi <= 10^9
- 0 < Pi, Qi < 10^9
- 1 <= Li < Ri <= M
Sample input :
1
7 5 3
1 1
1 5
5 1
7 2
7 8
9 5
5 5
6 2
2 3
5 4
8 6
5 3
1 5
2 4
3 5
Sample output :
2
2
1
Explanation of sample :
- query 1 : we can slide 2-nd dish and 4-th dish to the middle region
- query 2 : using the same way as query 1
- query 3 : prefer sliding 4-th dish (1 move) rather than sliding 3-rd and 5-th dishes (2 moves) | 34,491 |
Fibonacci Power Sum (FIBPWSUM)
The fibonacci series is defined as below:
fib(0) = 0, fib(1) = 1
fib(n) = fib(n-1) + fib(n-2) for n > 1
Given three integers
N
,
C
and
K
, find the summation of the following series:
fib(0*C)^K + fib(1*C)^K + fib(2*C)^K + fib(3*C)^K + … + fib(N*C)^K
Since the answer can be huge, output it modulo
1000000007
Input
The first line contains an integer
T
, denoting the number of test cases. Each test case contains three space separated integers in the order:
N
,
C
and
K
.
Constraints
1 ≤ T ≤ 100
0 ≤
N
≤ 10
15
1 ≤ C, K ≤ 10
Output
For each test case, output a single line in the format
“Case X: Y”
without the quotes. Here,
X
is the case number and
Y
is the desired answer denoting the sum of the series.
Example
Input:
5
10 1 1
5 2 2
3 3 4
1000000007 7 9
996969696969696 9 6
Output:
Case 1: 143
Case 2: 3540
Case 3: 1340448
Case 4: 880410497
Case 5: 689328397
Challenge
Try the harder version here:
liouzhou_101 - FIBPSUM2 | 34,492 |
Fibonacci Power Sum (hard) (FIBPSUM2)
This problem is a harder version of
FIBPWSUM
.
The Fibonacci numbers is defined by
$$ f_0=0, f_1=1, $$
and
$$ f_n = f_{n-1}+f_{n-2} $$
for $n > 1$.
Given three integers $N$, $C$ and $K$, compute the summation
$$ \sum_{n=0}^N f_{Cn}^K. $$
Since the answer can be huge, output it modulo $10^9+7$.
Input
The first line contains an integer $T$, denoting the number of test cases. Each test case contains three space separated integers in the order: $N$, $C$ and $K$.
Constraints
$1 \leq T \leq 100$
$1 \leq N, C \leq 10^{18}$
$1 \leq K \leq 10^5$
Output
For each test case, output a single line in the format "
Case X: Y
" without the quotes. Here, X is the case number and Y is the desired answer denoting the sum of the series.
Example
Input:
5
10 1 1
5 2 2
3 3 4
1000000007 7 9
996969696969696 9 6
Output:
Case 1: 143
Case 2: 3540
Case 3: 1340448
Case 4: 880410497
Case 5: 689328397
Credits
sgtlaugh
-
FIBPWSUM
ZOJ 3774. Power of Fibonacci
Information
There are two test files. The first file is randomly generated while the second file is not.
@Speed Adicts: My solution runs in 1.94s. (approx less than 1s per file) | 34,493 |
Moon Safari (Extreme) (MOON4)
This problem is a harder version of
MOON2
.
Your task is: given $N$, $a$ and $r$, compute
$$ S(N, a, r) = \sum_{i=1}^N a^i i^r. $$
Input
The first line contains an integer $T$
, the number of test cases.
On the next $T$
lines, you will be given three integers $N$
, $a$
and $r$
.
Output
Output $T$
lines, one for each test case, with $S(N, a, r)$.
Since the answer can get very big, output it modulo $10^9+7$
.
Example
Input:
2
3 4 5
6 7 8
Output:
16068
329990641
Constraints
Overall constraints:
$1 \leq T \leq 10^5$
$1 \leq N \leq 10^{18}$
$1 \leq a \leq 10^{18}$
$1 \leq r \leq 10^8$
More precise information: there are 6 test cases.
Test #0: $1 \leq T \leq 100000$ and $1 \leq r \leq 1000$.
Test #1:
$1 \leq T \leq 10000$ and $1 \leq r \leq 10000$.
Test #2:
$1 \leq T \leq 1000$ and $1 \leq r \leq 100000$.
Test #3:
$1 \leq T \leq 100$ and $1 \leq r \leq 1000000$.
Test #4:
$1 \leq T \leq 10$ and $1 \leq r \leq 10000000$.
Test #5:
$T = 1$ and $1 \leq r \leq 100000000$.
Information
Four trips on the moon are provided,
Moon
(easy),
Moon1
(medium),
Moon2
(hard),
Moon4
(extreme) with different constraints.
Please pay attention to the constraints which may differ from the previous versions.
Also please handle the constraints carefully.
We do not provide the intended time complexity in order to encourage possible various ways of thinking.
My fastest C++ code got AC under in 12.14s. (approx 2.02s per file)
Good luck and have fun :-)
You may be surprised why the code of this problem is not
Moon3
. It is because
4 is a lucky number on the moon.
This problem is the 4th one in the Moon series.
4 is a power of 2, which indicates exponential increasing difficulty starting from 2.
Moon3 has been used. | 34,494 |
Consecutive Letters (CONSEC)
You are given a string S containing only uppercase English letters. There are Q queries. Each query can be of two types:
1 i
: Find the maximum size of the segment
[b, e]
where
0 ≤ b ≤ i ≤ e < |S|
and substring
S[b...e]
contains only the letter
S[i]
. A Substring is a contiguous sequence of characters in a string.
2 i
: Change the character in index i with the character ‘#’.
For both type of queries,
S[i]
will not contain the character ‘#’.The characters of the string are indexed from 0.
Input
The first line contains number of test cases
T (1 ≤ T ≤ 15)
.
For each test cases, the first line contains the string
S (1 ≤ |S| ≤ 200000)
. The 2
nd
line contains number of queries
Q (1 ≤ Q ≤ 100000)
. Each of the next
Q
lines contains one query in the format mentioned in the problem statement.
Output
For each test case, first print the test case number and output of every query of type 1 in a single line.
Sample
Input
2
AABBBCCCC
5
1 0
2 1
1 0
2 2
1 3
XXYYY
3
1 3
2 3
1 2
Output
Case 1:
2
1
2
Case 2:
3
1
Warning: The input file is huge, please use fast I/O.
Note: The dataset and time limit have been modified to fit SPOJ. | 34,495 |
Try to learn properly (LDP)
Most of the programmers like the problem description to be as short as possible, right? I also never like the problem description to be so narrative.
In this problem, you will be given an array
a
of n integers. If we multiply all the numbers of the array then we will get a result. Suppose the result is
K
.
Let's introduce another list of all the common multiples of an array as
cmp
. Definitely, the list has an infinite number of elements.
Suppose, we have an array
a
= {2, 3, 6}. The result of multiplication of 2, 3 and 6 is 36. So
K
= 36.
The 1st common multiple of
a
is 6, the 2nd common multiple is 12, 3rd is 18, and so on.
So the list,
cmp = {6, 12, 18, 24, 30, 36, 42, ....... }.
Now the question is what the position of
K in the cmp list
? (1-based indexing)
As the result can be very big, you have to print
K
%
1000000009
(
10
9
+ 9
), where % is modulo operator.
Note: In the above-described example, the position of
K
is 6. (
cmp
6
= K = 36).
Input
The first line of the input will be
n
, the number of elements in the array.
In the next line,
n
elements of the array
a
1
,
a
2
,
a
3
...
a
n
will be given.
Output
Print a single integer, the result of the problem described above with a new line.
Constraints
0 < n ≤ 10
5
0 <
a
i
≤ 10
9
(1 ≤
i
≤ n)
Example
Input:
3
6 10 8
Output:
4 | 34,496 |
COLORFUL ARRAY (CLFLARR)
You have been given an array of
n
unpainted elements. By unpainted, we mean that each element initially has a value of
0
. You have to process
q
queries of the form
l r c
, in which you paint all the elements of the array from index
l
to index
r
with color
c
. Assume that, each new color currently being applied to an element overrides its previous color. Output the color of each element after all the queries have been processed.
Note
: The problem is guaranteed to be solved using C or C++ programming language.
Input
The first line of input consists of two integers n and q. Next q lines consists of 3 integers l, r and c denoting the starting index, ending index and the color respectively.
1 <=
n
<= 200000
1 <=
q
<= 200000
1 <=
l
<=
r
<=
n
1 <=
c
<= 1 000 000 000
Output
Output the final color of each element starting from index 1 on a new line.
Example
Input:
4 3
1 3 2
2 4 6
2 3 7
Output:
2
7
7
6
Input:
10 5
3 9 13
1 4 9
2 10 14
2 7 10
6 9 44
Output:
9
10
10
10
10
44
44
44
44
14 | 34,497 |
Primes in GCD Table (Hard) (PGCD2)
This problem is a harder version of
PGCD
.
Let $P$ be the set of all prime numbers. For two positive integers $n$ and $m$, define
$$ f(n,m) = \sum_{i=1}^n \sum_{j=1}^m [\gcd(i,j) \in P], $$
which counts the number of prime numbers among the greatest common divisors $\gcd(i,j)$ for $1 \leq i \leq n$ and $1 \leq j \leq m$.
Your task: given $n$ and $m$, compute $f(n,m)$.
Input
The first line contains an integer $T$, indicating the number of test cases.
Each of the next $T$ lines contains two positive integers $n$ and $m$.
Output
For each test case, print $f(n, m)$ in a single line.
Example
Input:
4
10 10
100 100
123456789 987654321
233333333333 233333333333
Output:
30
2791
33523360713808196
14968599673221238693021
Constraints
There are 6 test files.
Test #0: $1 \leq T \leq 10000$, $1 \leq n, m \leq 10^7$.
Test #1: $1 \leq T \leq 200$, $1 \leq n, m \leq 10^8$.
Test #2: $1 \leq T \leq 40$, $1 \leq n, m \leq 10^9$.
Test #3: $1 \leq T \leq 10$, $1 \leq n, m \leq 10^{10}$.
Test #4: $1 \leq T \leq 2$, $1 \leq n, m \leq 10^{11}$.
Test #5: $T = 1$, $1 \leq n, m \leq 235711131719$.
@Speed Addicts: My solution runs in 4.87s (total time). (approx 0.81s per file) | 34,498 |
Trending GCD (Hard) (TGCD2)
This problem is a harder version of
TRENDGCD
.
Given $n$ and $m$, compute
$$ S(n, m) = \sum_{i=1}^n \sum_{j=1}^m ij \cdot f(\gcd(i,j)), $$
where $f(n) = (\mu(n))^2 n$ and $\mu(n)$ is the Möbius function, that is, $f(n) = n$ if $n$ is square-free and $0$ otherwise. Especially, $f(1)=1$.
Input
The first line contains an integer $T$, indicating the number of test cases.
Each of the next $T$ lines contains two positive integers $n$ and $m$.
Output
For each test case, print $S(n, m)$ modulo $10^9+7$ in a single line.
Example
Input:
5
42 18
35 1
20 25
123456789 987654321
233333333333 233333333333
Output:
306395
630
128819
897063534
355737203
Constraints
There are 6 test files.
Test #0: $1 \leq T \leq 10000$, $1 \leq n, m \leq 10^7$.
Test #1: $1 \leq T \leq 200$, $1 \leq n, m \leq 10^8$.
Test #2: $1 \leq T \leq 40$, $1 \leq n, m \leq 10^9$.
Test #3: $1 \leq T \leq 10$, $1 \leq n, m \leq 10^{10}$.
Test #4: $1 \leq T \leq 2$, $1 \leq n, m \leq 10^{11}$.
Test #5: $T = 1$, $1 \leq n, m \leq 235711131719$.
@Speed Addicts: My solution runs in 20.76s (total time). (approx 3.46s per file)
WARNING
: The time limit may be somewhat strict. | 34,499 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.