Pages

Showing posts with label BigInteger. Show all posts
Showing posts with label BigInteger. Show all posts

Friday, 28 June 2013

UVA - 10992 - The Ghost of Programmers

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigInteger;
import java.util.StringTokenizer;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringBuffer sb = new StringBuffer();
        boolean first=true;
        while(true){
            BigInteger bg=new BigInteger(br.readLine());
            if(bg.compareTo(BigInteger.ZERO)==0){
                break;
            }
            if(!first){
                sb.append("\n");
            }
            sb.append(bg).append("\n");
            answer(bg, sb);
            first=false;
        }
        System.out.print(sb);
    }
   
    static void answer(BigInteger bg,StringBuffer sb){
        boolean flag=false;
        if(bg.compareTo(BigInteger.valueOf(2147))==1){
            BigInteger leap=bg.add(BigInteger.ZERO);
            bg=bg.subtract(BigInteger.valueOf(2148));
            if((bg.mod(BigInteger.valueOf(2))).compareTo(BigInteger.ZERO)==0){
                sb.append("Ghost of Tanveer Ahsan!!!\n");
                flag=true;
            }
            if((bg.mod(BigInteger.valueOf(5))).compareTo(BigInteger.ZERO)==0){
                sb.append("Ghost of Shahriar Manzoor!!!\n");
                flag=true;
            }
            if((bg.mod(BigInteger.valueOf(7))).compareTo(BigInteger.ZERO)==0){
                sb.append("Ghost of Adrian Kugel!!!\n");
                flag=true;
            }
            if((bg.mod(BigInteger.valueOf(11))).compareTo(BigInteger.ZERO)==0){
                sb.append("Ghost of Anton Maydell!!!\n");
                flag=true;
            }
            if((bg.mod(BigInteger.valueOf(15))).compareTo(BigInteger.ZERO)==0){
                sb.append("Ghost of Derek Kisman!!!\n");
                flag=true;
            }
            if((bg.mod(BigInteger.valueOf(20))).compareTo(BigInteger.ZERO)==0){
                sb.append("Ghost of Rezaul Alam Chowdhury!!!\n");
                flag=true;
            }
            if((bg.mod(BigInteger.valueOf(28))).compareTo(BigInteger.ZERO)==0){
                sb.append("Ghost of Jimmy Mardell!!!\n");
                flag=true;
            }
            if((bg.mod(BigInteger.valueOf(36))).compareTo(BigInteger.ZERO)==0){
                sb.append("Ghost of Monirul Hasan!!!\n");
                flag=true;
            }
            if((bg.mod(BigInteger.valueOf(4))).compareTo(BigInteger.ZERO)==0){
                if((leap.mod(BigInteger.valueOf(400))).compareTo(BigInteger.ZERO)==0){
                    sb.append("Ghost of K. M. Iftekhar!!!\n");
                    flag=true;
                }
                if(!((leap.mod(BigInteger.valueOf(100))).compareTo(BigInteger.ZERO)==0)){
                    sb.append("Ghost of K. M. Iftekhar!!!\n");
                    flag=true;
                }
            }
        }
        if(!flag){
            sb.append("No ghost will come in this year\n");
        }
    }
}

Monday, 24 June 2013

UVA - 10433 - Automorphic Numbers

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigInteger;

public class Main{

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringBuilder sb = new StringBuilder();  
        String m="";
        while((m=br.readLine())!=null){
            int val=checkBg(m);
            if(val==0){
                sb.append("Not an Automorphic number.\n");
            }else{
                sb.append(String.format("Automorphic number of %d-digit.\n", val));
            }
        }
        System.out.print(sb);
    }
   
    static int checkBg(String m){
       
        BigInteger bg=new BigInteger(m);
        int n=m.length();
        String str=bg.modPow(BigInteger.valueOf(2), BigInteger.TEN.pow(n)).toString();
        if(str.equals("0") ||str.equals("1"))
            return 0;
        while(str.length()!=n){
           str="0"+str;
        }
        for(int i=0;i<n;i++){
            if(str.charAt(i)!=m.charAt(i))
                return 0;
        }
        return n;
    }
}
  

Wednesday, 12 December 2012

UVA - 10070 - Leap Year or Not Leap Year

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigInteger;

public class Main {

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringBuffer sb = new StringBuffer("");
        String m="";
        boolean first=true;
        while((m=br.readLine())!=null){
            if(!first){
                sb.append("\n");
            }
            boolean leap=false,bul=false,hul=false;
            BigInteger x = new BigInteger(m);
            if(x.mod(BigInteger.valueOf(4)).compareTo(BigInteger.ZERO)==0){
                if(x.mod(BigInteger.valueOf(100)).compareTo(BigInteger.ZERO)==0){
                    if(x.mod(BigInteger.valueOf(400)).compareTo(BigInteger.ZERO)==0){
                        leap=true;
                    }
                }else{
                    leap=true;
                }
            }
            if(x.mod(BigInteger.valueOf(15)).compareTo(BigInteger.ZERO)==0){
                hul=true;
            }
            if(x.mod(BigInteger.valueOf(55)).compareTo(BigInteger.ZERO)==0){
                bul=true;
            }
            if(hul||leap){
                if(leap){
                    sb.append("This is leap year.\n");
                }
                if(hul){
                    sb.append("This is huluculu festival year.\n");
                }
                if(leap&&bul){
                    sb.append("This is bulukulu festival year.\n");
                }
            }else{
                sb.append("This is an ordinary year.\n");
            }
            first=false;
        }
        System.out.print(sb);
    }
}