結果

問題 No.1835 Generalized Monty Hall Problem
ユーザー tentententen
提出日時 2023-07-21 08:39:46
言語 Java21
(openjdk 21)
結果
AC  
実行時間 95 ms / 1,000 ms
コード長 2,858 bytes
コンパイル時間 4,018 ms
コンパイル使用メモリ 95,932 KB
実行使用メモリ 40,180 KB
最終ジャッジ日時 2024-09-21 10:51:33
合計ジャッジ時間 4,830 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 86 ms
38,484 KB
testcase_01 AC 83 ms
38,200 KB
testcase_02 AC 83 ms
39,800 KB
testcase_03 AC 82 ms
38,468 KB
testcase_04 AC 95 ms
40,016 KB
testcase_05 AC 82 ms
38,472 KB
testcase_06 AC 89 ms
39,652 KB
testcase_07 AC 88 ms
39,748 KB
testcase_08 AC 82 ms
38,408 KB
testcase_09 AC 80 ms
38,316 KB
testcase_10 AC 84 ms
40,180 KB
testcase_11 AC 84 ms
38,220 KB
testcase_12 AC 86 ms
40,036 KB
testcase_13 AC 84 ms
38,120 KB
testcase_14 AC 83 ms
38,476 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;
import java.util.stream.*;

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        int m = sc.nextInt();
        int k = sc.nextInt();
        Bunsu x = new Bunsu(m, n).multiply(new Bunsu(m - 1, n - k - 1)).add(new Bunsu(n - m, n).multiply(new Bunsu(m, n - k - 1)));
        System.out.println(x);
    }
    
    static class Bunsu {
        long child;
        long mother;
        
        public Bunsu(long child, long mother) {
            this.child = child;
            this.mother = mother;
            init();
        }
        
        private void init() {
            if (child == 0) {
                mother = 1;
                return;
            }
            long gcd = getGCD(child, mother);
            child /= gcd;
            mother /= gcd;
        }
        
        private long getGCD(long x, long y) {
            if (y == 0) {
                return x;
            } else {
                return getGCD(y, x % y);
            }
        }
        
        private long getLCM(long x, long y) {
            return x / getGCD(x, y) * y;
        }
        
        public Bunsu multiply(Bunsu x) {
            return new Bunsu(child * x.child, mother * x.mother);
        }
        
        public Bunsu add(Bunsu x) {
            long lcm = getLCM(mother, x.mother);
            return new Bunsu(child * (lcm / mother) + x.child * (lcm / x.mother), lcm);
        }
        
        public String toString() {
            return child + " " + mother;
        }
    }
    
}
class Utilities {
    static String arrayToLineString(Object[] arr) {
        return Arrays.stream(arr).map(x -> x.toString()).collect(Collectors.joining("\n"));
    }
    
    static String arrayToLineString(int[] arr) {
        return String.join("\n", Arrays.stream(arr).mapToObj(String::valueOf).toArray(String[]::new));
    }
}
class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    StringBuilder sb = new StringBuilder();
    
    public Scanner() throws Exception {
        
    }
    
    public int nextInt() throws Exception {
        return Integer.parseInt(next());
    }
    
    public long nextLong() throws Exception {
        return Long.parseLong(next());
    }
    
    public double nextDouble() throws Exception {
        return Double.parseDouble(next());
    }
    
    public int[] nextIntArray() throws Exception {
        return Stream.of(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
    }
    
    public String next() throws Exception {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
    
}
0