Huas Summer training#1 B

Source: Internet
Author: User

Topic:

A ducci sequence is a sequence of n-tuples of integers. Given an n-tuple of integers (a1, a2, ..., aN), the next n-tuple In the sequence are formed by taking the absolute differences of neighboring integers:

( a 1, 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, 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) (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.

The main topic: give you n number, with ( a1, a2,..., aN) (| a1- a2|,| a2- a3|, ..., | aN- a1|) To find it in 1000 calculations is the output loop no output zero; a problem-solving idea: To store these numbers in a vector array for comparison; code:
1#include <iostream>2#include <math.h>3#include <vector>4 using namespacestd;5 Const  intL= ++Ten;6 7 intMain ()8 {9vector<int>A[l];Tenvector<int>b; One     intt,n,i,t,j,d,p; ACin>>T; -      while(t--) -     { theCin>>N; -         if(n>=3&&n<= the) -         { -D=0; + b.resize (n); -              for(i=0; i<l;i++) + a[i].resize (n); A              for(i=0; i<n;i++) at             { -scanf"%d", &a[0][i]); -b[i]=0; -                 if(a[0][i]<0|| a[0][i]> +) -scanf"%d", &a[0][i]); -             } in              for(i=0;i< +; i++)  -             { tot=a[i][0]; +                  for(j=0; j<n;j++)  -                 {         the                     if(j== (n1)) *a[i+1][j]=abs (a[i][n-1]-t);  $                     Else Panax Notoginsenga[i+1][j]=abs (a[i][j]-a[i][j+1]); -                 } the                 if(a[i+1]==b) +                 { Aprintf"zero\n"); theD=i; +                      Break; -                 } $             } $             if(d==0) -printf"loop\n");  -         }                 the     } -     return 0;


Huas Summer training#1 B

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.