結果

問題 No.463 魔法使いのすごろく🎲
ユーザー eitahoeitaho
提出日時 2016-12-14 14:04:57
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 1,265 ms / 2,000 ms
コード長 5,000 bytes
コンパイル時間 3,542 ms
コンパイル使用メモリ 114,256 KB
実行使用メモリ 31,660 KB
最終ジャッジ日時 2023-08-20 06:33:50
合計ジャッジ時間 10,002 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 100 ms
25,784 KB
testcase_01 AC 74 ms
23,740 KB
testcase_02 AC 71 ms
23,856 KB
testcase_03 AC 71 ms
23,684 KB
testcase_04 AC 70 ms
25,748 KB
testcase_05 AC 70 ms
25,656 KB
testcase_06 AC 69 ms
23,796 KB
testcase_07 AC 68 ms
23,720 KB
testcase_08 AC 77 ms
23,788 KB
testcase_09 AC 71 ms
23,772 KB
testcase_10 AC 69 ms
23,716 KB
testcase_11 AC 157 ms
29,904 KB
testcase_12 AC 91 ms
23,672 KB
testcase_13 AC 67 ms
23,632 KB
testcase_14 AC 87 ms
25,692 KB
testcase_15 AC 110 ms
25,684 KB
testcase_16 AC 69 ms
23,528 KB
testcase_17 AC 134 ms
25,676 KB
testcase_18 AC 68 ms
25,616 KB
testcase_19 AC 71 ms
23,636 KB
testcase_20 AC 68 ms
23,752 KB
testcase_21 AC 223 ms
27,856 KB
testcase_22 AC 1,265 ms
31,660 KB
testcase_23 AC 1,241 ms
29,496 KB
testcase_24 AC 69 ms
23,768 KB
testcase_25 AC 71 ms
23,652 KB
testcase_26 AC 75 ms
25,664 KB
testcase_27 AC 69 ms
21,688 KB
testcase_28 AC 368 ms
30,212 KB
testcase_29 AC 71 ms
23,772 KB
testcase_30 AC 93 ms
23,724 KB
testcase_31 AC 70 ms
23,700 KB
testcase_32 AC 91 ms
25,732 KB
testcase_33 AC 71 ms
25,744 KB
testcase_34 AC 69 ms
21,668 KB
testcase_35 AC 70 ms
23,620 KB
testcase_36 AC 71 ms
25,780 KB
testcase_37 AC 70 ms
23,708 KB
testcase_38 AC 67 ms
19,704 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc)
Copyright (C) Microsoft Corporation. All rights reserved.

ソースコード

diff #

using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using System.Diagnostics;
using System.Numerics;
using Enu = System.Linq.Enumerable;

public class Program
{
    public void Solve()
    {
        int N = Reader.Int();
        int D = Reader.Int();
        var Cost = new[] { 0 }.Concat(Reader.IntArray(N - 2)).Concat(new[] { 0 }).ToArray();
        var ans = new double[N, 2];

        int size = D - 1;
        var M = new double[size][];
        for (int i = 0; i < M.Length; i++)
            M[i] = new double[size];
        for (int i = 0; i < size; i++)
        {
            int start = N - D + i;
            for (int d = 1; d <= D; d++)
            {
                int next = start + d;
                if (next == N - 1) continue;
                if (next >= N) next = N - 2 - (next - N);
                next = next - (N - D);
                M[next][i] += 1.0 / D;
            }
        }
        M = MatrixPowerSum(M, 1L << 20);

        for (int i = 0; i < size; i++)
        {
            int at = N - D + i;
            double ex = Cost[at];
            for (int c = 0; c < size; c++)
                ex += M[c][i] * Cost[N - D + c];
            ans[at, 0] = Cost[at];
            ans[at, 1] = ex;
        }
        for (int i = N - D - 1; i >= 0; i--)
            for (int used = 0; used < 2; used++)
            {
                double ex0 = Cost[i], ex1 = double.MaxValue;
                for (int d = 1; d <= D; d++)
                {
                    ex0 += ans[i + d, used] / D;
                    if (used == 0) ex1 = Math.Min(ex1, Cost[i] + ans[i + d, 1]);
                }
                ans[i, used] = Math.Min(ex0, ex1);
            }

        Console.WriteLine(ans[0, 0]);
    }

    static double[][] MatrixPowerSum(double[][] A, long n)
    {
        if (n == 1) return A;
        int size = A.Length;
        var half = MatrixPower(A, n / 2);
        var sum = MatrixPowerSum(A, n / 2);
        var right = MatrixMult(half, sum);
        for (int r = 0; r < size; r++)
            for (int c = 0; c < size; c++)
                sum[r][c] += right[r][c];
        if (n % 2 == 0) return sum;
        var C = MatrixPower(A, n);
        for (int r = 0; r < size; r++)
            for (int c = 0; c < size; c++)
                sum[r][c] += C[r][c];
        return sum;
    }
    static double[][] MatrixPower(double[][] A, long n)
    {
        int size = A.Length;
        double[][] res = new double[size][];
        for (int i = 0; i < size; i++) { res[i] = new double[size]; res[i][i] = 1; }
        while (n > 0)
        {
            if ((n & 1) == 1) res = MatrixMult(res, A);
            A = MatrixMult(A, A);
            n >>= 1;
        }
        return res;
    }
    static double[][] MatrixMult(double[][] A, double[][] B)
    {
        double[][] res = new double[A.Length][];
        for (int i = 0; i < res.Length; i++) res[i] = new double[B[0].Length];
        for (int i = 0; i < A.Length; i++)
            for (int k = 0; k < A[0].Length; k++)
                for (int j = 0; j < B[0].Length; j++)
                    res[i][j] += A[i][k] * B[k][j];
        return res;
    }
}



class Entry { static void Main() { new Program().Solve(); } }
class Reader
{
    static TextReader reader = Console.In;
    static readonly char[] separator = { ' ' };
    static readonly StringSplitOptions op = StringSplitOptions.RemoveEmptyEntries;
    static string[] A = new string[0];
    static int i;
    static void Init() { A = new string[0]; }
    public static void Set(TextReader r) { reader = r; Init(); }
    public static void Set(string file) { reader = new StreamReader(file); Init(); }
    public static bool HasNext() { return CheckNext(); }
    public static string String() { return Next(); }
    public static int Int() { return int.Parse(Next()); }
    public static long Long() { return long.Parse(Next()); }
    public static double Double() { return double.Parse(Next()); }
    public static int[] IntLine() { return Array.ConvertAll(Split(Line()), int.Parse); }
    public static int[] IntArray(int N) { return Range(N, Int); }
    public static int[][] IntTable(int H) { return Range(H, IntLine); }
    public static string[] StringArray(int N) { return Range(N, Next); }
    public static string[][] StringTable(int N) { return Range(N, () => Split(Line())); }
    public static string Line() { return reader.ReadLine().Trim(); }
    public static T[] Range<T>(int N, Func<T> f) { var r = new T[N]; for (int i = 0; i < N; r[i++] = f()) ; return r; }
    static string[] Split(string s) { return s.Split(separator, op); }
    static string Next() { CheckNext(); return A[i++]; }
    static bool CheckNext()
    {
        if (i < A.Length) return true;
        string line = reader.ReadLine();
        if (line == null) return false;
        if (line == "") return CheckNext();
        A = Split(line);
        i = 0;
        return true;
    }
}
0