568 - Just the Facts

All about problems in Volume 5. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

starrynight
New poster
Posts: 7
Joined: Mon Mar 05, 2007 3:24 pm

how to improve it?

Post 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!!!
RED-C0DE
New poster
Posts: 16
Joined: Mon Mar 26, 2007 6:47 pm

Post 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;
}
mf
Guru
Posts: 1244
Joined: Mon Feb 28, 2005 4:51 am
Location: Zürich, Switzerland
Contact:

Post by mf »

You should follow the problem's output format.
coded
New poster
Posts: 1
Joined: Sat May 17, 2008 8:59 pm

Re: 568 time limit helllp

Post 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?
newton
Experienced poster
Posts: 162
Joined: Thu Jul 13, 2006 7:07 am
Location: Campus Area. Dhaka.Bangladesh
Contact:

Re: 568 time limit helllp

Post by newton »

May anybody explain, why i need to mod the fact by 1000000 rather than 10?
Thanks
kbr_iut
Experienced poster
Posts: 103
Joined: Tue Mar 25, 2008 11:00 pm
Location: IUT-OIC, DHAKA, BANGLADESH
Contact:

Re: 568 time limit helllp

Post 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?
It is tough to become a good programmer.
It is more tough to become a good person.
I am trying both...............................
vahid sanei
Learning poster
Posts: 84
Joined: Fri Jan 09, 2009 4:37 pm
Location: IRAN

Re: 568 time limit helllp

Post 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
Impossible says I`m possible
aldi3594
New poster
Posts: 1
Joined: Thu Jul 08, 2010 10:12 am

Re: 568 time limit helllp

Post 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;
}
sazzadcsedu
Experienced poster
Posts: 136
Joined: Sat Nov 29, 2008 8:01 am
Location: narayangong,bangladesh.
Contact:

Re: 568 time limit helllp

Post 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.
Life is more complicated than algorithm.
http://felix-halim.net/uva/hunting.php?id=32359
For Hints: http://salimsazzad.wordpress.com
uDebug
A great helper
Posts: 475
Joined: Tue Jul 24, 2012 4:23 pm

Re: 568 time limit helllp

Post by uDebug »

Replying to follow the thread.
Check input and AC output for over 7,500 problems on uDebug!

Find us on Facebook. Follow us on Twitter.
Post Reply

Return to “Volume 5 (500-599)”