AtCoderの企業コンテスト    前の企業コンテストの問題へ

CodeQUEEN 2025 決勝 C 整理券


問題へのリンク


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 1");
            //4
        }
        else if (InputPattern == "Input2") {
            WillReturn.Add("4 1");
            //8
        }
        else if (InputPattern == "Input3") {
            WillReturn.Add("2025 314");
            //31233731
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

    static long[] GetSplitArr(string pStr)
    {
        return (pStr == "" ? new string[0] : pStr.Split(' ')).Select(pX => long.Parse(pX)).ToArray();
    }

    const long Hou = 998244353;

    static void Main()
    {
        List<string> InputList = GetInputList();
        long[] wkArr = GetSplitArr(InputList[0]);
        long N = wkArr[0];
        long K = wkArr[1];

        // (1 + 椅子の数)が上限
        long ProdLimit = 1 + K;

        long Answer = 1;

        long ProdVal = 0;

        // 降順に配置していく
        for (long I = 1; I <= N; I++) {
            ProdVal++;
            ProdVal = Math.Min(ProdVal, ProdLimit);

            Answer *= ProdVal;
            Answer %= Hou;
        }
        Console.WriteLine(Answer);
    }
}


解説

1 2 3 4 5 6
で椅子が2つあるとして、
降順に既存列に隣接させて配置するとして考えます。

6の配置 □          1通り(□に配置可能)
5の配置 □6□       2通り(□に配置可能)
4の配置 □5□6□    3通り(□に配置可能)
3の配置 □4□5□6   3通り(□に配置可能)
2の配置 □3□4□56  3通り(□に配置可能)
1の配置 □2□3□456 3通り(□に配置可能)

後は、積の法則で総積を求めれば良いです。