読者です 読者をやめる 読者になる 読者になる

Codeforces Round #369 Div2 E. ZS and The Birthday Paradox

競技プログラミング Codeforces

問題

Problem - E - Codeforces

1 年は 2^n 日であるとする。k 人の人に誕生日を聞いた時、誕生日が同じ人が入る確率を求めよ。

解法

http://mayokoex.hatenablog.com/entry/2016/08/30/083738

  • MOD 以上の連続した区間の総乗の剰余を取ると 0 になる。
  • 2^{n-k} を求めるとき、2.modPow(n) * 2.modInverse().modPow(k) で求めることができる。

コード

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

/*
                   _ooOoo_
                  o8888888o
                  88" . "88
                  (| -_- |)
                  O\  =  /O
               ____/`---'\____
             .'  \\|     |//  `.
            /  \\|||  :  |||//  \
           /  _||||| -:- |||||-  \
           |   | \\\  -  /// |   |
           | \_|  ''\---/''  |   |
           \  .-\__  `-`  ___/-. /
         ___`. .'  /--.--\  `. . __
      ."" '<  `.___\_<|>_/___.'  >'"".
     | | :  `- \`.;`\ _ /`;.`/ - ` : | |
     \  \ `-.   \_ __\ /__ _/   .-` /  /
======`-.____`-.___\_____/___.-`____.-'======
                   `=---='
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
            pass System Test!
*/

public class E {
  private static class Task {
    final int MOD = (int) 1e6 + 3;

    long modPow(long x, long e) {
      long ret = 1;
      long cur = x;
      while (e > 0) {
        if ((e & 1) != 0) ret = (ret * cur) % MOD;
        cur = (cur * cur) % MOD;
        e /= 2;
      }
      return ret;
    }

    void solve(FastScanner in, PrintWriter out) {
      long N = in.nextLong();
      long K = in.nextLong();

      if (N <= 60 && (1L << N) < K) {
        out.println("1 1");
        return;
      }

      if (K < MOD) {
        long[] modPow = new long[62];
        Arrays.fill(modPow, -1);
        long up = 1;
        long down = 1;
        for (int i = 1; i < K; i++) {
          int cnt = Integer.numberOfTrailingZeros(i);
          long j = i >> cnt;
          if (modPow[cnt] < 0) modPow[cnt] = modPow(2, N - cnt);
          up *= (modPow[cnt] - j + MOD) % MOD;
          up %= MOD;
          down *= modPow[cnt];
          down %= MOD;
        }
        up = (down - up + MOD) % MOD;
        out.println(up + " " + down);
        return;
      }

      long count = N;
      long k = K - 1;
      while (k > 0) {
        count += k / 2;
        k /= 2;
      }
      BigInteger two = BigInteger.valueOf(2);
      BigInteger mod = BigInteger.valueOf(MOD);
      BigInteger up = two.modPow(BigInteger.valueOf(N), mod);
      up = up.modPow(BigInteger.valueOf(K), mod);
      up = up.multiply((two.modInverse(mod)).modPow(BigInteger.valueOf(count), mod));
      up = up.mod(mod);
      out.println(up.toString() + " " + up.toString());
    }
  }

  /**
   * ここから下はテンプレートです。
   */
  public static void main(String[] args) {
    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;
    }

    public long[] nextLongArray(int n) {
      long[] array = new long[n];
      for (int i = 0; i < n; i++) array[i] = nextLong();
      return array;
    }

    public String[] nextStringArray(int n) {
      String[] array = new String[n];
      for (int i = 0; i < n; i++) array[i] = next();
      return array;
    }

    public char[][] nextCharMap(int n) {
      char[][] array = new char[n][];
      for (int i = 0; i < n; i++) array[i] = next().toCharArray();
      return array;
    }
  }
}