Codeforces Round #362 (Div. 2) E. PLEASE

問題

Problem - E - Codeforces

3つのカップを1列に伏せて並べておき、真ん中のカップにコインを入れる。左右どちらかのカップをランダムに選んで真ん中のカップと交換するという操作をn回繰り返す。

n は非常に大きい数なので、以下の式で与える。

 n = \prod_{i=1}^k a_i

n 回目の操作が終わった後、真ん中のカップの中にコインがある確率を分数で出力しなさい。分数は既約分数で、分母と分子は、mod 1000000007 しなさい。

解法

i 回目の操作が終わった後に右のカップにコインが入っている確率をa_i, 中央のカップにコインが入っている確率をb_i, 右のカップにコインが入っている確率をc_i, とおくと、

\left(
    \begin{array}{c}
a_{i+1} \\
b_{i+1} \\
c_{i+1} \\
    \end{array}
  \right)
 = \left(
    \begin{array}{ccc}
1/2 & 1/2 & 0 \\
1/2 & 0 & 1/2 \\
0 & 1/2 & 1/2 \\
    \end{array}
  \right)
\left(
    \begin{array}{c}
a_{i} \\
b_{i} \\
c_{i} \\
    \end{array}
  \right)

と漸化式が書ける。

https://www.wolframalpha.com/input/?i=%7B%7B1%2F2,1%2F2,0%7D,%7B1%2F2,0,1%2F2%7D,%7B0,1%2F2,1%2F2%7D%7D%5En

より、 2^n とかを頑張って計算すれば良さそう、ということが分かる。

問題の本質はここではなく、いかに大きい数をオーバーフローさせずに保存し続けるかということであり、言語クイズのような側面がある。

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.NoSuchElementException;

public class E {
  private static class Task {
    final long MOD = (long) 1e9 + 7;
    BigInteger modInteger = BigInteger.valueOf(MOD);
    BigInteger two = BigInteger.valueOf(2);
    BigInteger three = BigInteger.valueOf(3);

    void solve(FastScanner in, PrintWriter out) throws Exception {
      int K = in.nextInt();
      BigInteger n2 = two;
      boolean allOdd = true;
      boolean allOne = true;
      long check30 = 1;
      for (int i = 0; i < K; i++) {
        long a = in.nextLong();
        if (a % 2 == 0) allOdd = false;
        if (a > 1) allOne = false;
        if (check30 <= 30) {
          if (a > 30 || check30 * a > 30) {
            check30 = 31;
          } else {
            check30 *= a;
          }
        }
        n2 = n2.modPow(BigInteger.valueOf(a), modInteger);
      }
      if (check30 <= 30) {
        n2 = n2.divide(two);
      } else {
        n2 = n2.multiply(two.modInverse(modInteger));
      }
      n2 = n2.remainder(modInteger);
      if (allOne) {
        out.println("0/1");
        return;
      }

      BigInteger child = n2;
      //allOdd=true ならば全て奇数なので、n は偶数になり、1引くことになる
      if (!allOdd) {
        child = child.add(BigInteger.ONE);
      } else {
        child = child.subtract(BigInteger.ONE);
      }

      if (check30 <= 30) {
        child = child.divide(three);
      } else {
        child = child.multiply(three.modInverse(modInteger));
      }
      child = child.remainder(modInteger);

      out.print(child.toString());
      out.print("/");
      out.print(n2.toString());
      out.println();
    }
  }

  // Template
  public static void main(String[] args) throws Exception {
    OutputStream outputStream = System.out;
    FastScanner in = new FastScanner();
    PrintWriter out = new PrintWriter(outputStream);
    Task solver = new Task();
    solver.solve(in, out);
    out.close();
  }
  private static class FastScanner {
    private final InputStream in = System.in;
    private final byte[] buffer = new byte[1024];
    private int ptr = 0;
    private int bufferLength = 0;

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

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

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

    private void skipUnprintable() {
      while (hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++;
    }

    boolean hasNext() {
      skipUnprintable();
      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();
    }

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

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

    double[] nextDoubleArray(int n) {
      double[] array = new double[n];
      for (int i = 0; i < n; i++) {
        array[i] = nextDouble();
      }
      return array;
    }

    double[][] nextDoubleMap(int n, int m) {
      double[][] map = new double[n][];
      for (int i = 0; i < n; i++) {
        map[i] = nextDoubleArray(m);
      }
      return map;
    }

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

    public int[] nextIntArray(int n) {
      int[] array = new int[n];
      for (int i = 0; i < n; i++) {
        array[i] = nextInt();
      }
      return array;
    }
  }
}