AtCoderのARC    次のARCの問題へ    前のARCの問題へ

ARC166-B Make Multiples


問題へのリンク


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("3 3 4 5");
            WillReturn.Add("8 9 11");
            //2
        }
        else if (InputPattern == "Input2") {
            WillReturn.Add("3 3 4 5");
            WillReturn.Add("14 11 59");
            //1
        }
        else if (InputPattern == "Input3") {
            WillReturn.Add("6 10 20 30");
            WillReturn.Add("8 17 5 28 39 13");
            //3
        }
        else if (InputPattern == "Input4") {
            WillReturn.Add("1 999997 999998 999999");
            WillReturn.Add("123456789123456789");
            //876537210887543205
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

    struct SeniDef
    {
        internal long LCM;
        internal long HasBit;
    }
    static List<SeniDef> mSeniList = new List<SeniDef>();

    static void Main()
    {
        List<string> InputList = GetInputList();
        long[] wkArr = InputList[0].Split(' ').Select(pX => long.Parse(pX)).ToArray();
        long A = wkArr[1];
        long B = wkArr[2];
        long C = wkArr[3];
        long[] ValArr = InputList[1].Split(' ').Select(pX => long.Parse(pX)).ToArray();

        Action<long, long[]> SeniAddAct = (pBit, pArr) =>
        {
            SeniDef WillAdd;
            WillAdd.LCM = DeriveLCM(pArr, long.MaxValue);
            WillAdd.HasBit = pBit;
            mSeniList.Add(WillAdd);
        };
        SeniAddAct(Convert.ToInt64("100", 2), new long[] { A });
        SeniAddAct(Convert.ToInt64("010", 2), new long[] { B });
        SeniAddAct(Convert.ToInt64("001", 2), new long[] { C });
        SeniAddAct(Convert.ToInt64("110", 2), new long[] { A, B });
        SeniAddAct(Convert.ToInt64("101", 2), new long[] { A, C });
        SeniAddAct(Convert.ToInt64("011", 2), new long[] { B, C });
        SeniAddAct(Convert.ToInt64("111", 2), new long[] { A, B, C });

        long AllBitOn = (1 << 3) - 1;

        // 最小コスト[BitSet]なDP表
        long?[] DPArr = new long?[AllBitOn + 1];
        DPArr[0] = 0;

        foreach (long EachVal in ValArr) {
            for (long I = AllBitOn - 1; 0 <= I; I--) {
                if (DPArr[I].HasValue == false) continue;

                foreach (SeniDef EachSeni in mSeniList) {
                    long NewI = I | EachSeni.HasBit;
                    long NewCost = DPArr[I].Value + DeriveCost(EachVal, EachSeni.LCM);

                    if (DPArr[NewI].HasValue) {
                        if (DPArr[NewI].Value <= NewCost) {
                            continue;
                        }
                    }
                    DPArr[NewI] = NewCost;
                }
            }
        }
        Console.WriteLine(DPArr[AllBitOn]);
    }

    // 引数1を、引数2の倍数にするコストを返す
    static long DeriveCost(long p1, long p2)
    {
        if (p1 < p2) return p2 - p1;
        if (p1 % p2 == 0) return 0;
        long Mod = p1 % p2;
        return p2 - Mod;
    }

    // 列挙を引数として、最小公倍数を返す
    static long DeriveLCM(IEnumerable<long> pEnum, long pLimit)
    {
        long LCM = pEnum.First();
        foreach (long EachLong in pEnum) {
            LCM = DeriveLCM2(LCM, EachLong);
            if (LCM > pLimit) {
                break;
            }
        }
        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;
        }
    }
}


解説

最小コスト[Aの倍数の登場有無,Bの倍数の登場有無,Cの倍数の登場有無]
でBitDPしてます。