ACM problem submit in Java with BigInteger in uva online judge

When to use :

First of all in c,c++ we have some problem with Big number Like 2^32 or more.This problem Can be solved by using c,c++ ,but it is more easy or faster to use Java here.
Because Java support BigInteger And BigDecimal in java.math.BigInteger library.
Sometimes we have to generate number series like Fibonacci upto 1000.It is more easy in java to generate these without C.Where C or c++ is more complex.
Note: When time is factor it is not right to use java
Library to use:

Import java.io.*;
Import java.math.BigInteger;
Import java.math.BigDecimal;
Import java.util.*;
Taking Input:
For taking input Try to use Scanner.
Because it handle all space and newline itself.
Format:
Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
It will take input from Commandline.
Use while(in.hasNext())  to take every input from file or input line.
After that you have to take it as BigInteger ,BigDecimal,
Int ,String.Because All of them can be taken by this.
Exp:   int no = in.nextInt(); 
Usable Math Operation:
Power
Mod
Add
Substruct
Divide
Multiply
Remainder
Compare
Using Procedure is given bellow:
All type Declaration:
BigInteger bi, bi1, bi2, bi3, bi4;
BigInteger[] bia; // array holding division result and remainder. String s; int i;
long lng;
float f;
Double d;
Note : Be sure to Submit solution By class Name as “Main”.
How to Use :
bi = new BigInteger(s); Create BigInteger with decimal value represented by decimal String s.
bi = BigInteger.ONE;Predefined value 1.
bi = BigInteger.ZERO;Predefined value 0.
bi = BigInteger.valueOf(lng);Use this factory method to create BigIntegers from numeric expressions. An int parameter will be automatically promoted to long.
bi1bi2.abs();Returns BigInteger absolute value.
bi1bi2.add(bi3);Returns sum of bi2 and bi3.
bi1bi2.divide(bi3);Returns division of bi2 and bi3.
biabi2.divideAndRemainder(bi3);Returns array of two BigIntegers representing the result of division and remainder of bi2 and bi3.
bi1bi2.gcd(bi3);Returns greatest common divisor of bi2 and bi3
bi1bi2.max(bi3);Returns maximum of bi2 and bi3.
bi1bi2.min(bi3);Returns minimum of bi2 and bi3.
bi1bi2.mod(bi3);Returns remainder after dividing bi2 by bi3 .
bi1bi2.multiply(bi3);Returns product of bi2 and bi3.
bi1bi2.pow(bi3);Returns bi2 to the bi3 power.
bi1bi2.remainder(bi3);Returns remainder of dividing bi2 by bi3. May be negative.
ibi.signum();-1 for neg numbers, 0 for zero, and +1 for positive.
bi1bi2.subtract(bi3);Returns bi2 - bi3.
dbi.doubleValue();Returns double value equivalent of bi.
fbi.floatValue();Returns float value equivalent of bi.
ibi.intValue();Returns int value equivalent of bi.
lngbi.longValue();Returns long value equivalent of bi.
sbi.toString();Returns decimal string representation of bi.
sbi.toString(i);Returns string representation of bi in radix i.
bbi1.compareTo(bi2);Returns negative number if bi1<bi2, 0 if bi1==bi2, or positive number if bi1>bi2.
Example of Code in Uva:

/**
* @(#)prob_948.java
* @author Sharma
* @version 1.00 2010/10/6
*/
import java.io.*;
import java.math.BigInteger;
import java.util.*;
public class prob_948 {
public static void main (String[] args) {
  Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
 
   BigInteger rp,pt,op,sub,di,re,st;
   BigInteger[] fb = new BigInteger[1050];
   fb[0] = BigInteger.ONE;
   fb[1] = BigInteger.ONE;
 
  
    for(int i=2;i<=1010;i++)
    fb[i] = fb[i-1].add(fb[i-2]);
            
    int cas = in.nextInt();
    op = BigInteger.ZERO

while(in.hasNext())
    {
     
          di =in.nextBigInteger();
          re = di;
          String str="";int o=0;
      if(di.compareTo(op) != 0)
      {
      for(int i=1000;i>=1;i--)
      {
      if(di.compareTo(fb[i]) >= 0)
      {
      str += "1";
      di = di.subtract(fb[i]);
      o = 1;
      }
      else if(o == 1)
      {
      str += "0";
      }
      }
      }
      else
      {
      str += "0";
      }
     
        System.out.println(re+" = "+str+" (fib)");
        cas = cas-1;
        if(cas == 0)break;
    }
     
  }
   
}
  Just change class prob_948 into class Main then it will accept.

Post a Comment