• API
• FAQ
• Tools
• Archive
daily pastebin goal
56%
SHARE
TWEET

a guest Oct 9th, 2014 203 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. <?php
2.
4.
5. function convolve (\$a,\$b) {
6.         \$c = array();
7.
8.         foreach (array_keys(\$a) as \$ka) {
9.                 foreach (array_keys(\$b) as \$kb) {
10.                         \$c[\$ka] = \$c[\$kb] = \$c[\$ka+\$kb] = 1;
11.
12.                 }
13.         }
14.
15.         return \$c;
16.
17. }
18.
19. if (\$argc != 3) die ("uso: \$argv[0] N k\nN = número de dados, k = número de caras\n");
20.
21. \$N = intval(\$argv[1]);
22. \$k = intval(\$argv[2]);
23.
24. if (\$N < 1 || \$k < 1) die ("menos lobos\n");
25.
26. // 1 - calcular todos los valores que un dado único puede generar
27. // son todos los valores del dado X todas las posibles potencias de 10
28. // ie. k * N valores
29.
30. \$dice_gen = array(); // usado como hash
31.
32. for (\$exp = 0, \$mul = 1; \$exp != \$N; \$exp++, \$mul *= 10) {
33.         for (\$die_face = 1; \$die_face <= \$k; \$die_face++) {
34.                 \$dice_gen[\$die_face * \$mul] = 1;
35.         }
36. }
37.
38.
39. \$result = \$dice_gen; // primer dado
40.
41. for (\$die = 1; \$die != \$N; \$die++) {
42.         \$result = convolve(\$result,\$dice_gen);
43. }
44.
45. for (\$n = 1; isset(\$result[\$n]); \$n++);
46.
47. echo "\$n\n";
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.

Top