Header Ads Widget

Takeoff

 Problem:-

There are three planes AB and C. Plane A will takeoff on every pth day i.e. p2p3p and so on. Plane B will takeoff on every qth day and plane C will takeoff on every rth day. There is only one runway and the takeoff timing is same for each of the three planes on each day. Your task is to find out the maximum number of flights that will successfully takeoff in the period of N days.

Note: If there is collision between the flights no flight will take off on that day.

Input Format
The first line of the input contains a single integer T, the number of test cases.
Then T lines follow each containing four space-separated integers Npq and r as denoted in the statement.

Output Format
For each test case print a single integer representing the maximum number of flights that will successfully takeoff in the period of N days.

Constraints
1T10
1N,p,q,r105

Sample Input
2
10 2 3 4
10 2 2 4
Sample Output
4
0
Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

Sample test case 1:
Note that on days 2 and 10 plane A can takeoff, on days 3 and 9 plane B can takeoff.

Sample test case 2:
Note that there is no day on which there is no collision.

Code:-

#include<stdio.h>
int main()
{
  int t;
  long int n,p,q,r,sum,i;
  scanf("%d",&t);
  while(t--)
  {
    sum=0;
    scanf("%ld%ld%ld%ld",&n,&p,&q,&r);
    for(i=1;i<=n;i++)
    {
      if(i%p==0)
      {
        if(i%q==0||i%r==0)
         continue;
        else
         sum++;
      }
      if(i%q==0)
      {
        if(i%p==0||i%r==0)
         continue;
        else
         sum++;
      }
      if(i%r==0)
      {
        if(i%p==0||i%q==0)
         continue;
        else
         sum++;
      }
      
    }
    printf("%ld\n",sum);
  }
  return 0;
}



Post a Comment

0 Comments