UVA 1594 Ducci Sequence

来源:互联网 发布:excel删除不重复数据 编辑:程序博客网 时间:2024/05/17 01:55

题意:有一个n元组,变化到下一个状态时a[i]为上一个状态的a[i+1]-a[i](i=n时为a[1]-a[n]),一直变化问会循环还是变成n个0,最多一千步就能得到答案。3<=N<=15。

思路:可以考虑用map判重,循环一千次,如果当前状态重复了或者为全零就跳出循环输出。用struct类型作map的key时要重载<号。

(这里说要么会循环,要么会变为全零,也可以循环一千步,只判零,如果最后没有出现全零那一定是循环,这样写起来会简单一些)

#include <cstdio>#include <cmath>#include <cstring>#include <string>#include <cstdlib>#include <iostream>#include <algorithm>#include <map>using namespace std;#define rep(i,j,k) for (int i=j;i<=k;i++)#define Rrep(i,j,k) for (int i=j;i>=k;i--)#define Clean(x,y) memset(x,y,sizeof(x))#define LL long long#define ULL unsigned long long#define inf 0x7fffffff#define mod %100000007struct node{    int a[20];    int tot;    void change()    {        int temp = a[1];        rep(i,1,tot-1) a[i] = abs( a[i+1] - a[i] );        a[tot] = abs( temp - a[tot] );    }    bool operator < (const node t) const    {        rep(i,1,tot)            if ( a[i] != t.a[i] ) return a[i]<t.a[i];        return false;    }};bool check(node t,int n){    rep(i,1,n) if ( t.a[i]!=0 ) return false;    return true;}int n;map<node,int> flag;node x;int main(){    int T;    cin>>T;    while(T--)    {        cin>>n;        flag.clear();        x.tot = n;        rep(i,1,n)  cin>>x.a[i];        rep(i,1,1000)        {            if ( flag[x] || check(x,n) ) break;            flag[x] = 1;            x.change();        }        if ( check(x,n) )            puts("ZERO");        else            puts("LOOP");    }    return 0;}


0 0
原创粉丝点击