典型90問    次の典型90問へ    前の典型90問へ

典型90問 046 I Love 46(★3)


問題へのリンク


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");
            WillReturn.Add("10 13 93");
            WillReturn.Add("5 27 35");
            WillReturn.Add("55 28 52");
            //3
        }
        else if (InputPattern == "Input2") {
            WillReturn.Add("3");
            WillReturn.Add("10 56 102");
            WillReturn.Add("16 62 108");
            WillReturn.Add("20 66 112");
            //27
        }
        else if (InputPattern == "Input3") {
            WillReturn.Add("20");
            WillReturn.Add("238 395 46 238 264 114 354 52 324 14 472 64 307 280 209 24 165 194 179 248");
            WillReturn.Add("270 83 377 332 173 21 362 75 66 342 229 117 124 481 48 235 376 13 420 74");
            WillReturn.Add("175 427 76 278 486 169 311 47 348 225 41 482 355 356 263 95 170 156 340 289");
            //183
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

    static void Main()
    {
        List<string> InputList = GetInputList();

        long[] AArr = InputList[1].Split(' ').Select(pX => long.Parse(pX)).ToArray();
        long[] BArr = InputList[2].Split(' ').Select(pX => long.Parse(pX)).ToArray();
        long[] CArr = InputList[3].Split(' ').Select(pX => long.Parse(pX)).ToArray();

        Dictionary<long, long> CntDict1 = DeriveCntDict(AArr);
        Dictionary<long, long> CntDict2 = DeriveCntDict(BArr);
        Dictionary<long, long> CntDict3 = DeriveCntDict(CArr);

        long Answer = 0;
        foreach (var EachPair1 in CntDict1) {
            foreach (var EachPair2 in CntDict2) {
                foreach (var EachPair3 in CntDict3) {
                    long SumVal = EachPair1.Key + EachPair2.Key + EachPair3.Key;
                    if (SumVal % 46 == 0) {
                        Answer += EachPair1.Value * EachPair2.Value * EachPair3.Value;
                    }
                }
            }
        }
        Console.WriteLine(Answer);
    }

    // 件数[46で割った余り]なDictを返す
    static Dictionary<long, long> DeriveCntDict(long[] pArr)
    {
        var WillReturn = new Dictionary<long, long>();
        foreach (long EachVal in pArr) {
            long Mod = EachVal % 46;
            if (WillReturn.ContainsKey(Mod) == false) {
                WillReturn[Mod] = 0;
            }
            WillReturn[Mod]++;
        }
        return WillReturn;
    }
}


解説

最初に、件数[46で割った余り]な度数分布表を求めてます。