結果
| 問題 |
No.316 もっと刺激的なFizzBuzzをください
|
| コンテスト | |
| ユーザー |
NetSeed
|
| 提出日時 | 2015-12-31 22:10:50 |
| 言語 | C#(csc) (csc 3.9.0) |
| 結果 |
AC
|
| 実行時間 | 48 ms / 1,000 ms |
| コード長 | 2,167 bytes |
| コンパイル時間 | 1,160 ms |
| コンパイル使用メモリ | 117,604 KB |
| 実行使用メモリ | 28,280 KB |
| 最終ジャッジ日時 | 2024-11-21 12:14:50 |
| 合計ジャッジ時間 | 3,968 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| 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;
namespace ConsoleApplication3
{
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 elem in source)
{
while (condition(elem))
{
sideEffect(elem);
yield return elem;
}
}
}
}
class Program
{
private static readonly List<int> PrimeNumbers = new List<int> {2, 3};
public static IEnumerable<int> GetPrimeSequence()
=> PrimeNumbers.Concat(Enumerable.Range((PrimeNumbers.Max() - 1)/2 + 1, 1000000000)
.Select(x => x*2 + 1)
.Where(x => PrimeNumbers.TakeWhile(p => Math.Sqrt(x) >= p).All(p => x%p != 0))
.Do(x => PrimeNumbers.Add(x)));
public static IEnumerable<int> Factorization(long value)
{
var tmp = value;
return GetPrimeSequence().TakeWhile(_ => tmp != 1).Repeat(x => tmp%x == 0, x => tmp/=x);
}
public static long LeastCommonMultiple(long lParam, long rParam)
{
var lFact = Factorization(lParam).ToLookup(x => x).Select(x => new {prime = x.Key, value = (int)Math.Pow(x.Key, x.Count()) });
var rFact =
Factorization(rParam).ToLookup(x => x).Select(x => new {prime = x.Key, value = (int) Math.Pow(x.Key, x.Count())});
var ret = lFact.Concat(rFact).ToLookup(x => x.prime,x=>x.value);
var hoge = ret.Select(x => x.Max());
return hoge.Aggregate(1L, (accum, i) => accum *= i);
}
static void Main(string[] args)
{
var n = int.Parse(Console.ReadLine());
var input = Console.ReadLine().Split(' ').Select(x => int.Parse(x)).ToArray();
var ab = LeastCommonMultiple(input[0], input[1]);
var bc = LeastCommonMultiple(input[1], input[2]);
var ca = LeastCommonMultiple(input[2], input[0]);
var med = LeastCommonMultiple(ab, input[2]);
var accum = input.Select(x => (long) n/x).Sum();
accum -= n/ab;
accum -= n/bc;
accum -= n/ca;
accum += n/med;
Console.WriteLine(accum);
}
}
}
NetSeed