Google Kick Start Archive — Round E 2016 problems

Overview

A. Diwali lightings

Problem

Diwali is the festival of lights. To celebrate it, people decorate their houses with multi-color lights and burst crackers. Everyone loves Diwali, and so does Pari. Pari is very fond of lights, and has transfinite powers, so she buys an infinite number of red and blue light bulbs. As a programmer, she also loves patterns, so she arranges her lights by infinitely repeating a given finite pattern S.

For example, if S is BBRB, the infinite sequence Pari builds would be BBRBBBRBBBRB...

Blue is Pari's favorite color, so she wants to know the number of blue bulbs between the Ith bulb and Jth bulb, inclusive, in the infinite sequence she built (lights are numbered with consecutive integers starting from 1). In the sequence above, the indices would be numbered as follows:

B  B  R  B  B  B  R  B  B  B  R  B...
1  2  3  4  5  6  7  8  9  10 11 12

So, for example, there are 4 blue lights between the 4th and 8th positions, but only 2 between the 10th and 12th.

Since the sequence can be very long, she wrote a program to do the count for her. Can you do the same?

Input

The first line of the input gives the number of test cases, T. T test cases follow.
First line of each test case consists of a string S, denoting the initial finite pattern.
Second line of each test case consists of two space separated integers I and J, defined above.

Output

For each test case, output one line containing Case #x: y, where x is the test case number (starting from 1) and y is number of blue bulbs between the Ith bulb and Jth bulb of Pari's infinite sequence, inclusive.

Limits

1 ≤ T ≤ 100.
Time limit: 30 seconds per test set.
Memory limit: 1GB.
1 ≤ length of S ≤ 100.
Each character of S is either uppercase B or uppercase R.

Small dataset (Test set 1 - Visible)

1 ≤ IJ ≤ 106.

Large dataset (Test set 2 - Hidden)

1 ≤ IJ ≤ 1018.

Sample

Sample Input
content_copy Copied!
3
BBRB
4 8
BBRB
10 12
BR
1 1000000
Sample Output
content_copy Copied!
Case #1: 4
Case #2: 2
Case #3: 500000

Cases #1 and #2 are explained above.

In Case #3, bulbs at odd indices are always blue, and bulbs at even indices are always red, so there are half a million blue bulbs between positions 1 and 106.

B. Beautiful Numbers

Problem

We consider a number to be beautiful if it consists only of the digit 1 repeated one or more times. Not all numbers are beautiful, but we can make any base 10 positive integer beautiful by writing it in another base.

Given an integer N, can you find a base B (with B > 1) to write it in such that all of its digits become 1? If there are multiple bases that satisfy this property, choose the one that maximizes the number of 1 digits.

Input

The first line of the input gives the number of test cases, T. T test cases follow. Each test case consists of one line with an integer N.

Output

For each test case, output one line containing Case #x: y, where x is the test case number (starting from 1) and y is the base described in the problem statement.

Limits

1 ≤ T ≤ 100.
Time limit: 30 seconds per test set.
Memory limit: 1GB.

Small dataset (Test set 1 - Visible)

3 ≤ N ≤ 1000.

Large dataset (Test set 2 - Hidden)

3 ≤ N ≤ 1018.

Sample

Sample Input
content_copy Copied!
2
3
13
Sample Output
content_copy Copied!
Case #1: 2
Case #2: 3

In case #1, the optimal solution is to write 3 as 11 in base 2.

In case #2, the optimal solution is to write 13 as 111 in base 3. Note that we could also write 13 as 11 in base 12 or as 1 in base 13, but neither of those representations has as many 1s.

C. Partioning Number

Problem

Shekhu has N balls. She wants to distribute them among one or more buckets in a way that satisfies all of these constraints:

  1. The numbers of balls in the buckets must be in non-decreasing order when read from left to right.
  2. The leftmost bucket must be non-empty and the number of balls in the leftmost bucket must be divisible by D.
  3. The difference (in number of balls) between any two buckets (not just any two adjacent buckets) must be less than or equal to 2.

How many different ways are there for Shekhu to do this? Two ways are considered different if the lists of numbers of balls in buckets, reading left to right, are different.

Input

The first line of the input gives the number of test cases, T.
T test cases follow. Each test case consists of one line with two integers N and D, as described above.

Output

For each test case, output one line containing Case #x: y, where x is the test case number (starting from 1) and y is the answer, as described above.

Limits

1 ≤ T ≤ 100.
Time limit: 30 seconds per test set.
Memory limit: 1GB.
1 ≤ D ≤ 100.

Small dataset (Test set 1 - Visible)

1 ≤ N ≤ 2000.

Large dataset (Test set 2 - Hidden)

1 ≤ N ≤ 105.

Sample

Sample Input
content_copy Copied!
3
7 1
7 2
2 4
Sample Output
content_copy Copied!
Case #1: 10
Case #2: 1
Case #3: 0

In sample case #1, the possible distributions are:

  • 1 1 1 1 1 1 1
  • 1 1 1 1 1 2
  • 1 1 1 1 3
  • 1 1 1 2 2
  • 1 2 2 2
  • 1 1 2 3
  • 1 3 3
  • 2 2 3
  • 3 4
  • 7

