Ducci序列(Ducci Sequence)

2023-05-16

Ducci Sequence

Time limit: 3.000 seconds

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:

( a 1, a 2, ... , a n) $\displaystyle \rightarrow$ (| a 1 - a 2|,| a 2 - a 3|, ... ,| a n - a 1|)

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) $\displaystyle \rightarrow$ (3, 9, 5, 1) $\displaystyle \rightarrow$ (6, 4, 4, 2) $\displaystyle \rightarrow$ (2, 0, 2, 4) $\displaystyle \rightarrow$ (2, 2, 2, 2) $\displaystyle \rightarrow$ (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) $\displaystyle \rightarrow$ (2, 2, 2, 2, 4) $\displaystyle \rightarrow$ ( 0, 0, 0, 2, 2) $\displaystyle \rightarrow$ (0, 0, 2, 0, 2) $\displaystyle \rightarrow$ (0, 2, 2, 2, 2) $\displaystyle \rightarrow$ (2, 0, 0, 0, 2) $\displaystyle \rightarrow$
(2, 0, 0, 2, 0) $\displaystyle \rightarrow$ (2, 0, 2, 2, 2) $\displaystyle \rightarrow$ (2, 2, 0, 0, 0) $\displaystyle \rightarrow$ (0, 2, 0, 0, 2) $\displaystyle \rightarrow$ (2, 2, 0, 2, 2) $\displaystyle \rightarrow$ (0, 2, 2, 0, 0) $\displaystyle \rightarrow$
(2, 0, 2, 0, 0) $\displaystyle \rightarrow$ (2, 2, 2, 0, 2) $\displaystyle \rightarrow$ (0, 0, 2, 2, 0) $\displaystyle \rightarrow$ (0, 2, 0, 2, 0) $\displaystyle \rightarrow$ (2, 2, 2, 2, 0) $\displaystyle \rightarrow$ ( 0, 0, 0, 2, 2) $\displaystyle \rightarrow$ ...

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$ \le$n$ \le$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
  
【分析】

       由于输入保证最多1000步就会变成0或者循环,那么我们只需要在1005步内(当然只要大于1000步就可以,但也不能太大,因为考虑到效率问题)判断是否变成0即可,如果在1005步内没有变成0的话说明已经进入了循环。如何判断是否全变为0,只要将所有的数相加判断他们的总和是否等于0即可。

用java语言编写程序,代码如下:

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		int[] ds = new int[20];
		int t = input.nextInt();
		for(int i = 0; i < t; i++) {
			int n = input.nextInt();
			
			int sum = 0;
			for(int j = 0; j < n; j++) {
				ds[j] = input.nextInt();
				sum += ds[j];
			}
			if(sum == 0) {
				System.out.println("ZERO");
				continue;
			}
			
			int temp = 0;
			boolean isLoop = true;
			for(int k = 0; k < 1005; k++) {
				temp = ds[0];
				sum = 0;
				for(int x = 0; x < n; x++) {
					if(x == n - 1)
						ds[x] = (int)(Math.abs(ds[x] - temp));
					else
						ds[x] = (int)(Math.abs(ds[x] - ds[x + 1]));
					
					sum += ds[x];
				}
				
				if(sum == 0) {
					isLoop = false;
					break;
				}
			}
			
			if(isLoop)
				System.out.println("LOOP");
			else
				System.out.println("ZERO");
		}
	}
}



