Ducci Sequence, duccisequence

Source: Internet
Author: User

Ducci Sequence, duccisequence
Ducci SequenceTime Limit:3000 MSMemory Limit:0 KB64bit IO Format:% Lld & % llu

Description

A Ducci sequence is a sequenceN-Tuples of integers. GivenN-Tuple of integers (A1,A2 ,...,AN), the nextN-Tuple in the sequence is 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 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, 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)...

GivenN-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 consistsTTest cases. The number of test casesTIs given in the first line of the input. Each test case starts with a line containing an integerN(3N15), which represents the size of a tuple in the Ducci sequences. In the following line,NIntegers are given which representsN-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 ZERO LOOP

 

Question: given an array, calculate the absolute values of the values minus the previous one in sequence. If it is the last one, it is the absolute value of the last value minus the first one,

The maximum number of loops is 1000. If all the values in the array are 0, the value is ZERO. Otherwise, the value is LOOP. Therefore, only 0 is required.

 

# Include <iostream>
Using namespace std;
Int main ()
{
Int a [20];
Int I, j, t, n, sum;
Cin> t;
While (t --)
{
Cin> n;
For (I = 0; I <n; I ++)
Cin> a [I];
For (I = 0; I <1000; I ++)
{
Sum = 0;
Int s = a [0]; // the value of a [0] changes once every cycle, so you need to define a variable s
For (j = 0; j <n-1; j ++)
{
If (a [j]> a [j + 1])
A [j] = a [j]-a [j + 1];
Else
A [j] = a [j + 1]-a [j];
Sum + = a [j];
}
If (a [n-1]> s)
A [n-1] = a [n-1]-s;
Else

A [n-1] = s-a [n-1];
Sum + = a [n-1];
If (sum = 0)
Break;

}
If (sum = 0)
Cout <"ZERO" <endl;
Else
Cout <"LOOP" <endl;

}
Return 0;
}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.