对于一个n元组(a 1 , a 2 , …, a n ),可以对于每个数求出它和下一个数的差的绝对值,得到
一个新的n元组(|a 1 -a 2 |, |a 2 -a 3 |, …, |a n -a 1 |)。重复这个过程,得到的序列称为Ducci序列,例如:
(8, 11, 2, 7) -> (3, 9, 5, 1) -> (6, 4, 4, 2) -> (2, 0, 2, 4) -> (2, 2, 2, 2) -> (0, 0, 0, 0).
也有的Ducci序列最终会循环。输入n元组(3≤n≤15),你的任务是判断它最终会变成0
还是会循环。输入保证最多1000步就会变成0或者循环。
题目很简单纯当复习C++了
看到有人用滚动数组速度挺快的(20ms) 我这个是 2490ms
因为没用使用to_string() 一直wa 后来一想直接等于 v[j] +48 肯定是会越界的 然后使用to_string() 果然一次AC了
然后加上ios::sync_with_stdio(false);时间降到了1450ms
#include <bits/stdc++.h>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
#define REP(i,n) for(int i = 0;i<(n);i++)
typedef long long LL;
const int maxd = 1005;
const int maxn = 1e4;
const int inf = 0x3f3f3f3f;
//inline int abs(int x){return x>0?x:-x;}
set<string>loop;
vector<int>v;
bool get_loop(int n)
{
int flag;
string s;
for(int i = 0;i<maxn;i++)
{
s.clear();
int t = v[0];
flag = 0;
for(int j = 0;j<n-1;j++)
{
v[j] = abs(v[j] - v[j+1]);
s+=to_string(v[j]);
if(!v[j])flag ++;
}
v[n-1] = abs(v[n-1] - t);
s+=to_string(v[n-1]);
// cout<<s<<endl;
// getchar();
if(!v[n-1]&&flag == n-1)return true;
if(loop.count(s))return false;
loop.insert(s);
}
return false;
}
int main(void)
{
// freopen("input.in","r",stdin);
// freopen("output.out","w",stdout);
int T,n;
cin>>T;
while(T--)
{
loop.clear();
v.clear();
cin>>n;
for(int i =0;i<n;i++)
{
int x;cin>>x;
v.push_back(x);
}
cout<<(get_loop(n)?"ZERO":"LOOP")<<endl;
}
return 0 ;
}