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|,···,|ana1|)

  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

HINT

 采用思路时使用两个数才存储,其中一个用于计算,另一个用于判断结束,也就是排序找出最大值,如果过最大值为0那就不是循环。另外还要计算判断次数。耗时370ms。

Accepted

#include<iostream>
#include<queue>
#include<vector>
#include<algorithm>
using namespace std; int main()
{
int m, n,t,i;
cin >> m;
while (m--) {
cin >> i;
n = i;
vector<int>arr,arr2;
while (i--)
{
cin >> t;
arr.push_back(t);
arr2.push_back(t);
}
for (i = 1;;i++)
{
t = arr[0]; for (int j = 0;j < n - 1;j++)
arr[j]= arr2[j] = abs(arr[j] - arr[j + 1]);
arr[n - 1] = arr2[n - 1] = abs(arr[n - 1] - t);
if (i > 1000) {
cout << "LOOP" << endl;
break;
} sort(arr2.begin(), arr2.end());
if (!arr2[arr2.size() - 1])break;
}
if (i <= 1000)cout << "ZERO" << endl;
}
}

Ducci Sequence UVA - 1594的更多相关文章

  1. UVA 1594 Ducci Sequence(两极问题)

           Ducci Sequence Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu   D ...

  2. uva 1594 Ducci Sequence <queue,map>

    Ducci Sequence Description   A Ducci sequence is a sequence of n-tuples of integers. Given an n-tupl ...

  3. UVA 1594 Ducci Sequence(紫书习题5-2 简单模拟题)

    A Ducci sequence is a sequence of n-tuples of integers. Given an n-tuple of integers (a1, a2, · · · ...

  4. UVa----------1594(Ducci Sequence)

    题目: 1594 - Ducci Sequence Asia - Seoul - 2009/2010A Ducci sequence is a sequence of n-tuples of inte ...

  5. Ducci Sequence

    Description   A Ducci sequence is a sequence of n-tuples of integers. Given an n-tuple of integers ( ...

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

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

  7. Ducci Sequence解题报告

    A Ducci sequence is a sequence of n-tuples of integers. Given an n-tuple of integers (a1, a2, ... ,  ...

  8. Uva - 1594 - Ducci Sequence

    水题,算出每次的结果,比较是否全0,循环1000次还不是全0则LOOP AC代码: #include <iostream> #include <cstdio> #include ...

  9. UVA 1594:Ducci Sequence (模拟 Grade E)

    题意: 对于一个n元组(a0,a1,...),一次变换后变成(|a0-a1|,|a1-a2|,...) 问1000次变换以内是否存在循环. 思路: 模拟,map判重 代码: #include < ...

随机推荐

  1. 必知必会之 Java

    必知必会之 Java 目录 不定期更新中-- 基础知识 数据计量单位 面向对象三大特性 基础数据类型 注释格式 访问修饰符 运算符 算数运算符 关系运算符 位运算符 逻辑运算符 赋值运算符 三目表达式 ...

  2. Ability之间或者进程间数据传递之对象(Sequenceable序列化)

    鸿蒙入门指南,小白速来!0基础学习路线分享,高效学习方法,重点答疑解惑--->[课程入口] 这两天51cto上的一个粉丝朋友问了我一个问题,Ability之间使用Sequenceable序列化传 ...

  3. 高级FTP

      一.作业需求 1. 用户加密认证(已完成) 2. 多用户同时登陆(已完成) 3. 每个用户有自己的家目录且只能访问自己的家目录(已完成) 4. 对用户进行磁盘配额.不同用户配额可不同(已完成) 5 ...

  4. 常用linux命令,开发必备-速收藏

    在前面我们介绍了通过VirtualBox安装Linux的方法,参考: 一网打尽,一文讲通虚拟机VirtualBox及Linux使用 本文我们将介绍在使用linux的过程中常用的一些Linux命令,掌握 ...

  5. SpringBoot 开发提速神器 Lombok+MybatisPlus+SwaggerUI

    导读 Lombok:可以让你的POJO代码特别简洁,不止简单在BO/VO/DTO/DO等大量使用,还有设计模式,对象对比等 MybatisPlus:增加版Mybatis,基础的数据库CRUD.分页等可 ...

  6. 死磕Spring之IoC篇 - @Bean 等注解的实现原理

    该系列文章是本人在学习 Spring 的过程中总结下来的,里面涉及到相关源码,可能对读者不太友好,请结合我的源码注释 Spring 源码分析 GitHub 地址 进行阅读 Spring 版本:5.1. ...

  7. 爬虫必知必会(7)_scrapy框架高级

    一.请求传参 实现深度爬取:爬取多个层级对应的页面数据 使用场景:爬取的数据没有在同一张页面中 在手动请求的时候传递item:yield scrapy.Request(url,callback,met ...

  8. JSP原理剖析

    什么是JSP JSP长得和html没有区别,但是服务器会把jsp转换为servlet类 JSP(Java Server Page)Java服务器端页面,和Servlet一样,用于动态Web 在jsp之 ...

  9. TensorFlow学习(1)

    初识TensorFlow 一.术语潜知 深度学习:深度学习(deep learning)是机器学习的分支,是一种试图使用包含复杂结构或由多重非线性变换构成的多个处理层对数据进行高层抽象的算法. 深度学 ...

  10. cocos 向左滚动公告

      properties:{ lblNotice:[cc.Node], speed:1, curtext:null }, start (){ this.getNotice(); }, getNotic ...