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

ABC263-E Sugoroku 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("1 1");
            //4
        }
        else if (InputPattern == "Input2") {
            WillReturn.Add("5");
            WillReturn.Add("3 1 2 1");
            //332748122
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

    const long Hou = 998244353;

    static long[] mAArr;
    static long UB;

    // フェニック木
    static Fenwick_Tree mIns_Fenwick_Tree;

    static void Main()
    {
        List<string> InputList = GetInputList();
        mAArr = InputList[1].Split(' ').Select(pX => long.Parse(pX)).ToArray();
        UB = mAArr.GetUpperBound(0);
        mIns_Fenwick_Tree = new Fenwick_Tree(mAArr.Length, Hou);

        for (long I = UB; 0 <= I; I--) {
            DeriveEx(I);
        }
        Console.WriteLine(mIns_Fenwick_Tree.GetSum(0, 0));
    }

    static void DeriveEx(long pCurrInd)
    {
        // 当たりの確率の分母
        long AtariBunbo = mAArr[pCurrInd] + 1;

        // 当たりの確率の分子
        long AtariBunsi = AtariBunbo - 1;

        // 当たりの確率の逆数
        long CurrEx = AtariBunbo;
        CurrEx *= DeriveGyakugen(AtariBunsi);
        CurrEx %= Hou;

        long RangeSta = pCurrInd + 1;
        long RangeEnd = pCurrInd + mAArr[pCurrInd];

        // 当たりの中での条件付確率
        long SumEx = mIns_Fenwick_Tree.GetSum(RangeSta, RangeEnd);
        SumEx *= DeriveGyakugen(AtariBunsi);
        SumEx %= Hou;

        CurrEx += SumEx;
        CurrEx %= Hou;

        if (CurrEx < 0) CurrEx += Hou;

        mIns_Fenwick_Tree.Add(pCurrInd, CurrEx);
    }

    // 引数の逆元を求める
    static Dictionary<long, long> mMemo2 = new Dictionary<long, long>();
    static long DeriveGyakugen(long pLong)
    {
        if (mMemo2.ContainsKey(pLong)) {
            return mMemo2[pLong];
        }
        return mMemo2[pLong] = DeriveBekijyou(pLong, Hou - 2, Hou);
    }

    // 繰り返し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;
        }
    }
}

// フェニック木
#region Fenwick_Tree
internal class Fenwick_Tree
{
    private long[] mBitArr;
    private long mExternalArrUB;
    private long mHou;

    // コンストラクタ
    internal Fenwick_Tree(long pExternalArrUB, long pHou)
    {
        mExternalArrUB = pExternalArrUB;

        // フェニック木の外部配列は0オリジンで、
        // フェニック木の内部配列は1オリジンなため、2を足す
        mBitArr = new long[pExternalArrUB + 2];

        mHou = pHou;
    }

    // [pSta,pEnd] のSumを返す
    internal long GetSum(long pSta, long pEnd)
    {
        long Result = GetSum(pEnd) - GetSum(pSta - 1);

        Result %= mHou;
        if (Result < 0) Result += mHou;
        return Result;
    }

    // [0,pEnd] のSumを返す
    internal long GetSum(long pEnd)
    {
        pEnd++; // 1オリジンに変更

        long Sum = 0;
        while (pEnd >= 1) {
            Sum += mBitArr[pEnd];
            Sum %= mHou;
            pEnd -= pEnd & -pEnd;
        }
        if (Sum < 0) Sum += mHou;
        return Sum;
    }

    // [I] に Xを加算
    internal void Add(long pI, long pX)
    {
        pI++; // 1オリジンに変更

        pX %= mHou;
        while (pI <= mBitArr.GetUpperBound(0)) {
            mBitArr[pI] += pX;
            mBitArr[pI] %= mHou;
            pI += pI & -pI;
        }
    }
}
#endregion


解説

下記の入力例では
3 1 2 1

下記の遷移候補のイメージになります。
0 0 0 0
1 1 1 1
2   2
3

なので、外れのある遷移での期待値を求める問題となり、
後退解析で解いてます。

区間和を高速に求めないとTLEするので
フェニック木を使ってます。