uva1591

maksyuki 发表于 oj 分类,标签:
0

Dr. Tuple is working on the new data-mining application for Advanced Commercial Merchandise Inc. One of the subroutines for this application works with two arrays P and Q containing N records of data each (records are numbered from 0 to N − 1). Array P contains hash-like structure with keys. Array P is used to locate record for processing and the data for the corresponding record is later retrieved from the array Q.

All records in array P have a size of SP bytes and records in array Q have size of SQ bytes. Dr. Tuple needs to implement this subroutine with the highest possible performance because it is a hot-spot of the whole data-mining application. However, SP and SQ are only known at run-time of application which complicates or makes impossible to make certain well-known compile-time optimizations.

The straightforward way to find byte-offset of i-th record in array P is to use the following formula:

Pofs(i) = SP · i, (1)

and the following formula for array Q:

Qofs(i) = SQ · i. (2)

However, multiplication computes much slower than addition or subtraction in modern processors. Dr. Tuple avoids usage of multiplication while scanning array P by keeping computed byte-offset Pofs(i) of i-th record instead of its index i in all other data-structures of data-mining application. He uses the following simple formulae when he needs to compute byte-offset of the record that precedes or follows i-th record in array P:

Pofs(i + 1) = Pofs(i) + SP

Pofs(i − 1) = Pofs(i) − SP

Whenever a record from array P is located by either scanning of the array or by taking Pofs(i) from other data structures, Dr. Tuple needs to retrieve information from the corresponding record in array Q. To access record in array Q its byte-offset Qofs(i) needs to be computed. One can immediately derive formula to compute Qofs(i) with known Pofs(i) from formulae (1) and (2):

Qofs(i) = Pofs(i)/SP · SQ (3)

Unfortunately, this formula not only contains multiplication, but also contains division. Even though only integer division is required here, it is still an order of magnitude slower than multiplication on modern processors. If coded this way, its computation is going to consume the most of CPU time in data-mining application for ACM Inc.

After some research Dr. Tuple has discovered that he can replace formula (3) with the following fast formula:

Qofs’(i) = (Pofs(i) + Pofs(i) << A) >> B (4)

where A and B are non-negative integer numbers, “<< A” is left shift by A bits (equivalent to integer multiplication by 2A), “ >> B” is right shift by B bits (equivalent to integer division by 2B).

This formula is an order of magnitude faster than (3) to compute, but it generally cannot always produce the same result as (3) regardless of the choice for values of A and B. It still can be used if one is willing to sacrifice some extra memory.

Conventional layout of array Q in memory (using formula (2)) requires N · SQ bytes to store the entire array. Dr. Tuple has found that one can always choose such K that if he allocates K bytes of memory for the array Q (where K ≤ N · SQ) and carefully selects values for A and B, the fast formula (4) will give non-overlapping storage locations for each of the N records of array Q.

Your task is to write a program that finds minimal possible amount of memory K that needs to be allocated for array Q when formula (4) is used. Corresponding values for A and B are also to be found. If multiple pairs of values for A and B give the same minimal amount of memory K, then the pair where A is minimal have to be found, and if there is still several possibilities, the one where B is minimal. You shall assume that integer registers that will be used to compute formula (4) are wide enough so that overflow will never occur.

Input

Input consists of several datasets. Each dataset consists of three integer numbers N, SP , and SQ separated by spaces (1 ≤ N ≤ 2 20 , 1 ≤ SP ≤ 2 10 , 1 ≤ SQ ≤ 2 10).

Output

For each dataset, write to the output file a single line with three integer numbers K, A, and B separated by spaces.

Sample Input

20 3 5
1024 7 1

Sample Output

119 0 0

1119 2 5

 

题目类型;简单枚举

算法分析:按照题目要求对A和B暴力枚举即可

 

uva12108

maksyuki 发表于 oj 分类,标签:
0

When a student is too tired, he can’t help sleeping in class, even if his favorite teacher is right here in front of him. Imagine you have a class of extraordinarily tired students, how long do you have to wait, before all the students are listening to you and won’t sleep any more? In order to complete this task, you need to understand how students behave.

When a student is awaken, he struggles for a minutes listening to the teacher (after all, it’s too bad to sleep all the time). After that, he counts the number of awaken and sleeping students (including himself). If there are strictly more sleeping students than awaken students, he sleeps for b minutes. Otherwise, he struggles for another a minutes, because he knew that when there is only very few sleeping students, there is a big chance for them to be punished! Note that a student counts the number of sleeping students only when he wants to sleep again.

Now that you understand each student could be described by two integers a and b, the length of awaken and sleeping period. If there are always more sleeping students, these two periods continue again and again. We combine an awaken period with a sleeping period after it, and call the combined period an awaken-sleeping period. For example, a student with a = 1 and b = 4 has an awaken-sleeping period of awaken-sleeping-sleeping-sleeping-sleeping. In this problem, we need another parameter c (1 ≤ c ≤ a + b) to describe a student’s initial condition: the initial position in his awaken-sleeping period. The 1st and 2nd position of the period discussed above are awaken and sleeping, respectively.

Now we use a triple (a, b, c) to describe a student. Suppose there are three students (2, 4, 1), (1, 5, 2) and (1, 4, 3), all the students will be awaken at time 18. The details are shown in the table below.

Write a program to calculate the first time when all the students are not sleeping.

Input

The input consists of several test cases. The first line of each case contains a single integer n (1 ≤ n ≤ 10), the number of students. This is followed by n lines, each describing a student. Each of these lines contains three integers a, b, c (1 ≤ a, b ≤ 5), described above. The last test case is followed by a single zero, which should not be processed.

Output

For each test case, print the case number and the first time all the students are awaken. If it’ll never happen, output ‘-1’.

Sample Input

3
2 4 1
1 5 2
1 4 3
3
1 2 1
1 2 2
1 2 3
0

Sample Output

Case 1: 18

Case 2: -1

 

题目类型:简单模拟

算法分析:最大枚举的范围为所有成员周期之积,超出即输出-1,然后对于每个时间位置模拟每个人的操作即可