UVA 1594 Ducci Sequence

UVA 1594	Ducci Sequence

#include <bits/stdc++.h>
#define endl "\n"
#define LL long long
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define fk freopen("input.txt", "r", stdin)
#define ALL(x) x.begin(), x.end()
#define INS(x) inserter(x, x.begin())
using namespace std;
typedef pair <int,int> PII;
/*    Promise me you won't do anything stupid.       */
map <vector <int>, int> Ducci;
vector <int> ele;
int T, a, n;
bool ju_z(){
    for(int i = 0; i < n; i++){
        if(ele[i] != 0)
            return false;
    }
    return true;
}
void solve(){
    int q = 1;
    while(q <= 1000){
        q++;
        int tem = ele[0];
        for(int i = 0; i < n; i++){
          ele[i] =  i < (n - 1) ? abs(ele[i] - ele[i + 1]) : abs(tem - ele[i]);
        }
        if(ju_z()){
            cout << "ZERO" <<endl;
            return;
        }

        if(Ducci.count(ele)){
            cout << "LOOP" << endl;
            return;
        }
        Ducci[ele] = q;
    }
}
int main(){
    IOS;
    //fk;
    cin >>T;
    while(T--){
        ele.clear();
        Ducci.clear();
        cin >> n;
        for(int i = 0; i < n; i++){
            cin >> a;
            ele.push_back(a);
        }
        Ducci[ele] = 1;
        solve();
    }
    return 0;
}