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 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 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 is reaching to a zeros tuple or a periodic loop.

Input

Your program is to read the input from standard input. The input consists of T test cases. The number of test cases T is given in the first line of the input. Each test case starts with a line containing an integer n (3 ≤ n ≤ 15), which represents the size of a tuple in the Ducci sequences. In the following line, n integers are given which represents the n-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.

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

Regionals 2009 >> Asia - Seoul

问题链接:UVA1594 UVALive4723 Ducci Sequence

问题简述:(略)

问题分析

序列放在STL向量中,出现过的序列放入STL集合中用来判断是否出现循环。

程序说明:(略)

题记:(略)

参考链接:(略)

AC的C++语言程序如下:

/* UVA1594 UVALive4723 Ducci Sequence */#include <bits/stdc++.h>using namespace std;vector<int> v, zero;
set< vector<int>> s;int main()
{int t, n;scanf("%d", &t);while(t--) {v.clear();zero.clear();s.clear();int a;scanf("%d", &n);for(int i=0; i<n; i++) {scanf("%d", &a);v.push_back(a);zero.push_back(0);}s.insert(v);for(;;) {if(v == zero) {puts("ZERO");break;}int a0 = abs(v[0] - v[1]);for(int i=1; i<n; i++)v[i] = abs(v[i] - v[(i + 1) % n]);v[0] = a0;if(s.count(v)) {puts("LOOP");break;} elses.insert(v);}}return 0;
}

UVA1594 UVALive4723 Ducci Sequence【vector+set】相关推荐

  1. 【❌❌vectorの奇技淫巧⭕⭕】C++ vector 如何正确处理动态申请内存的元素

    C++ vector 插入动态内存(new.malloc申请的内存)及销毁动态内存 (delete .free)demo 我们使用vector时候,有时候会插入一些动态内存数据(例如new出来的指针插 ...

  2. UVA10534 Wavio Sequence【LIS+DP】

    Wavio is a sequence of integers. It has some interesting properties. • Wavio is of odd length i.e. L ...

  3. UVA1584 UVALive3225 Circular Sequence【水题】

      Some DNA sequences exist in circular forms as in the following figure, which shows a circular sequ ...

  4. 【HDU5306】【DTOJ2481】Gorgeous Sequence【线段树】

    题目大意:给你一个序列a,你有三个操作,0: x y t将a[x,y]和t取min:1:x y求a[x,y]的最大值:2:x y求a[x,y]的sum 题解:首先很明显就是线段树裸题,那么考虑如何维护 ...

  5. AT2005-[AGC003E]Sequential operations on Sequence【差分,思维】

    正题 题目链接:https://www.luogu.com.cn/problem/AT2005 题目大意 开始有一个1∼n1\sim n1∼n依次排列的序列,然后QQQ次,第iii次把序列长度变为ai ...

  6. P4756-Added Sequence【斜率优化】

    正题 题目链接:https://www.luogu.com.cn/problem/P4756 题目大意 给出序列aaa,设f(l,r)=∣∑i=lrai∣f(l,r)=|\sum_{i=l}^ra_i ...

  7. CF438D-The Child and Sequence【线段树】

    正题 题目链接:https://www.luogu.com.cn/problem/CF438D 题目大意 一个序列要求支持 区间求和 区间取模 单点修改 解题思路 对于一个数取模会有结果x%p={x≤ ...

  8. 【codeforces 752C】Santa Claus and Robot

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  9. [刷题]算法竞赛入门经典(第2版) 5-2/UVa1594 - Ducci Sequence

    书上具体所有题目:http://pan.baidu.com/s/1hssH0KO 代码:(Accepted,20 ms) //UVa1594 - Ducci Sequence #include< ...

最新文章

  1. LabVIEW色彩定位实现药品包装质量检测(实战篇—4)
  2. linux 学习总结
  3. JS中有两种自加法操作
  4. 2021年李永乐6套卷一道无穷小定义的题目
  5. python调用系统api_Python调用系统底层API播放wav文件的方法
  6. 【Python】政府工作报告词云
  7. 基于JAVA+Servlet+JSP+MYSQL的银行账户管理系统
  8. python词组语义相似度_文本匹配,语义相似度,匹配相似短语/单词python语义wordNet模糊匹配...
  9. python小程序100题-python 练习题:流量套餐订购小程序
  10. 【BZOJ4566】[Haoi2016]找相同字符 后缀数组+单调栈
  11. 机器视觉最火应用领域
  12. 关于.dll文件的注册,如何注册.dll文件
  13. 武汉理工计算机研究生就业去向统计,武汉理工大学《2019届毕业生就业质量报告》发布,本科生月薪7333...
  14. 【翻译自mos文章】执行utlpwdmg.sql之后报ORA-28003, ORA-20001, ORA-20002, ORA-20003, ORA-20004 错误...
  15. android 开启wifi代码,Android编程打开WiFi
  16. #define 喵 int_【吃鸡大作战第三季】第12集 告白小雪喵
  17. 为什么说java是一个纯粹的面向对象的语言?面向对象语言和面向对象编程
  18. html5+css3实现2D动画效果演示
  19. 利用ENVI实现图像几何校正
  20. D4.1 About an initiating master

热门文章

  1. Discuz论坛分表以及memcache缓存优化
  2. PIO导出Excel 设置样式
  3. 在IDEA集成Github
  4. 计算机软件系统课程导入,中学信息技术 计算机系统的组成课件 硬件软件导入恰当...
  5. mysql调试问题_mysql 数据库调试分析
  6. CUDA算法——Stream and Event
  7. Win10+VS2015+CUDA9.0 环境搭建
  8. u-boot之u-boot.bin的生成
  9. C#调用open cv函数
  10. 关于如何打开一张jpg图片文件并存放到vector unsigned char中的讨论