Ducci Sequence解题报告
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:
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:
The 5-tuple sequence starting with 4,2,0,2,0 enters a loop after 2 steps:
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(3n
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.
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 题意:给你一个数组,相邻数相加(最后一个数应该加第一个数),然后又可以得到一个新的数组,反复进行这样的步骤,结果会有两种,数组每个成员都是零,或者在这样的过程中出现了周期,即出现了和以前已经出现过的相同的项。
最后要求判断是哪一种情况 思路:
如果按照一般的解题思想,每得到一个新的数组都去判断是zero,还是loop。zero还好说,如果判断loop那就要每次遍历一遍数组,程序肯定会超时。、
所以~~~
反正结果只有两种可能,不是loop,就是zero,只判断zero就好了 代码:
#include <iostream>
#include <cstdio>
using namespace std;
const int maxn=;
int a[][maxn];
int n;
int iszero; void Init()
{
cin>>n;
for(int i=;i<n;i++)
cin>>a[][i];
} bool judge()
{
iszero=;
int now,form;
for(int i=;i<=;i++)
{
if(i%==) now=,form=;
else now=,form=;
for(int j=;j<n;j++)
{
if(j==n-)
{
a[now][j]=a[form][j]+a[form][];
}
else
a[now][j]=a[form][j]+a[form][j+];
}
int flag=;
for(int k=;k<n;k++)
if(a[now][k]!=) {flag=;break;}
if(!flag) {iszero=;break;}
}
if(iszero) return true;
else return false;
} int main()
{
int T;
cin>>T;
while(T--)
{
Init();
if(judge()) cout<<"ZERO"<<endl;
else cout<<"LOOP"<<endl;
}
return ;
}
Ducci Sequence解题报告的更多相关文章
- USACO Section2.1 Sorting a Three-Valued Sequence 解题报告
sort3解题报告 —— icedream61 博客园(转载请注明出处)---------------------------------------------------------------- ...
- timus 1175. Strange Sequence 解题报告
1.题目描述: 1175. Strange Sequence Time limit: 1.0 secondMemory limit: 2 MB You have been asked to disco ...
- 【LeetCode】842. Split Array into Fibonacci Sequence 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】60. Permutation Sequence 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- LeetCode: Permutation Sequence 解题报告
Permutation Sequence https://oj.leetcode.com/problems/permutation-sequence/ The set [1,2,3,…,n] cont ...
- USACO Section 2.1 Sorting a Three-Valued Sequence 解题报告
题目 题目描述 给N个整数,每个整数只能是1,2,或3.现在需要对这个整数序列进行从小到大排序,问最少需要进行几次交换.N(1 <= N <= 1000) 样例输入 9 2 2 1 3 3 ...
- LeetCode: Longest Consecutive Sequence 解题报告
Longest Consecutive Sequence Given an unsorted array of integers, find the length of the longest con ...
- BZOJ 1367 [Baltic2004]sequence 解题报告
BZOJ 1367 [Baltic2004]sequence Description 给定一个序列\(t_1,t_2,\dots,t_N\),求一个递增序列\(z_1<z_2<\dots& ...
- Winter-1-F Number Sequence 解题报告及测试数据
Time Limit:1000MS Memory Limit:32768KB Description A number sequence is defined as follows:f(1) ...
随机推荐
- Seek the Name, Seek the Fame POJ - 2752
Seek the Name, Seek the Fame POJ - 2752 http://972169909-qq-com.iteye.com/blog/1071548 (kmp的next的简单应 ...
- URAL 7077 Little Zu Chongzhi's Triangles(14广州I)
题目传送门 题意:有n根木棍,三根可能能够构成三角形,选出最多的三角形,问最大面积 分析:看到这个数据范围应该想到状压DP,这次我想到了.0010101的状态中,1表示第i根木棍选择,0表示没选,每一 ...
- Linux下cpu过高问题排查
原文地址:https://blog.csdn.net/chenjunan888/article/details/80447800 在服务器报cpu过高时,可使用以下命令,快速导出堆栈信息,以方便查看具 ...
- DHCP服务简单搭建步骤
服务端:sishen_63 IP:192.168.1.63 客户端:sishen_64 IP:192.168.1.64 此外,因为本实验实在虚拟机中做的,所以对虚拟机还要做如下设置: 服务 ...
- 前端打印console
很多时候,我们都想知道,是否已经选中或得到数据时,我们可以利用console 打印出来.console有几种方式使用.具体有: console.log($scope.getParkId); conso ...
- 【学习笔记】C++ cout 输出小数点后指定位数
在C中我们可以使用 printf("%.2lf",a);但在C++中是没有格式操作符的,该如何操作: C++使用setprecision()函数,同时必须包含头文件iomanip, ...
- mac homebrew安装
http://book.51cto.com/art/201107/278761.htm 3.2.3 使用 Homebrew 安装 Git Mac OS X 有好几个包管理器,用于管理一些开源软件在 M ...
- (三)docker 的启动,重启,关闭命令
docker启动命令,docker重启命令,docker关闭命令 启动 systemctl start docker 守护进程重启 sudo systemctl daemon-reload 重 ...
- python 字符与字节 json序列和反序列及支持的类型
b = b"demo" s = "demo" # 字符串转字节 s = bytes(s, encoding = "utf8") s = st ...
- vue 高度 动态更新计算 calcHeight watch $route
vue 高度 动态更新计算 calcHeight () { // this.tableHeight = window.innerHeight - 210 } }, mounted () { // co ...