Ducci Sequence
InterviewTime limit1sMemory limit128 MB
Repeatedly replace each entry with the absolute difference of adjacent entries and report whether the tuple reaches all zeros or loops.
- Level
Easy3 of 10
- Topics
- Simulation, Hash map
- Solved
- No attempts yet
Problem
A Ducci sequence is a sequence of integer n-tuples. Given an integer n-tuple , the next tuple in the sequence is formed from the absolute differences of adjacent entries, where the last entry is treated as adjacent to the first entry .
Every Ducci sequence eventually reaches the all-zero tuple or falls into a periodic loop. For example, the sequence starting from reaches the all-zero tuple after 5 steps.
By contrast, the sequence starting from never reaches the all-zero tuple and instead enters a periodic loop. Notice below that the tuple recurs.
Given an n-tuple, write a program that determines whether the Ducci sequence starting from it reaches the all-zero tuple or falls into a periodic loop.
Input
The first line contains the number of test cases . Each test case consists of two lines: the first line contains the tuple size (), and the second line contains integers separated by spaces. Each integer is between 0 and 1000 inclusive. The number of steps needed for a Ducci sequence to reach the all-zero tuple or enter a loop does not exceed 1000.
Output
For each test case, print the answer on its own line. Print LOOP if the Ducci sequence falls into a periodic loop, or ZERO if it reaches the all-zero tuple.