using System; using System.Collections.Generic; using System.Linq; using static System.Math; namespace ConsoleApplication3 { public static class Extensions { public static IEnumerable Do(this IEnumerable source, Action process) { foreach (var elem in source) { process(elem); yield return elem; } } public static IEnumerable Repeat(this IEnumerable source, Func condition, Action sideEffect) { foreach (var i in source) { while (condition(i)) { sideEffect(i); yield return i; } } } } class Program { private static readonly List PrimeNumbers = new List {2}; public static IEnumerable GetPrimeSequence() => PrimeNumbers.Concat(Enumerable.Range((PrimeNumbers.Max() - 1)/2 + 1, 1000000000) .Select(x => x*2 + 1) .Where(c => PrimeNumbers.TakeWhile(p => p <= Sqrt(c)).All(p => c%p != 0)) .Do(x => PrimeNumbers.Add(x))); public static IEnumerable Factorization(long value) { var tmp = value; return GetPrimeSequence().TakeWhile(_ => tmp != 1).Repeat(p => tmp%p == 0, p => tmp /= p); } public static long LeastCommonMultiple(params long[] values) => values.Select( v => Factorization(v).ToLookup(x => x).Select(x => new {prime = x.Key, value = (long) Pow(x.Key, x.Count())})) .SelectMany(x => x) .ToLookup(x => x.prime, x => x.value) .Select(x => x.Max()) .Aggregate(1L, (a, d) => a*d); static void Main() { var n = long.Parse(Console.ReadLine()); var values = Console.ReadLine().Split(' ').Select(x => long.Parse(x)).ToArray(); var ab = LeastCommonMultiple(values[0], values[1]); var bc = LeastCommonMultiple(values[1], values[2]); var ca = LeastCommonMultiple(values[2], values[0]); var abc = LeastCommonMultiple(values); var ret = values.Select(x => n/x).Sum(); ret -= n/ab; ret -= n/bc; ret -= n/ca; ret += n/abc; Console.WriteLine(ret); } } }