Page 3 of 3

how to improve it?

Posted: Mon Mar 05, 2007 5:09 pm
by starrynight
int main()
{
double a ,b;
double factorial(double);
double result;
double n;

while (scanf("%lf",&n) == 1)
{
if (n > 10000.)
break;
else
{
result = factorial(n);

if( fmod(result,10.) != 0.)
{
a = fmod(result,10.);
printf("%5lf -> %lf\n",n,a);
}
else
{
do
{
b = result / 10.;
}while ( fmod(b ,10.) == 0.);

b = fmod(b ,10.);
printf("%5lf -> %lf\n",n,b);
}


}

}



system("PAUSE");
return 0;
}
double factorial(double n)
{
if(n == 0 )
return 1;
else
return factorial(n-1) * n;
}




that is my code...
but when i input number above 26...
it cannot work..(even below 26..maybe)
can anyone help me..
it seems has a big trouble :( thx!!!

Posted: Wed Jul 11, 2007 2:28 pm
by RED-C0DE
r there tricky test-cases? my program works correctly with many test-cases but i got WA again & again... :(

please help me.
here is my little code for this problem :

Code: Select all

//568
#include <iostream>
using namespace std;

int main()
{
       int N;

       while(cin >> N)
       {
               unsigned long long fact = 1;

               for(int i=1; i<=N; i++)
               {
                       fact *= i;

                       while( !(fact % 10) )
                               fact /= 10;

                       fact %= 1000000;
               }

               cout << fact%10 << endl;
       }

       return 0;
}

Posted: Wed Jul 11, 2007 2:38 pm
by mf
You should follow the problem's output format.

Re: 568 time limit helllp

Posted: Sat May 17, 2008 9:15 pm
by coded

Code: Select all

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigInteger;

public class Main {

	private static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
	private static BigInteger [] mem = new BigInteger [9000];

	public static void main(String[] args) {

		while (true){
			String input = readLn();

			if(input==null)
				break;

			int espacos = 5;

			for(int i=0;i<input.length();i++){
				espacos=espacos-1;
			}

			String res_input="";
			for(int i=0;i<espacos;i++){
				res_input = res_input.concat(" ");
			}

			String res = res_input.concat(input);

			long input_long = Long.parseLong(input);
			BigInteger input_big = BigInteger.valueOf(input_long);

			//BigInteger fact = calculaFactorial(input_big);
			BigInteger fact = calculaFactorialIterativo(input_big.intValue());

			String fact_str = fact.toString();

			char [] fact_char = fact_str.toCharArray();
			char num_res=' ';

			num_res=fact_char[fact_char.length-1];


			/*for(int i = fact_char.length-1;i>=0;i--){
				if(fact_char[i]!='0'){
					num_res=fact_char[i];
					break;
				}
			}*/

			System.out.println(res+" -> "+num_res);

		}
	}


	/*public static BigInteger calculaFactorial(BigInteger num){

		if(mem[num.intValue()]!=null){
			return mem[num.intValue()];
		}

		else{
			if(num.equals(BigInteger.ONE))
				return BigInteger.ONE;
			else{
				BigInteger valor = num.multiply(calculaFactorial(num.subtract(BigInteger.ONE)));

				if(valor.mod(BigInteger.valueOf(10)).equals(BigInteger.ZERO)){
					mem[num.intValue()]=valor;
					//System.out.println(valor);
					return valor.divide(BigInteger.valueOf(10));
				}
				else
					return valor;
			}
		}

	}*/

	public static BigInteger calculaFactorialIterativo(int valor) {

		BigInteger resultado = BigInteger.ONE;

		if (valor <= 1) {
			return BigInteger.ONE;
		}


		else {

			if(valor<9000){

				if(mem[valor]!=null){
					//System.out.println("está na tabela");
					return tiraZerosBigInteger(mem[valor]);
				}

				else{
					//System.out.println("vou calcular");

					for (int factor_actual = valor; factor_actual >= 2; --factor_actual) {

						if(mem[factor_actual]!=null){
							//System.out.println("parte dele está na tabela");
							resultado = resultado.multiply(mem[factor_actual]);

							if(factor_actual<9000)
								mem[valor]=tiraZerosBigInteger(resultado);

							return tiraZerosBigInteger(resultado);
						}

						resultado = resultado.multiply(BigInteger.valueOf(factor_actual));

					}

					return tiraZerosBigInteger(resultado);
				}
			}

			else{
				for (int factor_actual = valor; factor_actual >= 2; --factor_actual) {
					resultado = resultado.multiply(tiraZeros(factor_actual));

					if(factor_actual<9000){

						if(mem[factor_actual]!=null){
							resultado = resultado.multiply(tiraZerosBigInteger(mem[factor_actual]));
							return resultado;
						}
					}
				}

				return tiraZerosBigInteger(resultado);
			}
		}
	}


	public static BigInteger tiraZeros(int num){

		while(num%10==0)
			num=num/10;

		return BigInteger.valueOf(num);

	}

	public static BigInteger tiraZerosBigInteger(BigInteger num){

		while(num.mod(BigInteger.valueOf(10)).equals(BigInteger.ZERO))
			num=num.divide(BigInteger.valueOf(10));

		return num;

	}


	public static String readLn(){

		try {
			String res = br.readLine();
			if(res!=null) 
				return res.trim();
			else 
				return null;

		} catch (IOException e) {
			return null;
		}

	}

}
How can i optimize it?

Re: 568 time limit helllp

Posted: Thu Jul 03, 2008 2:33 pm
by newton
May anybody explain, why i need to mod the fact by 1000000 rather than 10?
Thanks

Re: 568 time limit helllp

Posted: Wed Jul 09, 2008 10:45 am
by kbr_iut
u have to mode the variable each time multiplying with increasing another variable with 100000 not 1000000.
actually my question is same ,,,,,,why i have to mode with 100000.Is anybody to answer this question?

Re: 568 time limit helllp

Posted: Sat Feb 28, 2009 3:44 pm
by vahid sanei
because n is to 10000 and you should use 100000
if n was to 100000 you should use 1000000 , i think :D

Re: 568 time limit helllp

Posted: Thu Jul 08, 2010 10:16 am
by aldi3594
pls help.. got WA.. i dont know what's wrong :(

Code: Select all

#include<cstdio>
int a,b,c,d;
int main(){
    while(scanf("%d",&a)!=EOF){
          if(a==0) printf("    0 -> 1\n");
          else{
               c=1;
               for(b=1;b<=a;b++){
                    c=c*b;
                    while(c%10==0){
                         c=c/10;        
                    }
                    while(c>=100){
                         c=c%100;
                    }
               }   
               d=c%10;
               printf("%5d -> %d\n",a,d);      
          }           
    }
    return 0;
}

Re: 568 time limit helllp

Posted: Fri Jul 09, 2010 9:45 am
by sazzadcsedu
How do u expect to get Acc? Your output don't even pass sample input.
for input 9999

sample output-
9999 -> 8

your output:-

9999 -> 2
you are taking mod(100), But you r calculating factorial of number greater than 100, in fact like 10000 . So take mod greater than 10000 ,otherwise there will be some loss of information and you will not get current answer.

Re: 568 time limit helllp

Posted: Sat May 03, 2014 1:19 pm
by uDebug
Replying to follow the thread.