Note that 1 2 4 is not a valid distribution, since the difference between 1 and 4 is more than 2.

In sample case #2, the possible distributions are:

  • 2 2 3

3 4 is not possible, since the first term is not divisible by 2.

In sample case #3, no possible arrangement exists.

D. Sorting Array

Problem

We are in the process of creating a somehow esoteric sorting algorithm to sort an array A of all integers between 1 and N. The integers in A can start in an arbitrary order. Besides the input order, the algorithm depends on two integers P(which would be at most 3) and K. Here is how the algorithms works:

  1. Partition A into K disjoint non-empty subarrays A1, A2, ..., AK such that such that concatenating them in order A1A2 ... AK produces A.
  2. Sort each subarray individually.
  3. Choose up to P of the subarrays, and swap any two of them any number of times.

For example, consider A = [1 5 4 3 2] and P = 2. A possible partition into K = 4 disjoint subarrays is:

A1 = [1]
A2 = [5]
A3 = [4]
A4 = [3 2]

After Sorting Each Subarray:
A1 = [1] A2 = [5] A3 = [4] A4 = [2 3] After swapping A4 and A2:
A1 = [1] A2 = [2 3] A3 = [4] A4 = [5]

We want to show the algorithm is good for distributed environments by finding, for a fixed input and value of P, the maximum number of partitions K such that, choosing the partitions and swaps wisely, we can achieve a sorting of the original order. Can you help us to calculate that K?

Input

The first line of the input gives the number of test cases, T.
T test cases follow. Each test case consists of two lines. The first line contains two integers N and P, as described above.
The second line of the test case contains N integers X1, X2, ..., XN represting array A.

Output

For each test case, output one line containing Case #x: y, where x is the test case number (starting from 1) and y is the maximum possible value for the parameter K.

Limits

1 ≤ T ≤ 100.
Time limit: 40 seconds per test set.
Memory limit: 1GB.
1 ≤ N ≤ 5000.
1 ≤ XiN, for all i.
XiXj for all i ≠ j.

Small dataset (Test set 1 - Visible)

P = 2.

Large dataset (Test set 2 - Hidden)

P = 3.

Sample

Sample Input
content_copy Copied!
5
5 2
1 5 4 3 2
5 2
4 5 1 2 3
6 2
6 3 5 2 4 1
5 3
4 5 1 2 3
6 3
1 2 6 4 5 3
Sample Output
content_copy Copied!
Case #1: 4
Case #2: 2
Case #3: 3
Case #4: 3
Case #5: 6

Case #1:
Same as walk through in the statement.

Case #2:
[4 5] [1 2 3]
Swap the 2 blocks: [1 2 3] [4 5]

Case #3:
[6] [3 5 2 4] [1]
Sort [3 5 2 4], then swap [6] and [1], we get: [1] [2 3 4 5] [6]

Case #4:
[4 5] [1] [2 3]
Swap [4 5] and [1], then swap [2 3] and [4 5]: [1] [2 3] [4 5]

Case #5:
[1] [2] [6] [4] [5] [3]
Swap [6] and [3]: [1] [2] [3] [4] [5] [6]

Note: First 3 sample cases would not appear in the Large dataset and the last 2 sample cases would not appear in the Small dataset.

Analysis — A. Diwali lightings

Test Data
info We recommend that you practice debugging solutions without looking at the test data.

Analysis — B. Beautiful Numbers

Test Data
info We recommend that you practice debugging solutions without looking at the test data.

Analysis — C. Partioning Number

Test Data
info We recommend that you practice debugging solutions without looking at the test data.

Analysis — D. Sorting Array

Test Data
info We recommend that you practice debugging solutions without looking at the test data.

Statistics — A. Diwali lightings

Test set 1: 1615 correct solutions (93.6% solve rate)

First
ypliu 5:37
daip 8:54
BishalG 9:38
aliange 10:17
forgot93 11:20

Test set 2: 1262 correct solutions (73.2% solve rate)

First
ypliu 5:37
daip 8:54
BishalG 9:38
aliange 10:17
forgot93 11:20

Statistics — B. Beautiful Numbers

Test set 1: 1429 correct solutions (82.8% solve rate)

First
adtac 22:09
daip 22:51
ankitDubey 23:06
man.ish 24:51
imranziad 25:07

Test set 2: 211 correct solutions (12.2% solve rate)

First
ypliu 32:25
Georeth.0v0 36:26
Amused brown hedgehog 38:10
RDipak 64:30
aayushkapadia 64:45

Statistics — C. Partioning Number

Test set 1: 646 correct solutions (37.4% solve rate)

First
daip 61:41
shdut0901 64:53
kesagar94 71:56
冷月冰瞳 76:34
jamesjaya 79:49

Test set 2: 193 correct solutions (11.2% solve rate)

First
daip 61:41
shdut0901 64:53
冷月冰瞳 76:34
Hewr 83:27
Amused brown hedgehog 86:45

Statistics — D. Sorting Array

Test set 1: 5 correct solutions (0.3% solve rate)

First
ypliu 201:04
wcwswswws 214:20
legedexinshi 220:05
LittleBuger 234:14
HELLO.ZZ 235:08

Test set 2: 2 correct solutions (0.1% solve rate)

First
ypliu 201:04
LittleBuger 234:14