Tuesday, 19 September 2017

Task for Alexey Problem

Task For Alexey

Alexey is trying to develop a program for a very simple microcontroller. It takes readings from various sensors over time, and these readings must happen at specific regular times. Unfortunately, if two of these readings occur at the same time, the microcontroller freezes and must be reset.

There are N different sensors that read data on a regular basis. For each i from 1 to N, the reading from sensor i will occur every Ai milliseconds with the first reading occurring exactly Ai milliseconds after the microcontroller is powered up. Each reading takes precisely one millisecond on Alexey's microcontroller.

Alexey wants to know when the microcontroller will freeze after he turns it on.

Input

The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows.

The first line contains single integer N denoting the number of sensors.

The second line contains N space-separated integers A1, A2, ..., AN denoting frequency of measurements. Namely, sensor i will be read every Ai milliseconds with the first reading occurring Ai milliseconds after the microcontroller is first turned on.

Output

For each test case, output a single line containing the number of milliseconds until the microcontroller freezes.


Constraints

1 ≤ T ≤ 10
2 ≤ N ≤ 500
1 ≤ Ai ≤ 109


Solution:

#include<stdio.h>

unsigned long long int hcf( unsigned long long int a, unsigned long long int b){
if(b == 0) return a;
else return hcf(b, a%b);
}

int main(void){
int t;
scanf("%d",&t);
while(t--){
 int n;
 scanf("%d",&n);
 int i,j;
 unsigned long long int arr[n],min=999999999999999999999, gcd, lcm;
 for(i=0; i<n; i++){
  scanf("%llu",&arr[i]);
 }
 for(i=0;i<n; i++){
  for(j=i+1; j<n ; j++){
  if( arr[i] > min || arr[j] > min) continue;
  gcd= hcf(arr[i],arr[j]);
  lcm= (arr[i]* arr[j])/gcd;
  if(min >lcm) min=lcm; 
 
  }
 }
 printf("%llu \n",min);



No comments:

Post a Comment