結果

問題 No.2758 RDQ
ユーザー aketijyuuzouaketijyuuzou
提出日時 2024-10-10 20:31:54
言語 C#
(.NET 8.0.203)
結果
AC  
実行時間 909 ms / 2,000 ms
コード長 5,214 bytes
コンパイル時間 7,979 ms
コンパイル使用メモリ 166,168 KB
実行使用メモリ 189,568 KB
最終ジャッジ日時 2024-10-10 20:32:16
合計ジャッジ時間 21,403 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 463 ms
79,360 KB
testcase_01 AC 80 ms
30,592 KB
testcase_02 AC 61 ms
30,592 KB
testcase_03 AC 61 ms
30,848 KB
testcase_04 AC 62 ms
30,592 KB
testcase_05 AC 63 ms
30,848 KB
testcase_06 AC 870 ms
103,040 KB
testcase_07 AC 904 ms
103,232 KB
testcase_08 AC 909 ms
103,040 KB
testcase_09 AC 890 ms
103,040 KB
testcase_10 AC 877 ms
102,968 KB
testcase_11 AC 621 ms
80,936 KB
testcase_12 AC 590 ms
81,152 KB
testcase_13 AC 623 ms
81,072 KB
testcase_14 AC 593 ms
80,768 KB
testcase_15 AC 611 ms
81,152 KB
testcase_16 AC 596 ms
80,768 KB
testcase_17 AC 591 ms
81,076 KB
testcase_18 AC 595 ms
80,768 KB
testcase_19 AC 598 ms
80,936 KB
testcase_20 AC 617 ms
80,768 KB
testcase_21 AC 62 ms
30,720 KB
testcase_22 AC 62 ms
30,720 KB
testcase_23 AC 61 ms
189,568 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
  復元対象のプロジェクトを決定しています...
  /home/judge/data/code/main.csproj を復元しました (95 ms)。
MSBuild のバージョン 17.9.6+a4ecab324 (.NET)
  main -> /home/judge/data/code/bin/Release/net8.0/main.dll
  main -> /home/judge/data/code/bin/Release/net8.0/publish/

ソースコード

diff #

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

// https://yukicoder.me/problems/no/2758
class Program
{
    static string InputPattern = "InputX";

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

        if (InputPattern == "Input1") {
            WillReturn.Add("5 3");
            WillReturn.Add("2 6 7 19 14");
            WillReturn.Add("1 4 2");
            WillReturn.Add("3 5 4");
            WillReturn.Add("1 5 7");
            //2
            //0
            //2
        }
        else if (InputPattern == "Input2") {
            WillReturn.Add("5 4");
            WillReturn.Add("7 6 5 4 3");
            WillReturn.Add("1 5 2");
            WillReturn.Add("2 3 3");
            WillReturn.Add("2 4 4");
            WillReturn.Add("1 3 5");
            //2
            //1
            //1
            //1
        }
        else if (InputPattern == "Input3") {
            WillReturn.Add("3 1");
            WillReturn.Add("1000 1000 100");
            WillReturn.Add("1 3 10");
            //3
        }
        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();

        // IndのList[Aの約数]なDict
        var IndListDict = new Dictionary<long, List<long>>();

        for (long I = 0; I <= AArr.GetUpperBound(0); I++) {
            long[] YakusuuArr = DeriveYakusuuArr(AArr[I]);
            foreach (long EachYakuu in YakusuuArr) {
                if (IndListDict.ContainsKey(EachYakuu) == false) {
                    IndListDict[EachYakuu] = new List<long>();
                }
                IndListDict[EachYakuu].Add(I);
            }
        }

        long[] wkArr = { };
        Action<string> SplitAct = pStr =>
            wkArr = pStr.Split(' ').Select(pX => long.Parse(pX)).ToArray();

        var sb = new System.Text.StringBuilder();
        foreach (string EachStr in InputList.Skip(2)) {
            SplitAct(EachStr);
            long RangeSta = wkArr[0] - 1;
            long RangeEnd = wkArr[1] - 1;
            long K = wkArr[2];
            if (IndListDict.ContainsKey(K) == false) {
                sb.Append(0);
            }
            else {
                long Answer = GetListRangeValueCnt.GetCnt(IndListDict[K], RangeSta, RangeEnd);
                sb.Append(Answer);
            }
            sb.AppendLine();
        }
        Console.Write(sb.ToString());
    }

    // 約数を列挙する
    static long[] DeriveYakusuuArr(long pTarget)
    {
        var YakusuuSet = new HashSet<long>();
        for (long I = 1; I * I <= pTarget; I++) {
            if (pTarget % I == 0) {
                YakusuuSet.Add(I);
                YakusuuSet.Add(pTarget / I);
            }
        }
        long[] YakusuuArr = YakusuuSet.ToArray();
        Array.Sort(YakusuuArr);
        return YakusuuArr;
    }
}

#region GetListRangeValueCnt
// 昇順にソートされたList、Min,Maxを引数とし、
// Min以上Max以下な値の個数を返す
internal class GetListRangeValueCnt
{
    static internal long GetCnt(List<long> pSortedList, long pMinVal, long pMaxVal)
    {
        if (pMinVal > pMaxVal) {
            throw new Exception("pMinVal > pMaxVal");
        }

        int ResultInd1 = ExecNibunhou_LowerBound(pMinVal, pSortedList);
        int ResultInd2 = ExecNibunhou_LowerOrEqual_Max(pMaxVal, pSortedList);

        if (ResultInd1 == -1 || ResultInd2 == -1) return 0;
        return ResultInd2 - ResultInd1 + 1;
    }

    // 二分法で、Val以上で最小の値を持つ、添字を返す
    static int ExecNibunhou_LowerBound(long pVal, List<long> pList)
    {
        if (pList.Count == 0) return -1;

        // 最後の要素がVal未満の特殊ケース
        if (pVal > pList[pList.Count - 1]) {
            return -1;
        }
        // 最初の要素がVal以上の特殊ケース
        if (pVal <= pList[0]) {
            return 0;
        }

        int L = 0;
        int R = pList.Count - 1;

        while (L + 1 < R) {
            int Mid = (L + R) / 2;

            if (pList[Mid] >= pVal) {
                R = Mid;
            }
            else {
                L = Mid;
            }
        }
        return R;
    }

    // 二分法で、Val以下で最大の値を持つ、添字を返す
    static int ExecNibunhou_LowerOrEqual_Max(long pVal, List<long> pList)
    {
        if (pList.Count == 0) return -1;

        // 最後の要素がVal以下の特殊ケース
        if (pVal >= pList[pList.Count - 1]) {
            return pList.Count - 1;
        }
        // 最初の要素がVal超えの特殊ケース
        if (pVal < pList[0]) {
            return -1;
        }

        int L = 0;
        int R = pList.Count - 1;

        while (L + 1 < R) {
            int Mid = (L + R) / 2;

            if (pList[Mid] <= pVal) {
                L = Mid;
            }
            else {
                R = Mid;
            }
        }
        return L;
    }
}
#endregion
0