/* * n 種のガチャポンなり食玩なりを * m 回でコンプリートする確率 */ #include #include #include #include static int verbose; static int *mark; static long long ok; static long long total; static void oain(int lv, int v[], int ntypes, int times) { if (lv == 0) { int i, j; int sum = 0; for (i = 0; i < ntypes; i++) mark[i] = 0; for (j = 0; j < times; j++) mark[v[j]] = 1; for (i = 0; i < ntypes; i++) sum += mark[i]; if (sum == ntypes) { ok++; if (verbose) { for (i = 0; i < times; i++) { printf("%d", v[i]); } printf("\n"); } } total++; return; } else { int i; for (i = 0; i < ntypes; i++) { v[times - lv] = i; oain(lv - 1, v, ntypes, times); } } }/* oain */ static int nain(int ntypes, int times) { int err = -1; int *v = malloc(sizeof(*v) * times); int vbyte = (sizeof(*v) * times); mark = malloc(sizeof(*mark) * ntypes); if (v != NULL && mark != NULL) { memset(v, '\0', vbyte); oain(times, v, ntypes, times); printf("%lld/%lld=%.16g\n", ok, total, (double)ok / total); err = 0; } else { perror("malloc"); } return err; } int main(int argc, char *argv[]) { int ex = 1, ch, show_usage = 0; while ((ch = getopt(argc, argv, "vV")) != EOF) { switch (ch) { default: case 'V': show_usage++; break; case 'v': verbose++; break; }/* switch */ }/* while */ if (argc - optind != 2 || show_usage) { fprintf(stderr, "usage: %s [-v] n m\nCalculate probability of complete n types by m times.\n", argv[0]); } else { int ntype = strtol(argv[optind], NULL, 0); int times = strtol(argv[optind + 1], NULL, 0); ex = nain(ntype, times) < 0; } return ex; }/* main */