結果

問題 No.301 サイコロで確率問題 (1)
ユーザー GrenacheGrenache
提出日時 2015-11-14 01:16:54
言語 Java21
(openjdk 21)
結果
AC  
実行時間 427 ms / 1,000 ms
コード長 2,939 bytes
コンパイル時間 3,562 ms
コンパイル使用メモリ 75,424 KB
実行使用メモリ 61,528 KB
最終ジャッジ日時 2023-10-11 17:34:59
合計ジャッジ時間 5,395 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 427 ms
61,528 KB
testcase_01 AC 396 ms
61,164 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Iterator;


public class Main_yukicoder301 {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Printer pr = new Printer(System.out);

        long n = 1000;
    	double[] aa = new double[6 + 1];
    	double[] bb = new double[6 + 1];
    	long j;
    	for (j = n - 1; j >= 0; j--) {
    		bb[(int)(j % 7)] = 1;
    		aa[(int)(j % 7)] = 0;
    		for (int k = 1; k <= 6; k++) {
    			if (j + k > n) {
    				aa[(int)(j % 7)] += (double)1 / 6;
    			} else {
    				aa[(int)(j % 7)] += aa[(int)((j + k) % 7)] / 6;
    				bb[(int)(j % 7)] += bb[(int)((j + k) % 7)] / 6;
    			}
    		}
    	}

        int t = sc.nextInt();
        for (int i = 0; i < t; i++) {
            n = sc.nextLong();

            if (n < 1000) {
            	double[] a = new double[6 + 1];
            	double[] b = new double[6 + 1];
            	for (j = n - 1; j >= 0; j--) {
            		b[(int)(j % 7)] = 1;
            		a[(int)(j % 7)] = 0;
            		for (int k = 1; k <= 6; k++) {
            			if (j + k > n) {
            				a[(int)(j % 7)] += (double)1 / 6;
            			} else {
            				a[(int)(j % 7)] += a[(int)((j + k) % 7)] / 6;
            				b[(int)(j % 7)] += b[(int)((j + k) % 7)] / 6;
            			}
            		}
            	}
            	
            	pr.printf("%.13f\n", b[(int)((j + 1) % 7)] / (1 - a[(int)((j + 1) % 7)]));
            } else {
            	j = n - 1000;
            	double btmp = bb[(int)((0 + 1) % 7)] + (1 - aa[(int)((0 + 1) % 7)]) * (j + 1);

            	pr.printf("%.13f\n", btmp / (1 - aa[(int)((0 + 1) % 7)]));
            }
        }

        pr.close();
        sc.close();
    }

	@SuppressWarnings("unused")
	private static class Scanner {
		BufferedReader br;
		Iterator<String> it;
		
		Scanner (InputStream in) {
			br = new BufferedReader(new InputStreamReader(in));
		}
		
		String next() throws RuntimeException  {
			try {
				if (it == null || !it.hasNext()) {
					it = Arrays.asList(br.readLine().split(" ")).iterator();
				}
				return it.next();
			} catch (IOException e) {
				throw new IllegalStateException();
			}
		}
		
		int nextInt() throws RuntimeException {
			return Integer.parseInt(next());
		}

		long nextLong() throws RuntimeException {
			return Long.parseLong(next());
		}

		float nextFloat() throws RuntimeException {
			return Float.parseFloat(next());
		}

		double nextDouble() throws RuntimeException {
			return Double.parseDouble(next());
		}

		void close() {
			try {
				br.close();
			} catch (IOException e) {
//				throw new IllegalStateException();
			}
		}
	}

	private static class Printer extends PrintWriter {
		Printer(PrintStream out) {
			super(out);
		}
	}
}
0