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

好水的题,,直接莽过去就行了过了,,,或许有不需要循环这么多次的方法。。。

 #include <iostream>
#include <algorithm>
#include <cstdio>
using namespace std;
int a[];
int n;
bool change() //模拟运算过程
{
int sum = ;
int t = a[];
for(int i = ; i <= n;i++)
{
if(i!=n) a[i] = abs(a[i]-a[i+]);
else a[i] = abs(a[i]-t);
sum+=a[i];
}
if(sum==) return true;
else return false;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(int i = ;i <= n;i++)
{
scanf("%d",&a[i]);
}
int j;
for( j = ;j <= ; j++)
{
if(change()) break;
}
if(j>) printf("LOOP\n");
else printf("ZERO\n");
}
return ;
}

UVA 1594 Ducci Sequence(紫书习题5-2 简单模拟题)的更多相关文章

  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

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

  4. 【暴力模拟】UVA 1594 - Ducci Sequence

    想麻烦了.这题真的那么水啊..直接暴力模拟,1000次(看了网上的200次就能A)后判断是否全为0,否则就是LOOP: #include <iostream> #include <s ...

  5. 紫书 习题 11-9 UVa 12549 (二分图最小点覆盖)

    用到了二分图的一些性质, 最大匹配数=最小点覆盖 貌似在白书上有讲 还不是很懂, 自己看着别人的博客用网络流写了一遍 反正以后学白书应该会系统学二分图的,紫书上没讲深. 目前就这样吧. #includ ...

  6. 紫书 习题 11-8 UVa 1663 (最大流求二分图最大基数匹配)

    很奇怪, 看到网上用的都是匈牙利算法求最大基数匹配 紫书上压根没讲这个算法, 而是用最大流求的. 难道是因为第一个人用匈牙利算法然后其他所有的博客都是看这个博客的吗? 很有可能-- 回归正题. 题目中 ...

  7. 紫书 习题8-12 UVa 1153(贪心)

    本来以为这道题是考不相交区间, 结果还专门复习了一遍前面写的, 然后发现这道题的区间是不是 固定的, 是在一个范围内"滑动的", 只要右端点不超过截止时间就ok. 然后我就先考虑有 ...

  8. 紫书 习题8-7 UVa 11925(构造法, 不需逆向)

    这道题的意思紫书上是错误的-- 难怪一开始我非常奇怪为什么第二个样例输出的是2, 按照紫书上的意思应该是22 然后就不管了,先写, 然后就WA了. 然后看了https://blog.csdn.net/ ...

  9. UVa 12169 Disgruntled Judge 紫书

    思路还是按照紫书,枚举a,得出b, 然后验证. 代码参考了LRJ的. #include <cstdio> #include <iostream> using namespace ...

随机推荐

  1. LR C语言语句复习,几个简单代码

    嵌套循环 Action() { int i,j; ;i<=;i++) { ) beark; else lr_output_message("i=%d",i); ;j<= ...

  2. ElasticSearch最新版本下载地址

    直接访问官方很慢,打不开,找到这个下载方法: ElasticSearch下载地址: https://download.elastic.co/elasticsearch/elasticsearch/el ...

  3. 妙用Object

    妙用Object 当你在写C#程序时,经常会用到“ToString()”这个方法,而且如果你细心你点就会发现所有的引用类型都含有“ToString()”这个方法,那么你知道为什么会这样吗?很简单,因为 ...

  4. chrome浏览器表单自动填充默认样式-autofill

    Chrome会在客户登陆过某网站之后, 会自动记住密码 当你下次再次进入该网站的时候, 可以自由的选择登陆的账号, Chrome会为你自动填充密码. 而你无需再输入密码 这本身是一个很好的功能, 但是 ...

  5. java8 peek

    这样不会有任何的输出:Stream.of("one", "two", "three", "four").peek(e - ...

  6. hibernate课程 初探单表映射3-1 hibernate单表操作简介

    本章简介: 1 单一主键 2 基本类型 3 对象类型 4 组件属性 5 单表操作CRUD实例

  7. weblogic 忘记控制台账号密码 进行重新设置

    第一步:首先要关闭weblogic服务. 第二步:对一些重要的文件进行备份: 1. 为了保证操作安全,备份%DOMAIN_HOME%/security/DefaultAuthenticatorInit ...

  8. Azure School,让你系统化快速学习人工智能

    要说目前最热门的技术,非人工智能莫属了,让计算机程序能够看懂.听懂.读懂.理解我们的世界!想想就激动!! 上至高大上的个人数字化助理,下至P图软件,各种应用都开始增加AI相关的功能,试问又有哪个技术爱 ...

  9. sweetalert 1.0多次回调函数bug

    一个删除功能,原来的实现方式(注释部分)有多次的回调,会出现第二个swal窗口不显示,回调函数体不执行的情况.后来的解决方式是使用bootstrap的modal模态框,删除成功后显示模态框,模态框关闭 ...

  10. 数组的reduce方法的应用

    var values = [1,2,3,4,5] var sum = values.reduce(function(pre,cur,index,array){ return pre + cur }) ...