AtCoderのABC    次のABCの問題へ    前のABCの問題へ

ABC167-E Colorful Blocks


問題へのリンク


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 2 1");
            //6
        }
        else if (InputPattern == "Input2") {
            WillReturn.Add("100 100 0");
            //73074801
        }
        else if (InputPattern == "Input3") {
            WillReturn.Add("60522 114575 7559");
            //479519525
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

    static long mN;
    static long mM;
    static long mK;
    const long Hou = 998244353;

    static void Main()
    {
        List<string> InputList = GetInputList();
        long[] wkArr = InputList[0].Split(' ').Select(pX => long.Parse(pX)).ToArray();
        mN = wkArr[0];
        mM = wkArr[1];
        mK = wkArr[2];

        long Result1 = Solve();
        Console.WriteLine(Result1);
    }

    static long Solve()
    {
        long Answer = 0;

        var InsChooseMod = new ChooseMod(mN, Hou);

        // 隣接マスの同色数で全探索
        for (long I = 0; I <= mK; I++) {
            long CurrPatternCnt = mM;
            CurrPatternCnt *= DeriveBekijyou(mM - 1, mN - I - 1, Hou);
            CurrPatternCnt %= Hou;

            long ChooseCnt = InsChooseMod.DeriveChoose(mN - 1, I);
            CurrPatternCnt *= ChooseCnt;
            CurrPatternCnt %= Hou;

            Answer += CurrPatternCnt;
            Answer %= Hou;
        }
        return Answer;
    }

    // 繰り返し2乗法で、(NのP乗) Mod Mを求める
    static long DeriveBekijyou(long pN, long pP, long pM)
    {
        long CurrJyousuu = pN % pM;
        long CurrShisuu = 1;
        long WillReturn = 1;

        while (true) {
            // 対象ビットが立っている場合
            if ((pP & CurrShisuu) > 0) {
                WillReturn = (WillReturn * CurrJyousuu) % pM;
            }

            CurrShisuu *= 2;
            if (CurrShisuu > pP) return WillReturn;
            CurrJyousuu = (CurrJyousuu * CurrJyousuu) % pM;
        }
    }

    static long SolveNaive()
    {
        long UB = mK;

        // 場合の数[隣接した同色ブロック数]なDP表
        long[] PrevDP = new long[UB + 1];
        PrevDP[0] = 1;

        for (long I = 1; I <= mN; I++) {
            long[] CurrDP = new long[UB + 1];
            if (I == 1) {
                CurrDP[0] = mM;
            }
            else {
                for (long J = 0; J <= UB; J++) {
                    if (PrevDP[J] == 0) break;

                    Action<long, long> UpdateAct = (pNewJ, pAddVal) =>
                    {
                        if (pNewJ > UB) return;
                        CurrDP[pNewJ] += pAddVal;
                        CurrDP[pNewJ] %= Hou;
                    };

                    // 同じ色で隣接させる場合
                    UpdateAct(J + 1, PrevDP[J]);

                    // 違う色で隣接させる場合
                    long ProdVal = PrevDP[J] * (mM - 1);
                    ProdVal %= Hou;
                    UpdateAct(J, ProdVal);
                }
            }
            PrevDP = CurrDP;
        }

        long Answer = 0;
        foreach (long EachLong in PrevDP) {
            Answer += EachLong;
            Answer %= Hou;
        }
        return Answer;
    }
}

#region ChooseMod
// 二項係数クラス
internal class ChooseMod
{
    private long mHou;

    private long[] mFacArr;
    private long[] mFacInvArr;
    private long[] mInvArr;

    // コンストラクタ
    internal ChooseMod(long pCnt, long pHou)
    {
        mHou = pHou;
        mFacArr = new long[pCnt + 1];
        mFacInvArr = new long[pCnt + 1];
        mInvArr = new long[pCnt + 1];

        mFacArr[0] = mFacArr[1] = 1;
        mFacInvArr[0] = mFacInvArr[1] = 1;
        mInvArr[1] = 1;
        for (int I = 2; I <= pCnt; I++) {
            mFacArr[I] = mFacArr[I - 1] * I % mHou;
            mInvArr[I] = mHou - mInvArr[mHou % I] * (mHou / I) % mHou;
            mFacInvArr[I] = mFacInvArr[I - 1] * mInvArr[I] % mHou;
        }
    }

    // nCrを返す
    internal long DeriveChoose(long pN, long pR)
    {
        if (pN < pR) return 0;
        if (pN < 0 || pR < 0) return 0;
        return mFacArr[pN] * (mFacInvArr[pR] * mFacInvArr[pN - pR] % mHou) % mHou;
    }
}
#endregion


解説

100ブロックを50色で塗り分けて、30箇所まで色重複を認める
ケースを考察します。

色重複の数を0から30まで全探索して考えると、

色重複の数が0の場合、50*(49の99乗)
色重複の数が1の場合、50*(49の98乗)* 99C1
色重複の数が2の場合、50*(48の98乗)* 99C2
色重複の数が3の場合、50*(48の97乗)* 99C3
以下同様
となるので、総合計が解になります。