Ducci Sequence, duccisequence

Description

A Ducci sequence is a sequence*N*-Tuples of integers. Given*N*-Tuple of integers (*A*1,*A*2 ,...,*A*N), the next*N*-Tuple in the sequence is formed by taking the absolute differences of neighboring integers:

(

*A*1,

*A*2 ,...,

*A*N) (|

*A*1-

*A*2 |, |

*A*2-

*A*3 |,..., |

*A*N-

*A*1 |)

Ducci sequences either reach a tuple of zeros or fall into a periodic loop. For example, the 4-tuple sequence starting with, takes 5 steps to reach the zeros tuple:

(8, 11, 2, 7) (3, 9, 5, 1) (6, 4, 4, 2) (2, 0, 2, 4) (2, 2, 2, 2) (0, 0, 0, 0 ).

The 5-tuple sequence starting with 4,2, 0,2, 0 enters a loop after 2 steps:

(4, 2, 0, 2, 0) (2, 2, 2, 2, 4 )(

**0, 0, 0, 2, 2**) (0, 0, 2, 0, 2) (0, 2, 2, 2) (2, 0, 0, 0, 2)

(2, 0, 0, 2, 0) (2, 0, 2, 2) (2, 2, 0, 0, 0) (0, 2, 0, 0, 0, 2) (2, 2, 0, 2, 2) (0, 2, 2, 0, 0)

(2, 0, 2, 0, 0) (2, 2, 2, 0, 2) (0, 0, 2, 2, 0) (0, 2, 0, 2, 0) (2, 2, 2, 2, 0 )(

**0, 0, 0, 2, 2**)...

Given*N*-Tuple of integers, write a program to decide if the sequence is reaching to a zeros tuple or a periodic loop.

Input

Your program is to read the input from standard input. The input consists*T*Test cases. The number of test cases*T*Is given in the first line of the input. Each test case starts with a line containing an integer*N*(3*N*15), which represents the size of a tuple in the Ducci sequences. In the following line,*N*Integers are given which represents*N*-Tuple of integers. The range of integers are from 0 to 1,000. You may assume that the maximum number of steps of a Ducci sequence reaching zeros tuple or making a loop does not exceed 1,000.

Output

Your program is to write to standard output. Print exactly one line for each test case. Print'LOOP'If the Ducci sequence falls into a periodic loop, print'ZERO'If the Ducci sequence reaches to a zeros tuple.

The following shows sample input and output for four test cases.

Sample Input

4 4 8 11 2 7 5 4 2 0 2 0 7 0 0 0 0 0 0 0 6 1 2 3 1 2 3

Sample Output

ZERO LOOP

This means that the number of n is input, and the absolute values of the two adjacent numbers are assigned to the previous number, and the last number is subtracted from the first number. If all the numbers of the series within one thousand times are 0, ZERO is output; otherwise, LOOP is output.

The Code is as follows:

# Include <iostream>

Using namespace std;

Int main ()

{

Int T;

Cin> T;

While (T --)

{

Int n, a [20];

Cin> n;

Int flag = 0; // set the flag to zero after each input.

For (int I = 0; I <n; I ++)

Cin> a [I];

For (int j = 0; j <1000; j ++)

{

Int f = a [0]; // because the first operation is a [0] And a [1] and then assigned to a [0], the value of a [0] will change, therefore, you must first assign the value of a [0] to f.

Int s = 0;

For (int I = 0; I <n-1; I ++)

{

If (a [I]> = a [I + 1])

A [I] = a [I]-a [I + 1];

Else

A [I] = a [I + 1]-a [I];

S + = a [I]; // evaluate the sum of array 0 to N-2.

}

If (f> = a [n-1])

A [n-1] = f-a [n-1];

Else

A [n-1] = a [n-1]-f;

S + = a [n-1]; // Add the [n-1] value.

If (s = 0)

{

Flag = 1;

Break;

}

}

If (flag)

Cout <"ZERO" <endl;

Else

Cout <"LOOP" <endl;

}

Return 0;

}

The first time I wrote a blog, I hope you can understand.