/* by Hiroki Sumida-Takahashi 2022, July 26 */ 

/* relation3xw.txt 0,2,0,2  relation4.txt  1,3,1,3 */ 

#include <stdio.h>

void main()
  {
    int tmp,tmpp,tmppp,j,jj,dlt,p,k,im,jm,prep,pmax=65536; /*32768,65536*/
    int ct[100],ctt[100],cttt[100],ctttt[100],irg[100];
    FILE *file0,*file1,*file2,*file3;

  dlt=1;  /* dlt=0  for 3x, dlt=1 for 3w 4 */
  file0=fopen("relation3wall.txt","r");
  file1=fopen("zero3w.txt","a");
  file2=fopen("irreg3w.txt","a"); 
  file3=fopen("index3w.txt","a");

  for (j=0;j<100;j++)
    {
      ct[j]=0;
      ctt[j]=0;
      cttt[j]=0;
      ctttt[j]=0;
      irg[j]=0;
    }

  tmppp=1;
  prep=0; 
  while((fscanf(file0,"%d %d %d %d\n",&p,&k,&im,&jm)!=EOF)&&(p<pmax))
    {
    printf("%d ",p);
    fprintf(file3,"%d %d \n",p, k);
    if (p!=prep)
     {
       prep=p;
       irg[tmppp]++;
       tmppp=1;
     }
    else
     {
      tmppp++;
     }
  
    tmp=0;  
    for (j=0;j<(p-1)/2;j++)
      {
      if (j!=(p-3)/2)
        {
         fscanf(file0,"%d",&tmpp);
        }
      else
        {
         fscanf(file0,"%d\n",&tmpp);
        }
      if (tmpp==0)
        {
         jj=2*j+dlt;
         fprintf(file3," %d ",jj);
         tmp++;
        }
      }
   fprintf(file3,"\n");
   
    if (im==jm)
     {
      if (((p-1)%4==0)&&(k%4==0))
        {
         ct[tmp]++;
         }
      if (((p-1)%4==0)&&(k%4==2))
        {
         ctt[tmp]++;
        }
      if (((p-1)%4==2)&&(k%4==0))
        {
         cttt[tmp]++;
        }
      if (((p-1)%4==2)&&(k%4==2))
        {
         ctttt[tmp]++;
        }
    }

 }/*while*/

  
    for (j=0;j<20;j++)
      {
       fprintf(file1,"%d ",ct[j]);
      }
    fprintf(file1,"\n");

    for (j=0;j<20;j++)
      {
       fprintf(file1,"%d ",ctt[j]);
      }
    fprintf(file1,"\n");

    for (j=0;j<20;j++)
      {
       fprintf(file1,"%d ",cttt[j]);
      }
    fprintf(file1,"\n");

    for (j=0;j<20;j++)
      {
       fprintf(file1,"%d ",ctttt[j]);
      }
    fprintf(file1,"\n");

    for (j=0;j<20;j++)
      {
       fprintf(file2,"%d ",irg[j]);
      }
    fprintf(file2,"\n");

  fclose(file0);
  fclose(file1);
  fclose(file2);


  } /*main*/
