紫书第五章训练 uva 1594 Ducci Sequence by crq

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

题意分析:对一个数组不断按照以下过程进行变换(每个数变成后面一个元素与自身相减后取绝对值,最后一个数的下一个是第一个数):

(a1, a2, · · · , an) → (|a1 ? a2|, |a2 ? a3|, · · · , |an ? a1|)

到最后要么变成全部为0(输出ZERO), 要么形成死循环(LOOP)。

解决方法:因为数组元素个数最大才15,直接模拟,数组用vector记录,并用map记录每次变换后数组元素:

map<vector<int>, int> mp;

每次变换后查找map中是否已经存在,若已经存在则输出LOOP,结束。如果变换后结果是全部为0的数组,输出ZERO,结束。

AC源码:

 1 #include <iostream>
 2 #include <string>
 3 #include <vector>
 4 #include <sstream>
 5 #include <map>
 6 using namespace std;
 7
 8 map<vector<int>, int> mp;
 9 int myabs(int a)
10 {
11     return a>0?a:-a;
12 }
13
14 bool Zero(vector<int> &vec)
15 {
16     for(int i=0;i<vec.size();i++)
17     {
18         if(vec[i]!=0)
19             return false;
20     }
21     return true;
22 }
23
24 void Change(vector<int>& vec)
25 {
26     int n = vec.size();
27     int x = vec[0];
28     for(int i=0;i<n-1;i++)
29     {
30         vec[i] = myabs(vec[i+1]-vec[i]);
31     }
32     vec[n-1] = myabs(vec[n-1]-x);
33 }
34
35 int main()
36 {
37 //    freopen("d:\\data1.in","r",stdin);
38     int cas;
39     cin>>cas;
40     while(cas--)
41     {
42         mp.clear();
43         int i, n;
44         vector<int> vec;
45         cin>>n;
46         for(i=0;i<n;i++)
47         {
48             int x;
49             cin>>x;
50             vec.push_back(x);
51         }
52         int flag = 0;
53         while(true)
54         {
55             if(mp[vec])
56             {
57                 flag = 2;//loop
58                 break;
59             }
60             mp[vec] = 1;
61             if(Zero(vec))
62             {
63                 flag = 1;//zero
64                 break;
65             }
66             Change(vec);
67         }
68         if(flag==1)
69             cout<<"ZERO"<<endl;
70         else if(flag==2)
71             cout<<"LOOP"<<endl;
72     }
73     return 0;
74 }
时间: 2024-08-02 03:32:08

紫书第五章训练 uva 1594 Ducci Sequence by crq的相关文章

紫书第五章训练 uva 10763 Foreign Exchange by crq

Your non-profit organization (iCORE - international Confederation of Revolver Enthusiasts) coordinates a very successful foreign student exchange program. Over the last few years, demand has sky-rocketed and now you need assistance with your task.The

紫书第五章训练 uva 10391 Compound Words by crq

You are to find all the two-word compound words in a dictionary. A two-word compound word is a word in the dictionary that is the concatenation of exactly two other words in the dictionary. Input Standard input consists of a number of lowercase words

紫书第五章训练3 D - Throwing cards away I

D - Throwing cards away I Given is an ordered deck of n cards numbered 1 to n with card 1 at the top and card n at the bottom. The following operation is performed as long as there are at least two cards in the deck: Throw away the top card and move

紫书第五章训练2 F - Compound Words

F - Compound Words You are to find all the two-word compound words in a dictionary. A two-word compound word is a word in the dictionary that is the concatenation of exactly two other words in the dictionary. Input Standard input consists of a number

紫书第三章训练2 暴力集

A - Master-Mind Hints MasterMind is a game for two players. One of them, Designer, selects a secret code. The other, Breaker, tries to break it. A code is no more than a row of colored dots. At the beginning of a game, the players agree upon the leng

lrj紫书第五章

UVA-1592 1 // UVa1592 Database 2 // Rujia Liu 3 // 本程序只是为了演示STL各种用法,效率较低.实践中一般用C字符串和哈希表来实现. 4 5 #include<iostream> 6 #include<cstdio> 7 #include<vector> 8 #include<string> 9 #include<map> 10 #include<sstream> 11 using n

紫书第四章训练 UVA1589 Xiangqi by 15 周泽玺

Xiangqi is one of the most popular two-player board games in China. The game represents a battle between two armies with the goal of capturing the enemy's "general" piece. In this problem, you are given a situation of later stage in the game. Be

UVa 1594 - Ducci Sequence

没看清题号,TimeLimit.啊.  直接计算次数即可. 代码 : import java.util.*; public class Main1954 { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int t = scan.nextInt(); while(t-- > 0) { int[] a = new int[16]; int n = scan.nextInt(); for

UVA 1594 - Ducci Sequence(暴力模拟)

想麻烦了.这题真的那么水啊..直接暴力模拟,1000次(看了网上的200次就能A)后判断是否全为0,否则就是LOOP: 1 #include <iostream> 2 #include <sstream> 3 #include <cstdio> 4 #include <cstring> 5 #include <cmath> 6 #include <string> 7 #include <vector> 8 #include