Pages

Showing posts with label Modulo. Show all posts
Showing posts with label Modulo. 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");
        }
    }
}

Saturday, 12 January 2013

UVA - 10489 - Boxes of Chocolates

#include<stdio.h>

int main()
{

   long n,friends,boxes,k,product,sum,temp;
   scanf("%ld",&n);
   while(n--){
      scanf("%ld %ld",&friends,&boxes);
      sum=0;
      while(boxes--){
         product=1;
         scanf("%ld",&k);
         while(k--){
            scanf("%ld",&temp);
            product=(product*temp)%friends;
         }
         sum=(sum+product)%friends;
      }
      printf("%ld\n",sum);
   }
   return 0;
}

UVA - 10515 - Powers Et Al.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
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();
        int[]mods={1, 1, 4, 4, 2, 1, 1, 4, 4, 2};
        while(true){
            StringTokenizer st=new StringTokenizer(br.readLine());
            String str=st.nextToken();
            int m=Integer.parseInt(str.charAt(str.length()-1)+"");
            str=st.nextToken();
            int n;
            if(str.length()>1){
                n=Integer.parseInt(str.charAt(str.length()-2)+""+str.charAt(str.length()-1)+"");
            }else{
                n=Integer.parseInt(str.charAt(str.length()-1)+"");
            }
            if(m==0 && n==0 &&str.length()==1){
                break;
            }
            if(m==0 ||m==1 ||m==5 ||m==6){
                sb.append(m).append("\n");
            }else if(n==0){
                if(str.length()==1)
                    sb.append("1\n");
                else{
                    sb.append(modP(m, 4,mods)).append("\n");
                }
            }else{
                sb.append(modP(m, n,mods)).append("\n");
            }
        }
        System.out.print(sb);
    }
   
    static int modP(int m,int n,int[]mods){
        if(n%mods[m]>0)
            n= n%mods[m];
        else
            n=mods[m];
        int res=1;
        for (int i = 0; i < n; i++) {
            res*=m;
        }
        return res%10;
    }
}

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);
    }
}