結果
| 問題 |
No.463 魔法使いのすごろく🎲
|
| コンテスト | |
| ユーザー |
eitaho
|
| 提出日時 | 2016-12-14 14:04:57 |
| 言語 | C#(csc) (csc 3.9.0) |
| 結果 |
AC
|
| 実行時間 | 1,229 ms / 2,000 ms |
| コード長 | 5,000 bytes |
| コンパイル時間 | 998 ms |
| コンパイル使用メモリ | 118,208 KB |
| 実行使用メモリ | 33,788 KB |
| 最終ジャッジ日時 | 2024-11-30 07:08:22 |
| 合計ジャッジ時間 | 6,616 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 36 |
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc) Copyright (C) Microsoft Corporation. All rights reserved.
ソースコード
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;
}
}
eitaho