Ducci Sequence
Description
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$](http://www.th7.cn/d/file/p/2015/10/06/1f62253e84bbf3341a43a6e641a8f36a.jpg)
(8, 11, 2, 7)
![$/displaystyle /rightarrow$](http://www.th7.cn/d/file/p/2015/10/06/1f62253e84bbf3341a43a6e641a8f36a.jpg)
![$/displaystyle /rightarrow$](http://www.th7.cn/d/file/p/2015/10/06/1f62253e84bbf3341a43a6e641a8f36a.jpg)
![$/displaystyle /rightarrow$](http://www.th7.cn/d/file/p/2015/10/06/1f62253e84bbf3341a43a6e641a8f36a.jpg)
![$/displaystyle /rightarrow$](http://www.th7.cn/d/file/p/2015/10/06/1f62253e84bbf3341a43a6e641a8f36a.jpg)
![$/displaystyle /rightarrow$](http://www.th7.cn/d/file/p/2015/10/06/1f62253e84bbf3341a43a6e641a8f36a.jpg)
(4, 2, 0, 2, 0)
![$/displaystyle /rightarrow$](http://www.th7.cn/d/file/p/2015/10/06/1f62253e84bbf3341a43a6e641a8f36a.jpg)
![$/displaystyle /rightarrow$](http://www.th7.cn/d/file/p/2015/10/06/1f62253e84bbf3341a43a6e641a8f36a.jpg)
![$/displaystyle /rightarrow$](http://www.th7.cn/d/file/p/2015/10/06/1f62253e84bbf3341a43a6e641a8f36a.jpg)
![$/displaystyle /rightarrow$](http://www.th7.cn/d/file/p/2015/10/06/1f62253e84bbf3341a43a6e641a8f36a.jpg)
![$/displaystyle /rightarrow$](http://www.th7.cn/d/file/p/2015/10/06/1f62253e84bbf3341a43a6e641a8f36a.jpg)
![$/displaystyle /rightarrow$](http://www.th7.cn/d/file/p/2015/10/06/1f62253e84bbf3341a43a6e641a8f36a.jpg)
(2, 0, 0, 2, 0)
![$/displaystyle /rightarrow$](http://www.th7.cn/d/file/p/2015/10/06/1f62253e84bbf3341a43a6e641a8f36a.jpg)
![$/displaystyle /rightarrow$](http://www.th7.cn/d/file/p/2015/10/06/1f62253e84bbf3341a43a6e641a8f36a.jpg)
![$/displaystyle /rightarrow$](http://www.th7.cn/d/file/p/2015/10/06/1f62253e84bbf3341a43a6e641a8f36a.jpg)
![$/displaystyle /rightarrow$](http://www.th7.cn/d/file/p/2015/10/06/1f62253e84bbf3341a43a6e641a8f36a.jpg)
![$/displaystyle /rightarrow$](http://www.th7.cn/d/file/p/2015/10/06/1f62253e84bbf3341a43a6e641a8f36a.jpg)
![$/displaystyle /rightarrow$](http://www.th7.cn/d/file/p/2015/10/06/1f62253e84bbf3341a43a6e641a8f36a.jpg)
(2, 0, 2, 0, 0)
![$/displaystyle /rightarrow$](http://www.th7.cn/d/file/p/2015/10/06/1f62253e84bbf3341a43a6e641a8f36a.jpg)
![$/displaystyle /rightarrow$](http://www.th7.cn/d/file/p/2015/10/06/1f62253e84bbf3341a43a6e641a8f36a.jpg)
![$/displaystyle /rightarrow$](http://www.th7.cn/d/file/p/2015/10/06/1f62253e84bbf3341a43a6e641a8f36a.jpg)
![$/displaystyle /rightarrow$](http://www.th7.cn/d/file/p/2015/10/06/1f62253e84bbf3341a43a6e641a8f36a.jpg)
![$/displaystyle /rightarrow$](http://www.th7.cn/d/file/p/2015/10/06/1f62253e84bbf3341a43a6e641a8f36a.jpg)
![$/displaystyle /rightarrow$](http://www.th7.cn/d/file/p/2015/10/06/1f62253e84bbf3341a43a6e641a8f36a.jpg)
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$](http://www.th7.cn/d/file/p/2015/10/06/39c93516fe388ef9f0b4b69a65eb222e.jpg)
![$ /le$](http://www.th7.cn/d/file/p/2015/10/06/39c93516fe388ef9f0b4b69a65eb222e.jpg)
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 ![](http://www.th7.cn/d/file/p/2015/10/06/9c32a529de5ebb97cb93e29289c4b4cf.jpg)
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
题意:
对于一个n元组(a0,a1,...),一次变换后变成(|a0-a1|,|a1-a2|,...)
问1000次变换以内是否存在循环。
思路:
模拟,map判重
#include <iostream>
#include <cstdio>
#include <map>
#include <cmath>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--){
map<string,bool> flag;
int n;
cin>>n;
string ans;
for(int i=0;i<n;i++){
ans+='0';
ans+=',';
}
//cout<<ans<<endl;
int a[20];
for(int i=0;i<n;i++)
cin>>a[i];
while(1){
string str;
for(int i=0;i<n;i++){
char temp[20];
sprintf(temp,"%d",a[i]);
string te=temp;
str+=te;
str+=',';
}
if(str==ans){
cout<<"ZERO"<<endl;
break;
}
if(flag[str]){
cout<<"LOOP"<<endl;
break;
}
flag[str]=1;
int tem=a[0];
for(int i=0;i<n-1;i++)
a[i]=abs(a[i]-a[i+1]);
a[n-1]=abs(a[n-1]-tem);
// cout<<str<<endl;
}
}
}