本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Ducci序列(Ducci Sequence) 的相关文章

  • 如何扩展integer_sequence?

    我有一个如下所示的函数 template
  • 在连续的值运行中创建计数器

    我希望在每次运行的相等值中创建一个连续的数字 就像出现的计数器一样 一旦当前行中的值与前一行不同 它就会重新启动 请在下面找到输入和预期输出的示例 dataset lt data frame input c a b b a a c a a
  • 如果下限大于上限,则创建空序列的序列构造

    不止一次体现R的 聪明 seq函数在极端情况下对我造成了严重的打击lower upper 1 gt 1 0 1 1 0 gt seq 1 0 1 1 0 gt seq 1 0 1 Error in seq default 1 0 1 wro
  • 将每月的日期序列拆分为一个块(包含开始日期和结束日期)

    假设我有一个如下所示的数据框 df lt data frame group c a a b start as Date c 2018 01 01 2018 09 01 2018 02 01 end as Date c 2018 02 15
  • 将Python序列转换为NumPy数组,填充缺失值

    Python 序列的隐式转换可变长度列表到 NumPy 数组中导致数组的类型object v 1 1 2 np array v gt gt gt array 1 1 2 dtype object 尝试强制另一种类型将导致异常 np arra
  • 如何在 C# 中启用此计时器?

    我已经开始了 C 课程 但无法让计时器运行 它可能非常简单 我只是在这里错过了一些东西 基本上我有一个按钮来启动和停止交通灯序列 我想要 1 秒的间隔 这是我写的 当我按下开始键时 它没有按预期工作 谢谢 public int counte
  • MySQL:基于另一个字段添加序列列

    我正在处理一些遗留代码 数据库 并且需要向数据库添加一个字段 该字段将记录与该 外国 id 相关的序列号 示例表数据 当前 ID ACCOUNT some other stuff 1 1 2 1 3 1 4 2 5 2 6 1 我需要添加一
  • 如何更改动态 SQL 中的序列?

    我正在尝试创建一个脚本来将数据从一个数据库迁移到另一个数据库 我当前无法做的一件事是将序列的 nextval 设置为另一个数据库中序列的 nextval 我从 user sequences 中得到了值的差异 并生成了以下动态 SQL 语句
  • 如何迭代计算这个序列?

    我想迭代计算这个序列 A 0 j j 1 A i 0 A i 1 0 A i j A i 1 A i j 1 这是我的尝试 public function calculsuite1Action i j A array for k 0 k l
  • 寻找连续重复序列的算法

    我正在寻找一种算法 可以在基因组序列中找到短串联重复 基本上 给定一个非常长的字符串 它只能包含 4 个字符 ATCG 我需要找到彼此相邻的 2 5 个字符长之间的短重复 前任 TACATGAGATCATGATGATGATGATGGAGCT
  • 如何从Oracle数据库获取自增PK? [复制]

    这个问题在这里已经有答案了 可能的重复 PLSQL JDBC 如何获取最后一行ID https stackoverflow com questions 3552260 plsql jdbc how to get last row id 我已
  • 创建以字母数字开头的 Oracle 序列

    我想创建以字符开头的序列inv并增加 1 的价值观 INV01 INV02 INV03 etc CREATE SEQUENCE invoice nun START WITH INV INCREMENT BY 1 只能创建整数值序列 所以声明
  • Java 中的事件顺序

    我有两个独立组件的两个事件 但有一个问题 JTabbedPane 的 stateChanged 事件在 JFormattedField 的 focusLost 事件之前触发 有没有办法使 stateChange 事件在 focusLost
  • 使用 lambda 或 Stream API 合并流以生成交替序列

    我有一些按预期返回 Stream 的代码 但也许可以用某种类型的 lambda 或 stream 操作替换它 而不是耗尽 a 中的迭代器while loop 它只是一种交替流中元素的方法first and second当其中一个元素耗尽时停
  • Clojure 集合与序列的相等性

    我注意到 Clojure 1 4 似乎很乐意考虑向量等于seq相同的向量 但同样不适用于地图 1 2 seq 1 2 gt true 1 2 seq 1 2 gt false 为什么要这样的行为 这样会有所不同吗 Clojure 的 可以认
  • Data.Sequence 中的 inits 和 tails 如何工作?

    Louis Wasserman 编写了当前的实现inits and tails in Data Sequence 他表示它们非常高效 事实上 只要查看代码 我就可以看到 无论它们在做什么 它们都是以干净 自上而下的方式进行的 这往往会给惰性
  • Matplotlib - 使用 plt.imshow() 时序列关闭

    我在 Jupyter 笔记本中编写了一个狗分类器 每次在图像中检测到狗时 它都应该显示该图像并打印一些描述该图像的文本 不知何故 无论我按什么顺序放置 图像总是在打印所有文本后显示plt imshow and print 有谁知道为什么会这
  • Oracle 触发器创建时出现编译错误,ORA-02289: 序列不存在

    当我使用 PowerDesigner 生成 SQL 并在 Oracle 中运行它时 它会抛出错误 警告 触发器创建时出现编译错误 create trigger tib material classify before insert on m
  • ValueError:形状(无,50)和(无,1)在 Tensorflow 和 Colab 中不兼容

    我正在使用 LSTM 训练 Tensorflow 模型以进行预测维护 对于每个实例 我创建一个矩阵 50 4 其中 50 是历史序列的长度 4 是每个记录的特征数量 因此为了训练模型 我使用例如 55048 50 4 张量和 55048 1
  • 到底什么是序列?

    蟒蛇docs https docs python org 3 glossary html term sequence有点模棱两可 sequence 一个可迭代对象 支持通过以下方式使用整数索引进行有效的元素访问 getitem 特殊方法并定

