E8本(数学)    次のE8本(数学)の問題へ    前のE8本(数学)の問題へ

E8本(数学) 068 Number of Multiples 2


問題へのリンク


C#のソース

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 3");
            WillReturn.Add("2 3 5");
            //74
        }
        else if (InputPattern == "Input2") {
            WillReturn.Add("100 3");
            WillReturn.Add("2 4 6");
            //50
        }
        else if (InputPattern == "Input3") {
            WillReturn.Add("10000000000000 10");
            WillReturn.Add("13 17 19 23 29 31 37 41 43 47");
            //3324865541894
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

    static long mN;
    static long[] mVArr;

    static void Main()
    {
        List<string> InputList = GetInputList();
        long[] wkArr = InputList[0].Split(' ').Select(pX => long.Parse(pX)).ToArray();
        mN = wkArr[0];

        mVArr = InputList[1].Split(' ').Select(pX => long.Parse(pX)).ToArray();
        List<long[]> DFSResult = ExecDFS();

        long Answer = 0;

        foreach (long[] EachValList in DFSResult) {
            long Length = EachValList.Length;
            long LCM = DeriveLCM(EachValList);

            long CurrCnt = mN / LCM;
            if (Length % 2 == 1) {
                Answer += CurrCnt;
            }
            else {
                Answer -= CurrCnt;
            }
        }
        Console.WriteLine(Answer);
    }

    struct JyoutaiDef
    {
        internal long CurrInd;
        internal List<long> SelectedValList;
    }

    static List<long[]> ExecDFS()
    {
        var WillReturn = new List<long[]>();

        var Stk = new Stack<JyoutaiDef>();
        JyoutaiDef WillPush;
        WillPush.CurrInd = 0;
        WillPush.SelectedValList = new List<long>();
        Stk.Push(WillPush);

        while (Stk.Count > 0) {
            JyoutaiDef Popped = Stk.Pop();

            if (Popped.SelectedValList.Count > 0) {
                WillReturn.Add(Popped.SelectedValList.ToArray());
            }

            for (long I = Popped.CurrInd; I <= mVArr.GetUpperBound(0); I++) {
                WillPush.CurrInd = I + 1;
                WillPush.SelectedValList = new List<long>(Popped.SelectedValList);
                WillPush.SelectedValList.Add(mVArr[I]);
                Stk.Push(WillPush);
            }
        }
        return WillReturn;
    }

    // 列挙を引数として、最小公倍数を返す
    static long DeriveLCM(IEnumerable<long> pEnum)
    {
        long LCM = pEnum.First();
        foreach (long EachLong in pEnum) {
            LCM = DeriveLCM2(LCM, EachLong);
        }
        return LCM;
    }

    // 2つの数のLCMを求める
    static long DeriveLCM2(long p1, long p2)
    {
        long GCD = DeriveGCD(p1, p2);
        return (p1 / GCD) * p2;
    }

    // ユークリッドの互除法で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;
        }
    }
}


解説

包除原理を使ってます。