Ducci Sequence UVA - 1594 多元组表示方法
习惯用结构体包一个数组,要存进set/map的话需要重载一下小于号,这样就能判重了
#include<bits/stdc++.h> #include<stdio.h> #include<algorithm> #include<queue> #include<string.h> #include<iostream> #include<math.h> using namespace std; #define ll long long const int maxn=1e2+7; const int inf=0x3f3f3f3f; #define FOR(n) for(int i=1;i<=n;i++) #define pb push_back /******************************************************/ namespace fastIO{ #define BUF_SIZE 100000 #define OUT_SIZE 100000 #define ll long long //fread->read bool IOerror=0; inline char nc(){ static char buf[BUF_SIZE],*p1=buf+BUF_SIZE,*pend=buf+BUF_SIZE; if (p1==pend){ p1=buf; pend=buf+fread(buf,1,BUF_SIZE,stdin); if (pend==p1){IOerror=1;return -1;} //{printf("IO error!\n");system("pause");for (;;);exit(0);} } return *p1++; } inline bool blank(char ch){return ch==' '||ch=='\n'||ch=='\r'||ch=='\t';} inline void read(int &x){ bool sign=0; char ch=nc(); x=0; for (;blank(ch);ch=nc()); if (IOerror)return; if (ch=='-')sign=1,ch=nc(); for (;ch>='0'&&ch<='9';ch=nc())x=x*10+ch-'0'; if (sign)x=-x; } inline void read(ll &x){ bool sign=0; char ch=nc(); x=0; for (;blank(ch);ch=nc()); if (IOerror)return; if (ch=='-')sign=1,ch=nc(); for (;ch>='0'&&ch<='9';ch=nc())x=x*10+ch-'0'; if (sign)x=-x; } inline void read(double &x){ bool sign=0; char ch=nc(); x=0; for (;blank(ch);ch=nc()); if (IOerror)return; if (ch=='-')sign=1,ch=nc(); for (;ch>='0'&&ch<='9';ch=nc())x=x*10+ch-'0'; if (ch=='.'){ double tmp=1; ch=nc(); for (;ch>='0'&&ch<='9';ch=nc())tmp/=10.0,x+=tmp*(ch-'0'); } if (sign)x=-x; } inline void read(char *s){ char ch=nc(); for (;blank(ch);ch=nc()); if (IOerror)return; for (;!blank(ch)&&!IOerror;ch=nc())*s++=ch; *s=0; } inline void read(char &c){ for (c=nc();blank(c);c=nc()); if (IOerror){c=-1;return;} } #undef OUT_SIZE #undef BUF_SIZE }; using namespace fastIO; /*****************************************************/ struct NODE{ int n; int arr[16]; bool operator <(const NODE &b)const{ for(int i=1;i<=n;i++){ if(arr[i]!=b.arr[i])return arr[i]<b.arr[i]; } return 0; } }; //set<NODE>S; map<NODE,bool>vis; int main(){ int T; //scanf("%d",&T); read(T); while(T--){ vis.clear(); NODE st; //scanf("%d",&st.n); read(st.n); for(int i=1;i<=st.n;i++){ //scanf("%d",&st.arr[i]); read(st.arr[i]); } vis[st]=1; for(int p=1;p<=1111;p++){ int tmp=st.arr[1]; int flag=1; for(int i=1;i<st.n;i++){ st.arr[i]=abs(st.arr[i+1]-st.arr[i]); if(st.arr[i]!=0)flag=0; } st.arr[st.n]=abs(st.arr[st.n]-tmp); if(st.arr[st.n]!=0)flag=0; if(flag){ printf("ZERO\n"); break; } else if(!vis[st]){ //S.insert(st); vis[st]=1; }else if(vis[st]){ printf("LOOP\n"); break; } } } }