// IPSC 2000, program solving data set E2

#include <stdio.h>

#define n 11

void boo(int a[])       //first element
{
  int i;
  for(i = 0; i < n; i++) {
    a[i] = i;
  }  
}

void foo(int a[])      //next element
{
  int i, c;
  c = (a[n-1] + a[0]/3) % 1001;
  for(i = 0; i < n-1; i++) {
    a[i] = a[i+1];
  }
  a[n-1] = c;
} 

int equal(int a[], int b[])  //do arrays contain the same elements?
{
  int i, result;
  result = 1;
  for(i = 0; i < n; i++) 
    if(a[i] != b[i]) result = 0;
  return result;
}

void copy(int a[], int b[])  //copy b to a
{
  int i;
  for(i=0; i<n; i++) 
    a[i]=b[i];
}

void main(void)
{
  int a[n],b[n];             //two elements of a sequence 
  long i,p,d;

  boo(a);
  boo(b);
  foo(b);

  //start with two pointers a and b, b going twice as fast as a
  //go until they meet somewhere in the loop
  while (!equal(a,b)) {
    foo(a);
    foo(b); foo(b);
  }

  //find out the period of the loop
  //keep a in place and move b until they meet
  copy(b,a);
  foo(b);
  d = 1;
  while (!equal(a,b)) {
    d++;
    foo(b);
  }
  
  //find the pre-period
  //first set b to be d elementf from start
  boo(b);
  for (i = 0; i<d; i++) foo(b);

  //move a and b simultaneously, keeping b d appart
  //until they meet
  p = 0;
  boo(a);
  while (!equal(a, b)) {
    foo(a);
    foo(b);
    p++;
  }
  
  printf(" pre-period %ld period %ld result %ld \n",p,d,p+d);
}















