結果

問題 No.463 魔法使いのすごろく🎲
ユーザー uwiuwi
提出日時 2016-12-14 01:11:07
言語 Java21
(openjdk 21)
結果
AC  
実行時間 490 ms / 2,000 ms
コード長 6,047 bytes
コンパイル時間 4,990 ms
コンパイル使用メモリ 84,724 KB
実行使用メモリ 48,248 KB
最終ジャッジ日時 2024-05-07 13:34:05
合計ジャッジ時間 16,731 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 213 ms
43,876 KB
testcase_01 AC 170 ms
41,560 KB
testcase_02 AC 173 ms
41,708 KB
testcase_03 AC 134 ms
41,592 KB
testcase_04 AC 215 ms
43,572 KB
testcase_05 AC 230 ms
45,016 KB
testcase_06 AC 129 ms
40,952 KB
testcase_07 AC 154 ms
41,580 KB
testcase_08 AC 158 ms
41,252 KB
testcase_09 AC 452 ms
48,248 KB
testcase_10 AC 158 ms
41,356 KB
testcase_11 AC 246 ms
44,680 KB
testcase_12 AC 212 ms
44,164 KB
testcase_13 AC 124 ms
41,172 KB
testcase_14 AC 428 ms
47,984 KB
testcase_15 AC 358 ms
47,008 KB
testcase_16 AC 130 ms
41,488 KB
testcase_17 AC 331 ms
47,072 KB
testcase_18 AC 125 ms
40,932 KB
testcase_19 AC 152 ms
41,668 KB
testcase_20 AC 126 ms
41,052 KB
testcase_21 AC 419 ms
47,412 KB
testcase_22 AC 53 ms
36,580 KB
testcase_23 AC 361 ms
47,400 KB
testcase_24 AC 380 ms
46,856 KB
testcase_25 AC 403 ms
47,676 KB
testcase_26 AC 461 ms
48,188 KB
testcase_27 AC 426 ms
47,712 KB
testcase_28 AC 490 ms
47,852 KB
testcase_29 AC 410 ms
47,632 KB
testcase_30 AC 438 ms
47,704 KB
testcase_31 AC 401 ms
47,476 KB
testcase_32 AC 472 ms
47,956 KB
testcase_33 AC 423 ms
47,452 KB
testcase_34 AC 410 ms
47,620 KB
testcase_35 AC 125 ms
40,732 KB
testcase_36 AC 125 ms
41,216 KB
testcase_37 AC 141 ms
41,180 KB
testcase_38 AC 52 ms
36,604 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package adv2016;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.InputMismatchException;

public class N463_2 {
	InputStream is;
	PrintWriter out;
	String INPUT = "";
	
	void solve()
	{
		int n = ni(), m = ni();
		if(n == 2){
			out.println(0);
			return;
		}
		if(m == n-1){
			out.println(0);
			return;
		}
		int[] c = na(n-2);
		
		double[][] M = new double[2*n][2*n];
		for(int i = 0;i < n;i++){
			if(i < n-1){
				for(int j = 1;j <= m;j++){
					int x = i + j;
					if(x >= n)x = 2*n-2-x;
					M[i][x] += 1./m;
					M[i+n][x+n] += 1./m;
					if(x >= 1 && x-1 < c.length)M[i][x+n] += (double)c[x-1]/m;
				}
			}
		}
		M[n][n] = 1;
		double[] start = new double[2*n];
		start[2*n-1] = 1;
		double[] es0 = S(M, start);
		
		double[] es1 = new double[n];
		for(int i = n-m-1;i >= 0;i--){
			double e = 0;
			for(int j = 1;j <= m;j++){
				e += 1./m*(es1[i+j] + (i+j-1 < c.length ? c[i+j-1] : 0.));
			}
			for(int j = 1;j <= m;j++){
				e = Math.min(e, es0[i+j]+(i+j-1 < c.length ? c[i+j-1] : 0.));
			}
			es1[i] = e;
		}
//		tr(es1);
		
		out.printf("%.14f\n", es1[0]);
	}
	
	public static double[] S(double[][] T, double[] v)
	{
		int n = T.length;
		double[][] X = new double[n][n];
		// ...(1+T^4)(1+T^2)(1+T)v
		for(int rep = 0;rep < 1000;rep++){ // FIXME
			for(int i = 0;i < n;i++){
				Arrays.fill(X[i], 0.);
				X[i][i] = 1;
				for(int j = 0;j < n;j++){
					X[i][j] += T[i][j];
				}
			}
			v = mul(X, v);
			
			double[][] U = p2(T);
			double norm = 0;
			for(int i = 0;i < n;i++){
				for(int j = 0;j < n;j++){
					double d = T[i][j]-U[i][j];
					norm += d*d;
				}
			}
			if(norm == 0)break;
			T = U;
		}
		return v;
	}

	public static double[] mul(double[][] A, double[] v)
	{
		int m = A.length;
		int n = v.length;
		double[] w = new double[m];
		for(int i = 0;i < m;i++){
			double sum = 0;
			for(int k = 0;k < n;k++){
				sum += A[i][k] * v[k];
			}
			w[i] = sum;
		}
		return w;
	}
	
