# | Problem | Pass Rate (passed user / total user) |
---|---|---|
12486 | Tower of Hanoi |
|
14438 | GCD but one missing |
|
Description
The Tower of Hanoi is a mathematical game puzzle. It consists of three rods, which are A, B and C. The puzzle starts with n disks in ascending order of size on rod A, the smallest at the top.
The objective of the puzzle is to move the entire stack to another rod, obeying the following simple rules:
1. Only one disk can be moved at a time.
2. Each move consists of taking the upper disk from one of the stacks and placing it on top of another stack i.e. a disk can only be moved if it is the uppermost disk on a stack.
3. No disk may be placed on top of a smaller disk.
Write a program to simulate the optimal moves of the disks. Print the number of disk which is moved in each step.
For example, if n = 3, the moves of each steps are:
move disk 1 from rod A to rod C
move disk 2 from rod A to rod B
move disk 1 from rod C to rod B
move disk 3 from rod A to rod C
move disk 1 from rod B to rod A
move disk 2 from rod B to rod C
move disk 1 from rod A to rod C
HINT : You can modify this sample code and implement the function 'hanoi'
#include <stdio.h>
void hanoi(int n, char A, char B, char C);
int main(){
int n;
scanf("%d", &n);
hanoi(n, 'A', 'B', 'C');
return 0;
}
Input
An integer n (0<n<20), which means the number of disk.
Output
Print out the information of each step, and there is a '\n' at the end of each line.
Sample Input Download
Sample Output Download
Tags
Discuss
Description
You are given an array (a1, a2, ..., an) with each element has integral value. We define the GCD (greatest common divisor) of the array to be the largest positive integer that divides all the elements in the array.
Now, you need to delete a element from the array, determine what is the maximum GCD of this new array can be.
For example, if the given array is [15, 18, 30, 90], then:
- If we delete 15, then the new array is [18, 30, 90], and the GCD is 6
- If we delete 18, then the new array is [15, 30, 90], and the GCD is 15
- If we delete 30, then the new array is [15, 18, 90], and the GCD is 3
- If we delete 90, then the new array is [15, 18, 30], and the GCD is 3
So the answer is 15.
Input
The first line contain an integer n, representing the length of array.
The second line contain n integers a1, a2, ..., an, each ai and ai+1 are seperated by a blank.
Constrain
- For testcase 1-4, 1 <= n <= 1000. For testcase 5, n = 200000.
- For each 1 <= i <= n, 1 <= ai <= 109.
Output
Output the maximum GCD of the array after deleting an element in one line. You need to add '\n' at the end.
Hint
If you use a brute force method to solve the problem, you may get a time limit exceed on testcase5. It may be more suitable to think how to solve testcase5 efficiently after solving other problems.