Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

S.O.S Help Problem The Last Zero Digit PLEASE

Posted by chiperortiz at 2007-06-20 09:45:03
The Last Non Zero Digit

I Calculate C(N,K) And Then Calculate P(N,K) = (K!) * C(N,K);

But I Get Time Limit Help I Just A Beginner 

Cristian Ortiz From Venezuela!!

import java.io.*;
import java.util.*;


public class Main
{    

    static char Digito (long Num)
    {
        String Aux = Long.toString(Num);
        int     m = Aux.length()-1;
        if (m == 0 && Aux.charAt(0) != 0) 
            return (Aux.charAt(0));  
        for (int i=m;i!=0;i--)
            if (Aux.charAt(i) != 48) return (Aux.charAt(i));
        return ('\r');
    }
    
  static long Factorial (long Num)
    {
       long Acu = 1;
       if (Num == 0 || Num == 1) return 1;
       for (int i = 2;i<=Num;i++) Acu *= i; return Acu;
    }

   static long gcd(long n1, long n2)
    {
      if (n2 == 0) return n1;
      else return gcd(n2, n1 % n2);
    }
    
   public static void main(String[] args)
    {
       try
       {
          BufferedReader   Leer  = new BufferedReader (new InputStreamReader (System.in));
          //BufferedReader   Leer  = new BufferedReader (new FileReader (new File ("Hola.txt")));
          StringTokenizer Cadena = new StringTokenizer (Leer.readLine());          
          long i, j;
          long bb, bs;
          long g;
          int M = Integer.parseInt (Cadena.nextToken()), N = Integer.parseInt (Cadena.nextToken());
          while (true)
          {    
            bb = 1; bs = 1;
           if (N == 0)     
               bs = 1;
            else
           if (N == 1)     
               bs = M;
            else 
           if (M == (N+1))
               bs = M;      
           else
           if (M == N) 
               bs = 1;    
           else
	   for (i = N, j = M; i >= 1; i--, j--)
           {
	     bb *= i;	             bs *= j;
	     g = gcd(bb, bs);	     bb /= g;	     bs /= g;
           }  	
            long k = Factorial (N) * bs;
            System.out.println (k+" "+Digito(k));
            Cadena = new StringTokenizer (Leer.readLine());          
            M = Integer.parseInt (Cadena.nextToken()); N = Integer.parseInt (Cadena.nextToken());
          }   
       }
       catch (Exception e)
       {
          System.out.println ("Fuck "+e);
       }                 
    }    
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator