Time spent here:

Thursday, 2 June 2016

WEEK OF CODE-18 -Ghosts Problem

Do you believe in ghosts? Citizens of Byteland do. They want to estimate the population size of ghosts in their country.

There are  towns in Byteland. Every town consists of  streets. Each street has  houses. Finally, there are apartments in each house. All the towns, streets, houses and apartments are numbered starting from . So, every apartment has a corresponding address described by  numbers.

A ghost lives in a particular apartment only if all the conditions below are true:

The difference between the town number and the street number is divisible by .
The sum of the street number and the house number is divisible by .
The product of the town number and the house number is divisible by .
The greatest common divisor of the town number and the apartment number is .
You are given the numbers , , , and . How many ghosts live in Byteland?

Input Format

The first line contains  space-separated integers: , , , .

Constraints


Output Format

Output the answer to the problem on the first line.

Sample Input

4 4 4 4
Sample Output

8
Explanation

Here are the addresses of all the ghosts:

1 1 4 1
1 1 4 2
1 1 4 3
1 1 4 4
4 1 4 1
4 1 4 3
4 4 1 1
4 4 1 3

CODE:

import java.io.*;
import java.util.*;

public class Solution {

    public static void main(String[] args) {
     
      Scanner sc=new Scanner(System.in);
       int a,b,c,d,i,j,k,l,m,count=0;
       a=sc.nextInt();
       b=sc.nextInt();
        c=sc.nextInt();
        d=sc.nextInt();
        for(i=1;i<=a;i++)
         {
             for(j=1;j<=b;j++)
                 {
                  for(k=1;k<=c;k++)
                      {
                       for(l=1;l<=d;l++)
                           {
                               if((i-j)%3==0 && (j+k)%5==0 && (i*k)%4==0)
                                {
                                 
                                           int r=0, a1, b1;

        a1 = (i > l) ? i : l;

        b1 = (i < l) ? i: l;


        r = b1;

        while(a1 % b1 != 0)

        {

            r = a1 % b1;

            a1 = b1;

            b1 = r;

        }
                                   if(r==1)
                                     count++;
                               }
                       }
                  }
             }
        }  
       System.out.println(count);
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
   
    }

}

No comments:

Post a Comment