//============================================================================
// Name        : NewPartitions#.cpp
// Author      : Us
// Version     : 1.0
// Copyright   : GNU
// Description : DIMENSION "m": Finds partitions of the integral value (z) into (cardI -nc) i.e.
//               (number of edges - number of cycles) positive integers that make the determinants of the 
//               block components of A[#] equal to zero. Here "#" is the position of the multigraph 
//               in the list "lff" determined by the notebook file.
//============================================================================

#include <iostream>
#include <fstream>
#include <stdio.h>
#include <numeric>
#include <cstdlib>
#include <vector>
#include <list>


using namespace std;

static ofstream myfile;

// Please insert in the line below the variables x1,x2,....,xnv, where nv = number of edges - number of cycles

int CalculDet(unsigned short x1,....., unsigned short xnv)
{ 
    return ....PLEASE INSERT HERE THE DETERMINANT OBTAINED BY MATHEMATICA NOTEBOOK MinimalIW.nb IN CFORM...;
};

bool validSet(unsigned short * firstNum, unsigned short l)

// Please insert in the line below the expression "l*firstNum[0],.....,l*firstNum[nv2-1]", substituting nv2 by (number of edges - 1 - number of cycles)

{  if(	0 == CalculDet(l,l*firstNum[0],.....,l*firstNum[nv2-1]))
	{       myfile<< l<<" ";

// Please substitute "?" below by  (number of edges - 1 - number of cycles)

		for( int i = 0; i<?; i++) myfile<< l*firstNum[i]<<" ";	
		myfile<<endl;
	}
	return true;
};

void part(unsigned short k, unsigned short t, unsigned short l)
{

     // *** Attention: Partitions with no zeros where, since there is an edge from  P_0 to P_1, the 1st magnitude is equal to l:

	unsigned short r[k];
	int i,j,s,m;
        m=t/l-1;
	r[0]=m-k+1;
        for(i=1;i<k;i++){
		r[i]=1;	}
        validSet(r,l);
	while(r[k-1]!=m-k+1){
		i=k;
		while(r[--i]==1);
		if(i!=k-1){
			r[i]--;
			r[i+1]=2;
			validSet(r,l);
		}
		else{
			while(r[--i]==1);
			r[i]--;
			s=r[k-1];
			r[k-1]=1;
			r[i+1]=s+1;
			validSet(r,l);
		}
	}
};


int main()
{

// Please assign the values of the necessary constants: m= dimension of the manifold, 
// z= value of the integral, nc= number of cycles of the graph

    int m= .... ;
   
    int z= .... ;
   
    int nc= .... ;
   
    
    
    // **** Computes the number of edges

    int cardI = (m/2+1)*m/4; 
 
    // *** Writes the partitions in the file OutputPart?.txt where the first element 
    // is a divisor of the value of integral, smaller or equal than m/2+1; the other elements of the partition are multiples of this divisor;
    
    // Please substitute "?" below by the position of the multigraph in the list "lff"
     
     cout << "Writing partitions to the file: OutputPart?.txt"<<endl;
     myfile.open ("OutputPart?.txt");

   // Please substitute "#" below by the number of constants C dividing z between 1 and m/2 + 1.
   // Please insert the list "r[0];...;r[#-1]=..." of such divisors

     unsigned short r[#]; r[0]=1;...;r[#-1]=...;
     for(int i=0; i<#; i++){         
         part(cardI-nc-1,z,r[i]);}
     myfile.close();
     cout << "Finished. \n";
     cout << "\n";

     return 0;
}
