結果

問題 No.316 もっと刺激的なFizzBuzzをください
ユーザー 明智重蔵明智重蔵
提出日時 2015-12-26 18:59:35
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 26 ms / 1,000 ms
コード長 2,724 bytes
コンパイル時間 852 ms
コンパイル使用メモリ 115,672 KB
実行使用メモリ 27,516 KB
最終ジャッジ日時 2024-05-01 09:00:28
合計ジャッジ時間 2,530 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 23 ms
25,224 KB
testcase_01 AC 25 ms
27,036 KB
testcase_02 AC 26 ms
25,008 KB
testcase_03 AC 26 ms
25,224 KB
testcase_04 AC 25 ms
25,264 KB
testcase_05 AC 24 ms
25,228 KB
testcase_06 AC 25 ms
25,392 KB
testcase_07 AC 25 ms
27,260 KB
testcase_08 AC 24 ms
23,180 KB
testcase_09 AC 24 ms
25,264 KB
testcase_10 AC 24 ms
25,256 KB
testcase_11 AC 24 ms
25,252 KB
testcase_12 AC 25 ms
25,512 KB
testcase_13 AC 26 ms
27,304 KB
testcase_14 AC 25 ms
25,512 KB
testcase_15 AC 26 ms
25,268 KB
testcase_16 AC 25 ms
27,436 KB
testcase_17 AC 26 ms
27,516 KB
testcase_18 AC 25 ms
25,356 KB
testcase_19 AC 25 ms
23,352 KB
testcase_20 AC 25 ms
22,964 KB
testcase_21 AC 24 ms
25,096 KB
testcase_22 AC 26 ms
25,380 KB
testcase_23 AC 25 ms
25,012 KB
testcase_24 AC 24 ms
25,092 KB
testcase_25 AC 25 ms
27,308 KB
testcase_26 AC 24 ms
25,136 KB
testcase_27 AC 24 ms
25,356 KB
testcase_28 AC 25 ms
25,256 KB
testcase_29 AC 26 ms
27,312 KB
testcase_30 AC 24 ms
27,180 KB
testcase_31 AC 24 ms
27,296 KB
testcase_32 AC 23 ms
25,356 KB
testcase_33 AC 23 ms
25,264 KB
testcase_34 AC 24 ms
27,308 KB
testcase_35 AC 25 ms
27,440 KB
testcase_36 AC 24 ms
25,100 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc)
Copyright (C) Microsoft Corporation. All rights reserved.

ソースコード

diff #

using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
    static string InputPattern = "InputX";

    static List<string> GetInputList()
    {
        var WillReturn = new List<string>();

        if (InputPattern == "Input1") {
            WillReturn.Add("100");
            WillReturn.Add("2 3 5");
            //74
            //1以上100以下の範囲内には,
            //2または3または5の倍数となる数字の個数が合計で74個あります
        }
        else if (InputPattern == "Input2") {
            WillReturn.Add("100");
            WillReturn.Add("2 5 6");
            //60
        }
        else if (InputPattern == "Input3") {
            WillReturn.Add("346346");
            WillReturn.Add("25 575 2525");
            //13853
        }
        else if (InputPattern == "Input4") {
            WillReturn.Add("83359640");
            WillReturn.Add("3304 9805 9945");
            //42104
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

    //No.316 もっと刺激的なFizzBuzzをください
    static void Main()
    {
        List<string> InputList = GetInputList();
        long N = long.Parse(InputList[0]);
        long[] wkArr = InputList[1].Split(' ').Select(X => long.Parse(X)).ToArray();
        long a = wkArr[0];
        long b = wkArr[1];
        long c = wkArr[2];

        //包除原理 N(A∪B∪C)=N(A)+N(B)+N(C)-N(A∩B)-N(A∩C)-N(B∩C)+N(A∩B∩C)で解く
        long LCMab = a * b / DeriveGCD(a, b);
        long LCMac = a * c / DeriveGCD(a, c);
        long LCMbc = b * c / DeriveGCD(b, c);
        long LCMabc = LCMab * c / DeriveGCD(LCMab, c);

        //Console.WriteLine("{0}と{1}とのLCM={2}", a, b, LCMab);
        //Console.WriteLine("{0}と{1}とのLCM={2}", a, c, LCMac);
        //Console.WriteLine("{0}と{1}とのLCM={2}", b, c, LCMbc);
        //Console.WriteLine("{0}と{1}と{2}のLCM={3}", a,b, c, LCMabc);

        long Na = N / a;
        long Nb = N / b;
        long Nc = N / c;
        long Nab = N / LCMab;
        long Nac = N / LCMac;
        long Nbc = N / LCMbc;
        long Nabc = N / LCMabc;

        Console.WriteLine(Na + Nb + Nc - Nab - Nac - Nbc + Nabc);
    }

    //ユークリッドの互除法で2数の最大公約数を求める
    static long DeriveGCD(long pVal1, long pVal2)
    {
        long WarareruKazu = pVal2;
        long WaruKazu = pVal1;

        while (true) {
            long Amari = WarareruKazu % WaruKazu;
            if (Amari == 0) return WaruKazu;
            WarareruKazu = WaruKazu;
            WaruKazu = Amari;
        }
    }
}
0