Ducci Sequence UVA - 1594
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
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的更多相关文章
- UVA 1594 Ducci Sequence(两极问题)
Ducci Sequence Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu D ...
- uva 1594 Ducci Sequence <queue,map>
Ducci Sequence Description A Ducci sequence is a sequence of n-tuples of integers. Given an n-tupl ...
- 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, · · · ...
- UVa----------1594(Ducci Sequence)
题目: 1594 - Ducci Sequence Asia - Seoul - 2009/2010A Ducci sequence is a sequence of n-tuples of inte ...
- Ducci Sequence
Description A Ducci sequence is a sequence of n-tuples of integers. Given an n-tuple of integers ( ...
- [刷题]算法竞赛入门经典(第2版) 5-2/UVa1594 - Ducci Sequence
书上具体所有题目:http://pan.baidu.com/s/1hssH0KO 代码:(Accepted,20 ms) //UVa1594 - Ducci Sequence #include< ...
- Ducci Sequence解题报告
A Ducci sequence is a sequence of n-tuples of integers. Given an n-tuple of integers (a1, a2, ... , ...
- Uva - 1594 - Ducci Sequence
水题,算出每次的结果,比较是否全0,循环1000次还不是全0则LOOP AC代码: #include <iostream> #include <cstdio> #include ...
- UVA 1594:Ducci Sequence (模拟 Grade E)
题意: 对于一个n元组(a0,a1,...),一次变换后变成(|a0-a1|,|a1-a2|,...) 问1000次变换以内是否存在循环. 思路: 模拟,map判重 代码: #include < ...
随机推荐
- 双重检验锁模式为什么要使用volatile?
并发编程情况下有三个要点:操作的原子性.可见性.有序性. volatile保证了可见性和有序性,但是并不能保证原子性. 首先看一下DCL(双重检验锁)的实现: public class Singlet ...
- if __name__ == '__main__':简单粗暴解释
这个脚本被执行的时候,__name__ 值就是 __main__ ,才会执行 main()函数如果这个脚本是被 import 的话,__name__的值不一样.main()函数就不会被调用.这个句子用 ...
- 栈的数组模拟(非STL)
#include<bits/stdc++.h> using namespace std; struct zhan{ int s[10000]; int top=0; void zhanpo ...
- 机器学习系统或者SysML&DL笔记(一)
前言 在使用过TVM.TensorRT等优秀的机器学习编译优化系统以及Pytorch.Keras等深度学习框架后,总觉得有必要从理论上对这些系统进行一些分析,虽然说在实践中学习是最快最直接的(指哪儿打 ...
- jquery ajax error 函数的参数及使用
使用jquery的ajax方法向服务器发送请求的时候,可选的回调函数有success.complete.beforeSend.error函数等.error函数常用来进行错误信息的处理,这里着重提一下e ...
- SpringMVC-02 第一个SpringMVC程序
SpringMVC-02 第一个SpringMVC程序 第一个SpringMVC程序 配置版 新建一个Moudle , springmvc-02-hello,确定依赖导入进去了 1.配置web.xml ...
- 虚拟机安装centos系统【史上最详细的】
因为文章中有很多的图片,在博客园中还需要单张上传,所以使用了将markdown的文件链接存入网盘中让大家下载 windows系统请使用Typora软件打开 如果不知道Typora是什么软件,请在百度搜 ...
- python 集合详解
字符串 一个个字符组成的有序的序列,时字符的集合 使用单引,双引,三引 引住的字符序列 字符时不可变的对象 bytes定义 bytes不可变字节序列 使用b前缀定义 只允许基本ASCII使用字符形式 ...
- crf++分词
1.linux下安装crf工具包 先下载CRF++-0.58.tar.xz,在Linux环境下安装CRF工具包 https://github.com/taku910/crfpp 解压到某一个目录下面 ...
- [倍增]luogu P4155 [SCOI2015]国旗计划
题面 https://www.luogu.com.cn/problem/P4155 问在环上最少取多少个区间能完全覆盖环 分析 首先发现是环,先把端点变为2n方便处理,注意离散化 其次要删去贡献不如其 ...