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

典型90問 055 Select 5(★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("6 7 1");
            WillReturn.Add("1 2 3 4 5 6");
            //1
        }
        else if (InputPattern == "Input2") {
            WillReturn.Add("10 1 0");
            WillReturn.Add("0 0 0 0 0 0 0 0 0 0");
            //252
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

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

        long[] wkArr = InputList[0].Split(' ').Select(pX => long.Parse(pX)).ToArray();
        long P = wkArr[1];
        long Q = wkArr[2];
        long[] AArr = InputList[1].Split(' ').Select(pX => long.Parse(pX)).ToArray();

        int UB = AArr.GetUpperBound(0);
        long Answer = 0;
        for (int I = 0; I <= UB; I++) {
            for (int J = I + 1; J <= UB; J++) {
                for (int K = J + 1; K <= UB; K++) {
                    for (int L = K + 1; L <= UB; L++) {
                        for (int M = L + 1; M <= UB; M++) {
                            long Mod = AArr[I] * AArr[J];
                            Mod %= P; Mod *= AArr[K];
                            Mod %= P; Mod *= AArr[L];
                            Mod %= P; Mod *= AArr[M];
                            Mod %= P;

                            if (Mod == Q) {
                                Answer++;
                            }
                        }
                    }
                }
            }
        }
        Console.WriteLine(Answer);
    }
}


解説

ナイーブに5重ループしてます。