Ducci Sequence
Description
A ducci sequence is a sequence of n-tuples of integers. Given an n-tuple of integers (a1, a2, ... ), a C11>n), the next n-tuple in the sequence are formed by taking the absolute differences of neighboring Integers:
(
a1,
a2,... ,
aN) --- (|
a1-
a2|,|
a2-
a3|,... ,|
aN-
a1|)
Ducci sequences either reach a tuple of zeros or fall into a periodic loop. For example, the 4-tuple sequence starting and 8,11,2,7 takes 5 steps to reach the zeros tuple:
(8, one, 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)---(2, 0, 0, 0, 2)---
(2, 0, 0, 2, 0)---(2, 0, 2, 2, 2)---(2, 2, 0, 0, 0)---(0, 2, 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 an n-tuple of integers, write a program to decide if the sequence are reaching to a zeros tuple or a per Iodic Loop.
Input
Your program was to read the input from standard input. The input consists of T test Cases. The number of test cases T is given on the first line of the input. Each test case starts with a line containing an integer n(3n), which represents the size of a Tuple in the Ducci sequences. In the following line, n integers is given which represents the n-tuple of integers. The range of integers is from 0 to 1,000. Assume that the maximum number of steps of a ducci sequence reaching zeros tuples or making a loop does not exceed A.
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 Reache s 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 Zero loop
UVA 1594 Ducci Sequence