#!/bin/sh # n 種のガチャポンなり食玩なりを # m 回でコンプリートする確率 # http://aquarius10.cse.kyutech.ac.jp/~otabe/shokugan/ awk ' function combination(n, r, i, x, y) { x = y = 1; for (i = 0; i < r; i++) { x *= n--; y *= (i + 1); } return x / y; } function ipow(x, n, i, y) { y = 1; for (i = 0; i < n; i++) { y *= x; } return y; } function nn_p(ntypes, times, i, sign, sigma) { sign = (ntypes % 2 == 0) ? -1 : 1; sigma = 0; for (i = 0; i < ntypes; i++) { sigma += sign * combination(ntypes - 1, i) * ipow((i + 1) / ntypes, times - 1); sign = sign < 0 ? 1 : -1; } return sigma; } BEGIN { myname = ENVIRON["_"]; if (ARGC != 3) { printf("usage: %s n m\nCalculate probability of complete n types by m times.\n", myname) > "/dev/stderr"; } else { ntypes = ARGV[1]; times = ARGV[2]; printf("%d %d = %.13g\n", ntypes, times, nn_p(ntypes, times)); } }' "$@"