How Many TrianglesProblem code: CB02 |
You are given N poles of length 1, 2, 3, ... , N. You need to tell us the number of triangles that can be formed using any three of the given poles. Constraints: 3 <= N <= 100,000
Input
The first line of the input contains the number of test cases T, at most 10000. Each of the next T lines contains a positive number N.
Output
For each value of N in the input, output on a new line the number of triangles that can be formed using the given poles.
Example
Input: 2 3 4 Output: 0 1
| Author: | moneymachine |
| Date Added: | 16-01-2010 |
| Time Limit: | 3 sec |
| Source Limit: | 50000 Bytes |
| Languages: | ADA, ASM, BASH, C, C99 strict, CAML, CLOJ, CLPS, CPP 4.0.0-8, CPP 4.3.2, CS2, D, ERL, F#, FORT, GO, HASK, ICON, JAR, JAVA, LISP clisp, LISP sbcl, LUA, NEM, NICE, PAS fpc, PAS gpc, PERL, PERL6, PHP, PIKE, PYTH, PYTH 3.1.2, RUBY, SCALA, SCM guile, SCM qobi, ST, TEXT |
Comments

Fetching successful submissions

Is this being conducted as a
Is this being conducted as a part of any fest?
#include<stdio.h>#include<std
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int main ()
{
int *vertex_cover;
int **p;
int **p1;
int vertex_cover_size;
int i,j,k,carry,l,k1,no_of_vertex,flag;
int y;// change it char later
int space_size;
int **bit_string;
float **transit_matrix;
int neighbours;
int *neighbour_list;
// enter the graph's no of vertex
printf("enter the no of vertex");
scanf("%d",&no_of_vertex);
space_size=pow(2,no_of_vertex);
p=(int**)malloc(no_of_vertex*sizeof(int));
p1=(int**)malloc(no_of_vertex*sizeof(int));
vertex_cover=(int *)malloc(no_of_vertex*sizeof(int));
transit_matrix=(float**)malloc(space_size*sizeof(float));
bit_string=(int**)malloc(space_size*sizeof(int));
neighbour_list=(int*)malloc(no_of_vertex*sizeof(int));
for(i=0;i<no_of_vertex;i++)
p[i]=(int*)malloc(no_of_vertex*sizeof(int));
for(i=0;i<no_of_vertex;i++)
for(j=0;j<no_of_vertex;j++)
p[i][j]=0;
printf("p_0_0 =%d ",p[0][0]);
for(i=0;i<no_of_vertex;i++)
p1[i]=(int*)malloc(no_of_vertex*sizeof(int));
printf("p_0_0 =%d ",p[0][0]);
for(i=0;i<space_size;i++)
transit_matrix[i]=(float*)malloc(space_size*sizeof(float));
printf("p_0_0 =%d ",p[0][0]);
for(i=0;i<space_size;i++)
/ { printf("ni=%d,p_0_0=%d,p_0_2=%d",i,p[0][0],p[0][2]); bit_string[i]=(int*)malloc((no_of_vertex)*sizeof(int));}
printf("p_0_0 =%d ",p[0][0]);
//initialization of vertex cover
for(i=0;i<no_of_vertex;i++)
vertex_cover[i]=1;
for(i=0;i<space_size;i++)
for(j=0;j<no_of_vertex+1;j++)
bit_string[i][j]=0;
for(i=0;i<space_size;i++)
{
for(j=0;j<space_size;j++)//prob
{
transit_matrix[i][j]=0;
}
}
printf("p_0_0 =%d ",p[0][0]);
//y='y';
// enter the graphs no of edges
while(1)
{
printf("nenter an edge: first end point ");
scanf("%d",&i);
printf("n the other end point");
scanf("%d",&j);
p[i][j]=1;
p[j][i]=1;
printf("p_0_0 =%d ",p[0][0]);
printf("continue 1/0?");
scanf("%d",&y);
printf("%d",y);
if(y==0)
break;
//printf("n more edge y/n? ");
//scanf("%c",&y);
}
//printing the adjacency matrix
printf("the adjacency matrix is : n");
for(i=0;i<no_of_vertex;i++)
{ for(j=0;j<no_of_vertex;j++)
{
printf("%d ",p[i][j]);
}
printf("n");
}
printf("n%dn",space_size); //check statement
//////////////////////////////////////////////////////
for(k=0;k<space_size;k++)
{
printf("nk loop againn");
// code for bit_stringrating the new bit_string using previous one
carry=1;
/*if(k>0)
for(l=no_of_vertex-1;l>-1;l--)
{
if(bit_string[k-1][l]==0 && carry==1)
{bit_string[k][l]=1;carry=0;}
else if (bit_string[k-1][l]==1 && carry==1) //code me prob hai
{bit_string[k][l]=0;carry=1;}
else if(bit_string[k-1][l]==0 && carry==0)
{bit_string[k][l]=0;}
else if(bit_string[k-1][l]==1 && carry==0)
{bit_string[k][l]=1;}
}
*/
printf("nbitstring check=%d",bit_string[10][0]);
if(k>0)
for(l=0;l<no_of_vertex;l++)
{
if(bit_string[k-1][l]==0 && carry==1)
{bit_string[k][l]=1;carry=0;}
else if (bit_string[k-1][l]==1 && carry==1) //code me prob hai
{printf("nabcd%d %dn",k,l);bit_string[k][l]=0;printf("seggi prob array me nahi hai");carry=1;}
else if(bit_string[k-1][l]==0 && carry==0)
{bit_string[k][l]=0;}
else if(bit_string[k-1][l]==1 && carry==0)
{bit_string[k][l]=1;}
}
printf("nthe bit_string is : ");
for(l=0;l<no_of_vertex;l++) //current bit_string
printf("%d",bit_string[k][l]);
for(l=0;l<no_of_vertex;l++)
{
neighbour_list[l] = -1;
}
neighbours=0;
for(l=0;l<no_of_vertex;l++)
{
for(i=0;i<no_of_vertex;i++) //sampling a vertex cover
vertex_cover[i]=bit_string[k][i];
if(vertex_cover[l]==0) // mutation at lth position to get lth neighbour
vertex_cover[l]=1;
else
vertex_cover[l]=0;
//get the value of k1 i.e. the value of bitstring after mutation now
k1=0;
for(i=0;i<no_of_vertex;i++)
{
if(vertex_cover[i]==1)
k1+= pow(2,i);
}
printf("nloop val=%dn",k);
////////////////////////////////////////////////////////////////
for(i=0;i<no_of_vertex;i++) //making a copy of adjacency matrix
for(j=0;j<no_of_vertex;j++)
p1[i][j]=p[i][j];
// checking whether the neighbour vertex is a vertex cover or not
for(i=0;i<no_of_vertex;i++)
{
if(vertex_cover[i]==1)
for(j=0;j<no_of_vertex;j++)
{
if(p1[i][j]==1)
{
p1[i][j]=0;
p1[j][i]=0;
}
}
}
for(i=0;i<no_of_vertex;i++)
for(j=0;j<no_of_vertex;j++)
{
if(p1[i][j]==1)
flag=1;
}
if(flag==1)
{
printf(" ::not a vertex cover");
}
else
{
j=0;
for(i=0;i<no_of_vertex;i++)
{
if(vertex_cover[i]==1)
j++;
}
vertex_cover_size=j;
printf(" fitness:%d ",j);
printf(" ::vertex cover");
neighbours++;
neighbour_list[l]=k1;
}
printf("nl value=%dn",l);
}// end of l-loop
//////somewhere we have to prep transit matrix probabality by counting the which were the valid neighbours///////////
printf("noutside l loopn");
for(l=0;l<no_of_vertex;l++)
{
if(neighbour_list[l]>=0)
transit_matrix[k][neighbour_list[l]]= (float)1.0/neighbours;
}
printf("ntransit value filledn");
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
flag=0;
}//end of k-loop
printf("jklakjfkljakfjkaljfklajfkjakjfkajkfjakjfkajfjakljfajklfjakjfjaklfjlajlaf");
for(i=0;i<space_size;i++)
{ for(j=i+1;j<space_size;j++)
{
printf("%f",transit_matrix[i][j]);
}
}
}
sorry !! i pasted the wrong
sorry !! i pasted the wrong code!!...............