結果
問題 |
No.316 もっと刺激的なFizzBuzzをください
|
ユーザー |
![]() |
提出日時 | 2015-12-31 23:09:59 |
言語 | C#(csc) (csc 3.9.0) |
結果 |
AC
|
実行時間 | 47 ms / 1,000 ms |
コード長 | 2,016 bytes |
コンパイル時間 | 1,050 ms |
コンパイル使用メモリ | 118,152 KB |
実行使用メモリ | 28,356 KB |
最終ジャッジ日時 | 2024-11-21 12:15:12 |
合計ジャッジ時間 | 3,753 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 33 |
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc) Copyright (C) Microsoft Corporation. All rights reserved.
ソースコード
using System; using System.Collections.Generic; using System.Linq; using static System.Math; namespace ConsoleApplication3 { public static class Extensions { public static IEnumerable<T> Do<T>(this IEnumerable<T> source, Action<T> process) { foreach (var elem in source) { process(elem); yield return elem; } } public static IEnumerable<T> Repeat<T>(this IEnumerable<T> source, Func<T, bool> condition, Action<T> sideEffect) { foreach (var i in source) { while (condition(i)) { sideEffect(i); yield return i; } } } } class Program { private static readonly List<int> PrimeNumbers = new List<int> {2}; public static IEnumerable<int> 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<int> 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) { return 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 => { return x.Max(); }) .Aggregate(1L, (a, d) => a*d); } static void Main(string[] args) { 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); } } }