結果

問題 No.3615 Ge Gusser
コンテスト
ユーザー 37zigen
提出日時 2026-08-10 19:50:23
言語 Java
(openjdk 25.0.2)
コンパイル:
javac -encoding UTF8 _filename_
実行:
java -ea -Xmx700m -Xss256M -DONLINE_JUDGE=true _class_
結果
AC  
実行時間 612 ms / 3,000 ms
+ 404µs
コード長 7,220 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,858 ms
コンパイル使用メモリ 106,184 KB
実行使用メモリ 147,412 KB
最終ジャッジ日時 2026-08-10 19:50:31
合計ジャッジ時間 6,651 ms
ジャッジサーバーID
(参考情報)
judge1_0 / judge2_0
このコードへのチャレンジ
(要ログイン)
サブタスク 配点 結果
サンプル 0 % AC * 3
小課題1 40 % AC * 8
小課題2 40 % AC * 15
小課題3 20 % AC * 27
合計 2.5 * 100% = 250 点
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Random;
import java.util.Set;
import java.util.TreeMap;
import java.util.function.BiFunction;
import java.util.function.DoubleUnaryOperator;
import java.util.function.IntBinaryOperator;
import java.util.function.LongBinaryOperator;
import java.util.function.LongToDoubleFunction;
import java.util.function.Predicate;
import java.util.random.RandomGenerator;

public class Main {
    static MyPrintWriter pw = MyPrintWriter.getInstance();

    static FastScanner sc = FastScanner.getInstance();

    public static void main(String[] args) throws IOException {
        Thread.setDefaultUncaughtExceptionHandler((t, e) -> System.exit(1));
        new Main().run();
        pw.flush();
    }

    void run() {
        int N = sc.nextInt();
        int M = sc.nextInt();
        long[] A = new long[N];
        for (int i = 0; i < N; i++) {
            char[] S = sc.next().toCharArray();
            for (int j = 0; j < S.length; j++) {
                if (S[j] == 'o') {
                    A[i] |= 1L << j;
                }
            }
        }
        var f = new long[1 << N];
        Arrays.fill(f, (1L << M) - 1);
        for (int i = 0; i < N; i++) {
            f[1 << i] = A[i];
        }
        f = BooleanLattice.subsetZeta(f, (x, y) -> x & y);
        var dp = new double[1 << N];
        for (int i = dp.length - 1; i >= 0; i--) {
            if (Long.bitCount(f[i]) <= 1) {
                continue;
            }
            int size = N - Long.bitCount(i);
            dp[i] = 1;
            for (int next = 0; next < N; ++next) {
                if (((i >> next) % 2) == 1) {
                    continue;
                }
                dp[i] += (1.0 / size) * dp[i | (1 << next)];
            }
        }
        double ans = dp[0];
        pw.println(ans);
    }
}

class BooleanLattice {
    /**
     * b(S) = op_{T ⊆ S} a(T) を計算する。
     *
     * @param a
     * 		入力配列
     * @param op
     * 		二項演算
     * @return subset zeta transform
     */
    public static long[] subsetZeta(long[] a, LongBinaryOperator op) {
        long[] b = Arrays.copyOf(a, a.length);
        int N = MathUtils.floorLog2(a.length);
        for (int i = 0; i < N; ++i) {
            for (int s = 0; s < (1 << N); ++s) {
                if (Ints.bitAt(s, i) == 0) {
                    continue;
                }
                b[s] = op.applyAsLong(b[s], b[s ^ (1 << i)]);
            }
        }
        return b;
    }
}

class FastScanner {
    private static FastScanner instance = null;

    private final InputStream in = System.in;

    private final byte[] buffer = new byte[1 << 16];

    private int ptr = 0;

    private int buflen = 0;

    private FastScanner() {
    }

    public static FastScanner getInstance() {
        if (instance == null) {
            instance = new FastScanner();
        }
        return instance;
    }

