forked from NemrudDemir/HackerRank
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.cs
More file actions
86 lines (70 loc) · 2.07 KB
/
Solution.cs
File metadata and controls
86 lines (70 loc) · 2.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization;
using System.Text.RegularExpressions;
using System.Text;
using System;
class Result
{
public static int getTotalX(List<int> a, List<int> b)
{
//TODO improve time complexity!
a.Sort();
b.Sort();
if(a.Last() > b.First())
return 0;
//calculate lcm of all elements in a
int lcm = a.First();
for(int i = 1; i<a.Count; i++)
lcm = LCM(lcm, a[i]);
//calculate gcd of all elements in b
int gcd = b.First();
for(int i = 1; i<b.Count; i++)
gcd = GCD(gcd, b[i]);
var result = 0;
var lcmR = lcm;
while(lcmR <= b.First()) {
if(gcd%lcmR == 0)
result++;
lcmR += lcm;
}
return result;
}
static int GCD(int a, int b)
{
while (b != 0)
{
int temp = b;
b = a % b;
a = temp;
}
return a;
}
static int LCM(int a, int b)
{
return (a / GCD(a, b)) * b;
}
}
class Solution
{
public static void Main(string[] args)
{
TextWriter textWriter = new StreamWriter(@System.Environment.GetEnvironmentVariable("OUTPUT_PATH"), true);
string[] firstMultipleInput = Console.ReadLine().TrimEnd().Split(' ');
int n = Convert.ToInt32(firstMultipleInput[0]);
int m = Convert.ToInt32(firstMultipleInput[1]);
List<int> arr = Console.ReadLine().TrimEnd().Split(' ').ToList().Select(arrTemp => Convert.ToInt32(arrTemp)).ToList();
List<int> brr = Console.ReadLine().TrimEnd().Split(' ').ToList().Select(brrTemp => Convert.ToInt32(brrTemp)).ToList();
int total = Result.getTotalX(arr, brr);
textWriter.WriteLine(total);
textWriter.Flush();
textWriter.Close();
}
}