Ducci Sequence

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

 

Description

A Ducci sequence is a sequence of n-tuples of integers. Given an n-tuple of integers (a1a2, ... an), the next n-tuple in the sequence is formed by taking the absolute differences of neighboring integers:

a1a2... 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(3n15), 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.

The following shows sample input and output for four test cases.

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

题解:给定数组,依次求前一个减后一个的值的绝对值,如果是最后一个则是最后一个减第一个的值的绝对值,

最多循环1000次,如果出现数组的值全部变为0,则为ZERO,否则为LOOP,所以只求全部为0的情况即可。

#include<iostream>
using namespace std;
int main()
{int a[];
int i,j,t,n,sum;
cin>>t;
while(t--)
{
cin>>n;
for(i=; i<n; i++)
cin>>a[i];
for(i=; i<; i++)
{ sum=;
int s=a[];
for(j=; j<n-; j++)
{
if(a[j]>a[j+])
a[j]=a[j]-a[j+];
else
a[j]=a[j+]-a[j];
sum+=a[j]; } if(a[n-]>s)
a[n-]=a[n-]-s;
else a[n-]=s-a[n-];
sum+=a[n-];
if(sum==)
break; } if(sum==)
cout<<"ZERO"<<endl;
else
cout<<"LOOP"<<endl; }
return ;
}

UVA 1594 Ducci Sequence(两极问题)的更多相关文章

  1. uva 1594 Ducci Sequence <queue,map>

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

  2. 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, · · · ...

  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. 【UVA】1594 Ducci Sequence(纯模拟)

    题目 题目     分析 真的快疯了,中午交了一题WA了好久,最后发现最后一个数据不能加\n,于是这次学乖了,最后一组不输出\n,于是WA了好几发,最后从Udebug发现最后一组是要输出的!!!   ...

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

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

  7. Ducci Sequence UVA - 1594

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

  8. UVa 1584 Circular Sequence --- 水题

    UVa 1584 题目大意:给定一个含有n个字母的环状字符串,可从任意位置开始按顺时针读取n个字母,输出其中字典序最小的结果 解题思路:先利用模运算实现一个判定给定一个环状的串以及两个首字母位置,比较 ...

  9. Ducci Sequence

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

随机推荐

  1. MySQL客户端执行外部sql文件命令

    客户端 source d:\bbs.sql 或者 \.  d:\bbs.sql

  2. [基础] C++与JAVA的内存管理

    在内存管理上(总之一句话——以后C++工程,一定要用智能指针!) 1.同是new一个对象,C++一定得手动delete掉,而且得时刻记住能delete的最早时间(避免使用空指针).JAVA可以存活于作 ...

  3. [Locked] Best Meeting Point

    Best Meeting Point A group of two or more people wants to meet and minimize the total travel distanc ...

  4. 路由器刷机常见第三方固件及管理前端种类(OpenWrt、Tomato、DD-Wrt)

    目前路由器折腾刷机,除了采用各品牌的原厂固件外,第三方路由器固件,基本就是:Tomato.DD-WRT.OpenWRT三种. 基本上所有第三方路由器固件的架构上可分为前端(Frontend)和后端(B ...

  5. poj1019

    有一个序列 1 12 123 1234 ..... ........ ........... 12345678910 ................................ 求第n个数字是什 ...

  6. 使用drawRect有什么影响

    用来画图,这个方法会在intiWithRect时候调用.这个方法的影响在于有touch event的时候之后,会重新绘制,很多这样的按钮的话就会比较影响效率.以下都会被调用1.如果在UIView初始化 ...

  7. 高效的DDoS攻击探测与分析工具——FastNetMon

    一.简介 FastNetMon这是一个基于多种抓包引擎(NetFlow, IPFIX, sFLOW, netmap, PF_RING, PCAP)的DoS/DDoS攻击高效分析工具,可以探测和分析网络 ...

  8. winform combobox控件绑定 分类: WinForm 2014-04-17 14:34 118人阅读 评论(0) 收藏

    想要达到的效果:把数据库中的一列数据绑定到combobox控件中. 数据库表:T_Task//任务表 列名:Task_Name//名称 主键:Task_ID combobox控件名称:cbName 解 ...

  9. 第四节:教你如何快速让浏览器兼容ES6特性

    写在正文前,本来这一节的内容应该放在第二节更合适,因为当时就有同学问ES6的兼容性如何,如何在浏览器兼容ES6的特性,这节前端君会介绍一个抱砖引玉的操作案例. 为什么ES6会有兼容性问题? 由于广大用 ...

  10. codevs 4909 寂寞的堆(写的好丑0.0)

    #include<iostream> #include<cstdio> #include<cstring> #include<cmath> #defin ...