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. Postman newman

    安装 官方文档: Newman - Running collections in the command line Installing Newman on Windows 1. 安装NodeJs 下 ...

  2. easyUI属性总结

    1.div easyui-window        生成一个window窗口样式.      属性如下:                   1)modal:是否生成模态窗口.true[是] fal ...

  3. Swift_UILabel

    一.初始化 // 初始化UIlabel,并设置frame //let labelOne = UILabel.init(frame: CGRect.init(x: 10, y: 20, width: 3 ...

  4. 你应该知道的8个Java牛人

    简单介绍一下8个Java牛人,他们为Java社区,创建了框架(framework),产品或者是写书,影响甚至改变了Java开发的方法 8.Tomcat创始人 James Duncan Davidson ...

  5. python获取本机IP、mac地址、计算机名

    在python中获取ip地址和在php中有很大不同,在php中往往比较简单.那再python中怎么做呢? 我们先来看一下python 获得本机MAC地址: 1 2 3 4 import uuid de ...

  6. cocos2d-x项目过程记录(Objective-C转C++)

    (原创作品,欢迎转载,注明出处,谢谢:http://www.cnblogs.com/binxindoudou/admin/EditPosts.aspx?postid=3179335) 1.单例模式中, ...

  7. MVC实现登录,增删改查之数据展示:JSP的EL表达式(二)

    这里的数据展示利用jsp的EL表达式,后台放入session,前台EL获取 数据库设计是这样的,一个老师对应有多个学生,在学生表student中建立外键tid与老师表teacher的tid对应,现在老 ...

  8. 关于 yii 验证码显示, 但点击不能刷新的处理

    先说说 render 与 renderPartial, 各位看官, 先别走, 我没跑题, 这个问题如果需要解决, 关键就在 render 与 renderPartial 的区别. renderPart ...

  9. SPOJ 4053 - Card Sorting 最长不下降子序列

    我们的男主现在手中有n*c张牌,其中有c(<=4)种颜色,每种颜色有n(<=100)张,现在他要排序,首先把相同的颜色的牌放在一起,颜色相同的按照序号从小到大排序.现在他想要让牌的移动次数 ...

  10. 9.16noip模拟试题

    题目描述 在幻想乡,东风谷早苗是以高达控闻名的高中生宅巫女.某一天,早苗终于入手了最新款的钢达姆模型.作为最新的钢达姆,当然有了与以往不同的功能了,那就是它能够自动行走,厉害吧(好吧,我自重).早苗的 ...