ICPC Preli-2018 Problem Set
ICPC Preli-2018 Problem Set
1
Welcome
A Input: Standard Input
Output: Standard Output
ACM ICPC Dhaka Regional is the biggest programming competition in Bangladesh. Also the most
anticipated one as well. Students from all the different universities storm their brains all year in
preparation for this competition. You are now taking part in this competition, so a big
congratulations to you.
Dhaka site is one of the biggest sites in ACM ICPC. This year almost 1500 teams are participating in
the competition. So in celebration to that, we are going to give you an easy problem to start with.
You have to write a program, which will print the line “Welcome to ACM ICPC Dhaka Regional
Online Preliminary Contest, 2018” (without quotes).
Note: you can’t output anything other than the required output, and each line must end with a
newline (‘\n’). Take special care about spelling and case. If you alter any of those, you may not get
accepted.
For your convenience, we are providing one sample program in C/C++ which prints “Bangladesh”.
You just have to change the code to your requirement.
#include <stdio.h>
int main()
{
printf("Bangladesh\n");
return 0;
}
2
Boring Chess
B Input: Standard Input
Output: Standard Output
In this problem, you are given the current position of a knight (r, c). You have to find, the number
of different cells where the knight can go in a single move from its current position.
Input
Input starts with an integer T (1 ≤ T ≤ 64) denoting the number of test cases. Following T lines will
contain two integers r, c where 1 ≤ r, c ≤ 8.
Output
For each test case, output the number of different cells the knight can go in a single move. For more
clarity, see sample input/output.
3
Odd is real
C Input: Standard Input
Output: Standard Output
Given a range [L, R], find the number of integers in that range which have an odd number of odd
divisors. For example, 1 has only one divisor and it is odd, 9 has three divisors {1, 3, 9} and all of
them are odd. Meanwhile, 18 has six divisors {1, 2, 3, 6, 9, 18} but three of them are odd. So 1, 9
and 18 have an odd number of odd divisors.
Input
Input will start with a positive integer T (T ≤ 105 ) denoting the number of test cases. Each test case
will have two positive integers L, R (1 ≤ L ≤ R ≤ 1018), the range.
Output
For each test case, the first line will be the case number in the format “Case t: x” without the
quotes. Here t is the case number starting from 1 and x is the number of integers that fall in the
range [L, R] and have an odd number of odd divisors.
4
Prime Friendly Numbers
D Input: Standard Input
Output: Standard Output
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:
Input
The first line contains an integer T, denoting the number of test cases. Each test case contains a
single positive integer N.
Output
For each test case, output the case number followed by the largest number X, not greater N. Please
refer to the sample input/output section for more clarity of the format.
Constraints:
1 ≤ T ≤ 1000
5
The End
E Input: Standard Input
Output: Standard Output
Output
For each test case, output the answer in a single line according to the problem. Since the answer can
be very large, simply output the answer modulo 1000000007 (109 + 7).
6
Sample Input Output for Sample Input
3 6
3 3 0
3 1 5
1 3
2 2
3 1
3 3
3 0
3 1
2 2
1 3
3 3
2 1
1 2
1 3
7
You are given a string S and Q queries. On the ith
query, you will be given two integers Ni and Mi.
Now for each query, you have to find such strings consisting of lowercase letters whose lengths are
at least Ni and at most Mi and also those strings are not substrings of S.
For example:
You are given S=“abac”. If a query contains Ni=3 and Mi=4 then according to our problem,
“aba”, “bac” and “abac” are not valid as they are substrings of S while “abc” is a valid string as it
is not a substring of S and also maintaining the constraints for the length. Now you have to find all
other strings which are also valid for this string S.
To make the problem a bit simple, you don’t have to print all the strings. You will just need to print
the number of such strings which are valid. To make the output more simple, print the answer
modulo 10^9+7.
Input
The first line of the input file will be a single integer T (1 ≤ T ≤ 5), denoting the number of test
cases.
Each test case contains two lines. The first line will contain the string S (1 ≤ |S| ≤ 1000000) of
lowercase letters and the second line will contain an integer Q (1 ≤ Q ≤ 100000). Each of the next Q
lines will contain two integers Ni and Mi (1 ≤ Ni ≤ Mi ≤ |S|).
Output
For each test case, the first line of the output should contain the case number in the format: “Case
X:”, where X is the test case number. Each of the next Q lines should contain the answer for the
specific query modulo 1000000007 (109+7).
8
Subset with GCD K
G Input: Standard Input
Output: Standard Output
Given a set of N positive distinct integers and a query value K, find if there exists a subset of the
integers whose greatest common division equals to K.
For example, if the given set is {1, 6, 2, 9, 8}, then the answer for the following values of K are:
Value of K Existence of Subset Possible Subset
1 Yes {1}
2 Yes {2,6}
3 Yes {6,9}
4 No -
Input
The first line contains a single integer N (1 ≤ N ≤ 100000). The next line contains N positive
distinct integers separated by whitespace. These N integers represent the set as mentioned above.
The third line contains a single integer Q (1 ≤ Q ≤ 1000). The next line contains Q distinct positive
integers, where each integer is a query K.
All values given will be less than or equal to 109. See sample I/O for more details.
Output
For each query, output in a single line the character “Y” if a subset exists whose GCD equals to K or
“N” if not, without the quotes.
See sample I/O for more details.
9
Colorful Balls
H Input: Standard Input
Output: Standard Output
Input
The first line contains the number of test cases T (1≤T ≤1000). In each line of the test cases, there
will be a string S of length N (1≤N ≤100000).
The total number of character in the input file will be less than 5 * 106.
Output
For each test case, print the case number and the answer to the problem.
Explanation for case 1: The four valid ways to color the balls are RBG, BRG, GRG, GBG.
10
Vugol Search
I Input: Standard Input
Output: Standard Output
"Did You Mean…" is a search engine function that scans for potential spelling or grammatical
errors in user queries and recommends alternative keywords, similar to the auto-correction feature
found in mobile messaging services. While the feature is designed to assist users in refining their
search results, it has been frequently exploited by Vugol search users for comedic purposes. In
Vugol search engine, people can search exactly one word at a time.
There are N words in Vugol's database. Each word has its Smartness value. If someone does search
in the search engine with a word then Vugol search engine calculates the score of each N words with
respect to the searched word and suggest the word which has the maximum score with respect to
searched word. If the searched word is A, the score is calculated with respect to word B as,
Input
The first line contains an integer T, denoting the number of test cases.
For each test case:
● The first line contains an integer N denoting the number of words.
● The second line contains an array of N-space separated positive integers denoting Smartness
value of words.
● Next N lines contain a word.
● Next line contains an integer number Q, denoting the number of queries.
● Next, Q lines contain a query word.
11
Constraints
● 1 ≤ T ≤ 10
● 1 ≤ N, Q, Smartness value ≤ 105
● A word consists of only lowercase English alphabets.
● For each test case, the sum of all words’ length is within 2 * 105, including query words.
Output
For each query, print the maximum score of a word that Vugol suggests.
Sample Input Output for Sample Input
1 12
4 10
5 2 4 10 11
google 0
oooogp 3
googoo
goloeg
5
google
lgooeg
glooeg
poops
goopoo
N.B. Dataset is large. Use faster I/O methods.
12
You will be given a graph of N nodes connected by N-1 edges where all the nodes are directly or
indirectly connected together. Now we want to modify the graph by making the edges directed. You
can visit a node from another one following a path formed by these directed edges. While
transforming the graph into a directed one, we also want to minimize the length of the longest
possible path in the graph.
You have to determine the direction of each edge to achieve this goal.
Input
There will be several test cases, T (1 ≤ T ≤ 20). For each test case, the first line will contain N (2 ≤
N ≤ 100000) denoting the number of nodes. Each of the following N-1 lines will contain two
integers u, v (1 ≤ u, v ≤ N) denoting an undirected edge between nodes u and v.
Output
For each test case, print a line in the format “Case X:” where X is the integer denoting the test case
number starting from 1, then print N-1 lines, each containing two integers u and v denoting that
there is a directed edge from node u to v in the graph after the transformation as described above. If
there are more than one solutions, print any of them, and print in any order you want.
13