    private boolean hasNextByte() {
        if (ptr < buflen) {
            return true;
        }
        ptr = 0;
        try {
            buflen = in.read(buffer);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return buflen > 0;
    }

    private int readByte() {
        if (hasNextByte()) {
            return buffer[ptr++];
        } else {
            return -1;
        }
    }

    private boolean isPrintableChar(int c) {
        return (33 <= c) && (c <= 126);
    }

    public boolean hasNext() {
        while (hasNextByte() && (!isPrintableChar(buffer[ptr]))) {
            ptr++;
        } 
        return hasNextByte();
    }

    public String next() {
        if (!hasNext()) {
            throw new NoSuchElementException();
        }
        StringBuilder sb = new StringBuilder();
        int b = readByte();
        while (isPrintableChar(b)) {
            sb.appendCodePoint(b);
            b = readByte();
        } 
        return sb.toString();
    }

    public long nextLong() {
        if (!hasNext()) {
            throw new NoSuchElementException();
        }
        long n = 0;
        boolean minus = false;
        int b = readByte();
        if (b == '-') {
            minus = true;
            b = readByte();
        }
        while ((b >= '0') && (b <= '9')) {
            // n = n * 10 + (b - '0');
            n = ((n << 1) + (n << 3)) + (b - '0');
            b = readByte();
        } 
        return minus ? -n : n;
    }

    public int nextInt() {
        return ((int) (nextLong()));
    }
}

class Ints {
    public static int bitAt(int binary, int pos) {
        if (pos >= 32) {
            return 0;
        }
        return (binary >>> pos) % 2;
    }
}

class MathUtils {
    /**
     * x=0のときは-1
     *
     * @param x
     * @return  */
    public static int floorLog2(long x) {
        if (x == 0) {
            return -1;
        }
        return 63 - Long.numberOfLeadingZeros(x);
    }
}

class MyPrintWriter extends PrintWriter {
    private static MyPrintWriter instance = null;

    private MyPrintWriter() {
        super(System.out);
    }

    public static MyPrintWriter getInstance() {
        if (instance == null) {
            instance = new MyPrintWriter();
        }
        return instance;
    }
}


// --- Original Code ---
// 
// 
// import java.io.IOException;
// import java.util.Arrays;
// 
// import library.tools.FastScanner;
// import library.tools.MergeFiles;
// import library.tools.MyPrintWriter;
// import library.util.ArrayUtils;
// import library.util.Longs;
// import library.util.fold.WaveletMatrix;
// import library.util.poset.BooleanLattice;
// 
// public class Main {
// 	static MyPrintWriter pw = MyPrintWriter.getInstance();
// 	static FastScanner sc = FastScanner.getInstance();
// 
// 	public static void main(String[] args) throws IOException {
// 		new Main().run();
// 		pw.flush();
// 		MergeFiles.export();
// 	}
// 	
// 
// 	void run() {
// 		int N=sc.nextInt();
// 		int M=sc.nextInt();
// 		long[]A=new long[N];
// 		for (int i = 0; i < N; i++) {
// 			char[]S=sc.next().toCharArray();
// 			for (int j = 0; j < S.length; j++) {
// 				if(S[j]=='o')A[i]|=1L<<j;
// 			}
// 		}
// 		var f=new long[1<<N];
// 		Arrays.fill(f, (1L<<M)-1);
// 		for (int i = 0; i < N; i++) {
// 			f[1<<i]=A[i];
// 		}
// 		f=BooleanLattice.subsetZeta(f, (x, y)-> x & y);
// 		var dp=new double[1<<N];
// 		for (int i = dp.length - 1; i >= 0; i--) {
// 			if (Long.bitCount(f[i])<=1)continue;
// 			int size=N-Long.bitCount(i);
// 			dp[i]=1;
// 			for (int next=0;next<N;++next) {
// 				if ((i >> next) % 2 == 1) continue;
// 				dp[i]+=1./size*dp[i|(1<<next)];
// 			}
// 		}
// 		double ans=dp[0];
// 		pw.println(ans);
// 	}
//     
// 	void tr(Object... objects) {
// 		System.out.println(Arrays.deepToString(objects));
// 	}
// }
// 
0