#include"lib.h"

fc fo;
int mot[1000000], code[1000], mot2[1000000];
int i, j, s, k, size, x, nb, mnb, pos, a_o, lim, l;

int check(){
  for(k = size; k < lim; k++){
    mot[k] = (mot[k/size]+mot[k%size])%a_o;
    if(!fo(mot, k)) return 0;
  }
  return 1;
}

int A(){
  for(i = 0; i < size; i++) mot[i] = 0;
  mnb = nb = 0;
  printf("a_o=%d size=%d lim=%d", a_o, size, lim); date("");
  for(*mot = i = 0; ;) if(!fo(mot, i)){
    here: while(mot[i] == a_o-1) if(!--i) return 1;
    mot[i]++;
    if(i < a_o && mot[i] > i) return 1;
  }else if(i+1 < size) mot[++i] = 0; else if(check()) break; else goto here;
  for(x = 0; x < a_o; x++, printf("\n")) for(k = 0; k < size; k++) printf("%d", (mot[k]+x)%a_o);
  printf("OK a_o=%d size=%d lim=%d", a_o, size, lim); date("");
  return 0;
}

void usage(){
  printf("Usage: aas_mul alphabet_size [size [lim]]\n");
  exit(0);
}

int main(int ac, char **av){
  if(ac < 2) usage();
  a_o = atoi(av[2]);
  if(a_o == 3) fo = aas_mul3;
  else if(a_o == 4) fo = aas_mul4;
  else if(a_o == 5) fo = aas_mul5;
  else usage();
  size = ac > 2 ? atoi(av[2]) : 2;
  lim  = ac > 3 ? atoi(av[3]) : 1000;
  for(; A() && size < 200; size++);
}
/*
a_o=3 size=24 lim=5000: 05/01/2007 18/26/30
012021201021012102120210
120102012102120210201021
201210120210201021012102
OK a_o=3 size=24 lim=5000: 05/01/2007 18/27/09
10000 ->>

a_o=4 size=36 lim=10000: 07/01/2007 01/06/51
012132303202321020123021203020121310
123203010313032131230132310131232021
230310121020103202301203021202303132
301021232131210313012310132313010203
OK a_o=4 size=36 lim=10000: 07/01/2007 06/43/47
100000 ->>
//*/