The problem of P42 that calculator in Great White Book is used this method, Floyd the Circle law.
Of course, using the map in STL is also possible.
1#include <cstdio>2#include <cmath>3 4 Const intMAXN = -;5 intN;6 intA[MAXN], B[MAXN], C[MAXN];7 8 voidNextinta[])9 {Ten for(inti =0; I < n-1; i++) A[i] = Std::abs (A[i]-a[i +1]); OneA[n-1] = a[0]; A } - - BOOLEqualintA[],intb[]) the { - for(inti =0; I < n; i++)if(A[i]! = B[i])return false; - return true; - } + - intMain () + { A //freopen ("In.txt", "R", stdin); at - intT scanf"%d", &T); - while(t--) - { -scanf"%d", &n); - for(inti =0; I < n; i++) {scanf ("%d", &a[i]); B[i] =a[i];} inA[n] = b[n] = a[0]; -n++; to + BOOLloop =true; - for(inti =0; I <1010; i++) the { * Next (a); next ( b); next (b) ; $ if(Equal (A, C)) {loop =false; Break; }Panax Notoginseng if(Equal (b, C)) {loop =false; Break; } - if(Equal (A, B)) {loop =true; Break; } the } +printf"%s\n", loop?"LOOP":"ZERO"); A } the + return 0; -}code June
UVa 1594 (Floyd ring) Ducci Sequence