skp1893's blog

By skp1893, history, 3 months ago, In English,

Some Big integer problems for beginners Solving BigInteger Problem is fun. There are many problem solvers around the world who come to use Java BigInteger class though they do not code in Java usually. Here are some simple problems with their links and solution. This problem are chosen from different online judges. Try yourself before you see the solution.

Codeforces Gym : 112

Solution

import java.util.Scanner ;
import java.math.BigInteger;
public class Main{
  public static void main(String[] args){
    int a,b;
    Scanner input = new Scanner(System.in);
    a = input.nextInt();
    b = input.nextInt();
    BigInteger A = BigInteger.valueOf(a);
    BigInteger B= BigInteger.valueOf(b);
    System.out.printf("%d",A.pow(b).subtract(B.pow(a)));
    
  }
}

Uva 10183 — How Many Fibs?

Solution

import java.util.*;
import java.math.*;
public class Main{
  public static void main(String[] args){
    Scanner input = new Scanner(System.in);
    BigInteger arr[] = new BigInteger[50000];
    arr[0]= BigInteger.valueOf(1);
    arr[1]= BigInteger.valueOf(2);
    for(int i=2;i<50000;i++){
      arr[i] = arr[i-1].add(arr[i-2]);
    }
    while(true){
      
      BigInteger a,b;
      a = input.nextBigInteger();
      b = input.nextBigInteger();
      if(b.compareTo(BigInteger.valueOf(0))==0){
        break;
      }
      int count=0;
      for(int i=0;i<50000;i++){
        if(arr[i].compareTo(a)>=0 && arr[i].compareTo(b)<=0){
          count++;
        }
        if(arr[i].compareTo(b)>0){
          break;
        }
      }
      System.out.println(count);
    }
  }
}

URI 1279

Solution

import java.util.*;
import java.math.*;
import java.io.*;  
public class Main{
  public static void main(String[] args){
    Scanner input = new Scanner(System.in);
    boolean start = true;
    while(input.hasNext()){
      boolean leap_year = false;
      boolean ordinary = true;
      if(start == false){
        System.out.print("\n");
      }
      start = false;
      BigInteger year;
      year = input.nextBigInteger();
      BigInteger four;
      four = BigInteger.valueOf(4);
      BigInteger fourh;
      fourh = BigInteger.valueOf(400);
      BigInteger oneh;
      oneh = BigInteger.valueOf(100);
      BigInteger temp = year.remainder(four);
      if(temp.compareTo(BigInteger.valueOf(0))==0){
        temp = year.remainder(oneh);
        if(temp.compareTo(BigInteger.valueOf(0))==0){
          temp = year.remainder(fourh);
          if(temp.compareTo(BigInteger.valueOf(0))==0){
            System.out.println("This is leap year.");
            leap_year = true;
            ordinary = false;
          }
        }
        else{
          System.out.println("This is leap year.");
          leap_year = true;
          ordinary = false;
        }
      }
      temp = year.remainder(BigInteger.valueOf(15));
      if(temp.compareTo(BigInteger.valueOf(0))==0){
        System.out.println("This is huluculu festival year.");
        ordinary = false;
      }
      if(leap_year==true){
        temp = year.remainder(BigInteger.valueOf(55));
        if(temp.compareTo(BigInteger.valueOf(0))==0){
          System.out.println("This is bulukulu festival year.");
          ordinary = false;
        }
      }
      if(ordinary == true){
        System.out.println("This is an ordinary year.");
      }
    }
  }
}

Project Euler Problem 13

Solution

import java.util.*;
import java.math.*;

public class problem13{
  public static void main(String[] args){
    Scanner input = new Scanner(System.in);
    BigInteger sum, a;
    sum = BigInteger.valueOf(0);
    for(int i=0;i<100;i++){
      a = input.nextBigInteger();
      sum = sum.add(a);
    }
    System.out.println(sum);    //take the first 10 digits manually
  }
}

Project Euler Problem 15

Solution

import java.math.*;

public class Problem15{
  public static void main(String[] args){
    BigInteger n=BigInteger.valueOf(1);
    BigInteger ans=BigInteger.valueOf(1);
    for(int i=0;i<40;i++){
        ans=ans.multiply(n);
        n= n.add(BigInteger.valueOf(1));
    }
    n=BigInteger.valueOf(1);
    BigInteger ans2=BigInteger.valueOf(1);
    for(int i=0;i<20;i++){
        ans2=ans2.multiply(n);
        n= n.add(BigInteger.valueOf(1));
    }
    System.out.println(ans.divide(ans2.multiply(ans2)));
  }
}

Project Euler Problem 16

Solution

import java.math.*;

public class Problem16{
  public static void main(String[] args){
    //BigInteger n=BigInteger.valueOf(1000);
    BigInteger two = BigInteger.valueOf(2);
    BigInteger ans = two.pow(1000);
    String s = ""+ans;
    System.out.println(s);
    int sum=0;
    for(int i=0;i<s.length();i++){
      sum+=s.charAt(i)-'0';
    }
    System.out.println(sum);
  }
}

Thank you. Happy coding.

 
 
 
 
  • Vote: I like it  
  • +1
  • Vote: I do not like it  

»
3 months ago, # |
Rev. 4   Vote: I like it +2 Vote: I do not like it

Python version

problem 1 :

a,b = map(int,input().split())
print((a**b)-(b**a))

problem 2 :

import bisect
array = [1,2]
for i in range(2,500) :
    array.append(array[i-1]+array[i-2])

while True :
    a,b = map(int,input().split())
    if a==0 and b==0 :
        break
    ans = bisect.bisect_right(array,b) - bisect.bisect_left(array,a)
    print(ans)
  • »
    »
    3 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    This is even faster than my Java code , your one took 124ms while mine took 140ms. Thanks for sharing.