Page 1 of 1

Getting TLE again and again

Posted: Mon Jul 28, 2008 10:30 am
by assasin
I could not figure out why my code is getting TLE?
Plese someone help me to find it out and give me some input so that my code runs infinitely

Here is my code:

Code: Select all

#include <stdio.h>
#include <math.h>

#define epsilon 0.0000001

int main(){

	double root,leaf;
	double lazy,height;
	double level,branch;
	bool ok = false;
	

	while(scanf("%lf%lf",&root,&leaf) ){
	
		if(root == 0 && leaf == 0)
			break;
		
		if(root == 1 && leaf == 1){	
			printf("0 1\n");
		}
		
		else if(leaf == 1){
			
			double k = log(root)/log(2);
			double Sheight = pow(2,k+1)-1;
						
			printf("%.0lf %.0lf\n",k,Sheight);
			
		}
		
		else{

			ok = false;		
			//finding branch
			branch = 2.0;
			while(!ok){
				
				double temp1,temp2;
				
				temp1 = log(root)/log(leaf);
				temp2 = log(branch+1.0)/log(branch);
				
				if(fabs(temp1 - temp2) <= epsilon)
					ok = true;	
				else branch+=1.0;
			}
			
			level = log(leaf) / log(branch);
			
			lazy = ( pow(branch,level) - 1 ) / (branch - 1);
			
	
			double loop = 1.00;
			height = root;
			
			while(1){
			
				height = height +  ( root / pow(branch+1,loop) ) * pow(branch,loop);
				if( fabs(loop - level) <= epsilon )
					break;
				else loop += 1.0;
			}
			
			printf("%.0lf %.0lf\n",lazy,height);
		}
	}
	
	return 0;
}
Thanks

Re: Getting TLE again and again

Posted: Mon Jul 28, 2008 5:29 pm
by Jan
Search the board first. And if you find no thread then open a new one with problem number and name.