随机推荐

  • SQL Server Management Studio 使用方法手记

    本方法只记录自己的操作方法 xff0c 仅供参考 一 下载安装 SQL Server Management Studio V15 0 18424 0 xff0c 链接 xff1a https download csdn net downlo
  • 蛇形填数(矩阵)

    蛇形填数 在 n x n 方针里填入 1 2 xff0c xff0c n x n xff0c 要求填成蛇形 例如 xff1a n 61 4时方阵为 xff1a 10 11 12 1 9 16 13 2 8 15 14 3 7 6 5 4 上
  • WERTYU

    Problem 1343 WERTYU Time Limit 1000 mSec Memory Limit 32768 KB Problem Description A common typing error is to place the
  • 分子量(Molar Mass)

    Molar mass Time limit 3 000 seconds An organic compound is any member of a large class of chemical compounds whose molec
  • 数数字(Digit Counting)

    Digit Counting Time limit 3 000 seconds Trung is bored with his mathematics homeworks He takes a piece of chalk and star
  • 周期串(Periodic Strings)

    周期串 Periodic Strings 如果一个字符串可以由某个长度为k的字符串重复多次得到 xff0c 则称该串以k为周期 例如 xff0c abcabcabcabc以3为周期 xff08 注意 xff0c 它也以6和12为周期 xff
  • 谜题(Puzzle)

    Puzzle Time limit 3 000 seconds Puzzle A children 39 s puzzle that was popular 30 years ago consisted of a 5x5 framewhic
  • 纵横字谜的答案(Crossword Answers)

    Crossword Answers Time limit 3 000 seconds Crossword Answers A crossword puzzle consists of a rectangular grid of black
  • DNA序列(DNA Consensus String)

    DNA Consensus String Time limit 3 000 seconds Figure 1 DNA Deoxyribonucleic Acid is the molecule which contains the gene
  • 古老的密码(Ancient Cipher)

    Ancient Cipher Time limit 3 000 seconds Ancient Roman empire had a strong governmentsystem with various departments incl
  • Java出现No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing

    原文转载自 sunny2038 的CSDN博客文章 原文地址 最近在看Java xff0c 在编译写书上一个例子时 xff0c 由于书上的代码只有一部分 xff0c 于是就自己补了一个内部类 结果编译时出现 xff1a No enclosi
  • 瑞星微开发工具下载镜像的配置方法?

    如何根据 parameter txt 建立配置表 xff1f 首先我们来看下 parameter txt 的内容 xff1a CMDLINE mtdparts 61 rk29xxnand 0x00002000 64 0x00004000 u
  • 特别困的学生(Extraordinarily Tired Students)

    Extraordinarily Tired Students Time limit 3 000 seconds When a student is too tired he can 39 t help sleeping in class e
  • 骰子涂色(Cube painting)

    Cube painting We have a machine for painting cubes It is supplied with three different colors blue red and green Each fa
  • 盒子(Box)

    Box Time limit 3 000 seconds Ivan works at a factory that produces heavy machinery He hasa simple job he knocks up woode
  • 循环小数(Repeating Decimals)

    Repeating Decimals Time limit 3 000 seconds Repeating Decimals The decimal expansion of the fraction 1 33 is wherethe is
  • 反片语(Ananagrams)

    反片语 Ananagrams 输入一些单词 xff0c 找出所有满足如下条件的单词 xff1a 该单词不能通过字母重排 xff0c 得到输入文本中的另外一个单词 在判断是否满足条件时 xff0c 字母不分大小写 xff0c 但在输出时应保留
  • 集合栈计算机(The SetStack Computer)

    The SetStack Computer Time limit 3 000 seconds 题目是这样的 xff1a 有一个专门为了集合运算而设计的 集合栈 计算机 该机器有一个初始为空的栈 xff0c 并且支持以下操作 xff1a PU
  • 代码对齐(Alignment of Code)

    Alignment of Code Time Limit 4000 2000 MS Java Others Memory Limit 32768 32768 K Java Others Total Submission s 958 Acce
  • Ducci序列(Ducci Sequence)

    Ducci Sequence Time limit 3 000 seconds A Ducci sequence is a sequence of n tuples of integers Given an n tuple of integ