	public static double[][] p2(double[][] A)
	{
		int n = A.length;
		double[][] C = new double[n][n];
		for(int i = 0;i < n;i++){
			for(int k = 0;k < n;k++){
				for(int j = 0;j < n;j++){
					C[i][j] += A[i][k] * A[k][j];
				}
			}
		}
		return C;
	}
	
	public static double[] solve(double[][] a, double[] c)
	{
		int n = a.length;
		int[] ps = new int[n];
		for(int i = 0;i < n;i++)ps[i] = i;
		
		// Forward Elimination
		for(int i = 0;i < n;i++){
			int pivot = -1;
			int from = -1;
			double amax = 0;
			for(int j = i;j < n;j++){
				if(Math.abs(a[ps[j]][i]) > amax){
					amax = Math.abs(a[ps[j]][i]);
					pivot = ps[j];
					from = j;
				}
			}
			if(pivot == -1)return null;
			int d = ps[i]; ps[i] = ps[from]; ps[from] = d;
			
			for(int j = i+1;j < n;j++){
				a[ps[i]][j] /= a[ps[i]][i];
			}
			c[ps[i]] /= a[ps[i]][i];
			a[ps[i]][i] = 1.0;
			for(int j = i+1;j < n;j++){
				for(int k = i+1;k < n;k++){
					a[ps[j]][k] -= a[ps[j]][i] * a[ps[i]][k];
				}
				c[ps[j]] -= a[ps[j]][i] * c[ps[i]];
				a[ps[j]][i] = 0.0;
			}
			
		}
		
		// Back Substitution
		for(int i = n-1;i >= 0;i--){
			for(int j = i-1;j >= 0;j--){
				c[ps[j]] -= a[ps[j]][i] * c[ps[i]];
			}
		}
		
		double[] ret = new double[n];
		for(int i = 0;i < n;i++){
			ret[i] = c[ps[i]];
		}
		
		return ret;
	}

	
	void run() throws Exception
	{
		is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes());
		out = new PrintWriter(System.out);
		
		long s = System.currentTimeMillis();
		solve();
		out.flush();
		if(!INPUT.isEmpty())tr(System.currentTimeMillis()-s+"ms");
//		Thread t = new Thread(null, null, "~", Runtime.getRuntime().maxMemory()){
//			@Override
//			public void run() {
//				long s = System.currentTimeMillis();
//				solve();
//				out.flush();
//				if(!INPUT.isEmpty())tr(System.currentTimeMillis()-s+"ms");
//			}
//		};
//		t.start();
//		t.join();
	}
	
	public static void main(String[] args) throws Exception { new N463_2().run(); }
	
	private byte[] inbuf = new byte[1024];
	public int lenbuf = 0, ptrbuf = 0;
	
	private int readByte()
	{
		if(lenbuf == -1)throw new InputMismatchException();
		if(ptrbuf >= lenbuf){
			ptrbuf = 0;
			try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); }
			if(lenbuf <= 0)return -1;
		}
		return inbuf[ptrbuf++];
	}
	
	private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); }
	private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; }
	
	private double nd() { return Double.parseDouble(ns()); }
	private char nc() { return (char)skip(); }
	
	private String ns()
	{
		int b = skip();
		StringBuilder sb = new StringBuilder();
		while(!(isSpaceChar(b))){ // when nextLine, (isSpaceChar(b) && b != ' ')
			sb.appendCodePoint(b);
			b = readByte();
		}
		return sb.toString();
	}
	
	private char[] ns(int n)
	{
		char[] buf = new char[n];
		int b = skip(), p = 0;
		while(p < n && !(isSpaceChar(b))){
			buf[p++] = (char)b;
			b = readByte();
		}
		return n == p ? buf : Arrays.copyOf(buf, p);
	}
	
	private int[] na(int n)
	{
		int[] a = new int[n];
		for(int i = 0;i < n;i++)a[i] = ni();
		return a;
	}
	
	private long[] nal(int n)
	{
		long[] a = new long[n];
		for(int i = 0;i < n;i++)a[i] = nl();
		return a;
	}
	
	private char[][] nm(int n, int m) {
		char[][] map = new char[n][];
		for(int i = 0;i < n;i++)map[i] = ns(m);
		return map;
	}
	
	private int[][] nmi(int n, int m) {
		int[][] map = new int[n][];
		for(int i = 0;i < n;i++)map[i] = na(m);
		return map;
	}
	
	private int ni() { return (int)nl(); }
	
	private long nl()
	{
		long num = 0;
		int b;
		boolean minus = false;
		while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
		if(b == '-'){
			minus = true;
			b = readByte();
		}
		
		while(true){
			if(b >= '0' && b <= '9'){
				num = num * 10 + (b - '0');
			}else{
				return minus ? -num : num;
			}
			b = readByte();
		}
	}
	
	private static void tr(Object... o) { System.out.println(Arrays.deepToString(o)); }
}
0