Skip to main content

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.

Comments

Popular posts from this blog

UDP server client in c

Server #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <stdio.h> #include <unistd.h> #include <errno.h> #include <string.h> #include <stdlib.h> int main() {         int sock;         int addr_len, bytes_read;         char recv_data[1024],send_data[1024];         struct sockaddr_in server_addr , client_addr;         if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {             perror("Socket");             exit(1);         }         server_addr.sin_family = AF_INET;         server_addr.sin...

My favourite writer Humayun Ahmed

There is none who can replace him.At least the standard which he create in is life time in the running literature it never be replaceable.The new generation which is all the time busy in playing PC games,chatting ,bands etc. only his writing makes them to take a glance on the literature.For example Himu and Misir ali all the time keep them on track by anti-logic and logic.They also have show-down on the novel.I myself read all of the books of Himu and Misir ali and wait for the new one to come every year.Now I have to wait for life time. Except books he makes our dirty film industry pure by his heart warming ,well versed film.Following him many new producer try to make well and good film (not the dirty one) .He also contribute our drama by his dashing drama's. In writing except romance ,logic ,anti-logic ,he also write many science fiction.His brother Sir Dr. Md. Zafar Iqbal is the man who started science fiction in bangla. The list of books: Selected novels • Lilaboti (2...

[ASTERIK] configure: error: *** uuid support not found (this typically means the uuid development package is missing)

ISSUE: Build error on Asterik , when you want test webrtc feature :) checking for uuid_generate_random in -luuid... no checking for uuid_generate_random in -le2fs-uuid... no checking for uuid_generate_random... no configure: error: *** uuid support not found (this typically means the uuid development package is missing) Fix: This issue arises due to missing of UUID generator specified by rfc4122 . +Linux sudo apt-get install uuid-dev  @Unix yum -y install libuuid-devel Asterik comes with lots of helpful script available on - asterisk/contrib/scripts/ folder of your ASTERIK source. So just use the following command on UNIX console to run the asterik pre-requisite script. contrib/scripts/install_prereq install And you are done! configuring. Now -- Make Asterik.