AtCoderのARC    次のARCの問題へ    前のARCの問題へ

ARC127-A Leading 1s


問題へのリンク


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("11");
            //4
        }
        else if (InputPattern == "Input2") {
            WillReturn.Add("120");
            //44
        }
        else if (InputPattern == "Input3") {
            WillReturn.Add("987654321");
            //123456789
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

    struct JyoutaiDef
    {
        internal bool FreeNum; // 数字自由フラグ
        internal long Cnt1;    // リーディング1の数
        internal bool IsLeading0;  // リーディング0フラグ
        internal bool IsLeading1;  // リーディング1フラグ
    }

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

        string StrN = InputList[0];

        // 場合の数[状態定義]なDP表
        var PrevDP = new Dictionary<JyoutaiDef, long>();
        JyoutaiDef FirstJyoutai;
        FirstJyoutai.FreeNum = false;
        FirstJyoutai.Cnt1 = 0;
        FirstJyoutai.IsLeading0 = true;
        FirstJyoutai.IsLeading1 = true;
        PrevDP[FirstJyoutai] = 1;

        for (int I = 0; I <= StrN.Length - 1; I++) {
            var CurrDP = new Dictionary<JyoutaiDef, long>();
            foreach (var EachPair in PrevDP) {
                for (char NewChar = '0'; NewChar <= '9'; NewChar++) {
                    if (EachPair.Key.FreeNum == false && StrN[I] < NewChar) break;

                    bool NewFreeNum = EachPair.Key.FreeNum;
                    if (StrN[I] > NewChar) NewFreeNum = true;

                    long NewCnt1 = EachPair.Key.Cnt1;
                    if (NewChar == '1' && EachPair.Key.IsLeading1) {
                        NewCnt1++;
                    }

                    bool NewIsLeading0 = EachPair.Key.IsLeading0;
                    bool NewIsLeading1 = EachPair.Key.IsLeading1;
                    if (NewChar == '0') {
                        if (EachPair.Key.IsLeading0 == false) {
                            NewIsLeading1 = false;
                        }
                    }
                    else if (NewChar == '1') {
                        NewIsLeading0 = false;
                    }
                    else {
                        NewIsLeading0 = false;
                        NewIsLeading1 = false;
                    }

                    JyoutaiDef NewJyoutai;
                    NewJyoutai.FreeNum = NewFreeNum;
                    NewJyoutai.Cnt1 = NewCnt1;
                    NewJyoutai.IsLeading0 = NewIsLeading0;
                    NewJyoutai.IsLeading1 = NewIsLeading1;
                    if (CurrDP.ContainsKey(NewJyoutai) == false) {
                        CurrDP[NewJyoutai] = 0;
                    }
                    CurrDP[NewJyoutai] += EachPair.Value;
                }
            }
            PrevDP = CurrDP;
        }

        long Answer = 0;
        foreach (var EachPair in PrevDP) {
            Answer += EachPair.Value * EachPair.Key.Cnt1;
        }
        Console.WriteLine(Answer);
    }
}


解説

リーディング0がありえるかと、
リーディング1がありえるかを管理しつつ、
桁DPしてます。