/home/toolbox/public_html/solutions/5/530/a.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <sys/types.h>
4 #include <sys/stat.h>
5 #include <fcntl.h>
6 #include <stdint.h>
7 #include <math.h>
8 #include <stdlib.h>
9
10 #define TRUE (1 == 1)
11 #define FALSE (1 != 1)
12
13 #define DEBUG if (FALSE)
14
15 /*
16 * Author: Isaac Traxler
17 * Date: 2015-03-24
18 * Purpose:
19 * Problem: 530
20 */
21
22 /*
23 * This template reads data until a terminating value is reached.
24 */
25
26 int n;
27 int k;
28
29 void init()
30 {
31 /* FUNCTION init */
32 } /* FUNCTION init */
33
34 void dump()
35 {
36 /* FUNCTION dump */
37 } /* FUNCTION dump */
38
39 int getInput()
40 {
41 /* FUNCTION getInput */
42 int dataReadFlag;
43
44 scanf(" %d %d ", &n, &k);
45 dataReadFlag = (n != 0);
46 return (dataReadFlag);
47 } /* FUNCTION getInput */
48
49 /* n choose k is
50 n!
51 ---------
52 k! (n-k)!
53
54 (n-k+1)*(n-k+2)***(n)
55 ---------------------
56 k!
57
58 (n-k+1)*(n-k+2)***(n)
59 ---------------------
60 1*2*3***k
61
62 product(i) (n-k+i) / i
63 */
64 unsigned long long choose(int n, int k)
65 {
66 /* FUNCTION choose */
67 int i;
68 unsigned long long tot = 1;
69 int tmp;
70
71 tmp = n - k;
72 for (i=1; i<=k; i++)
73 {
74 /* for */
75 tot = (tot * (tmp + i)) / i;
76 } /* for */
77 return tot;
78 } /* FUNCTION choose */
79
80 void process()
81 {
82 /* FUNCTION process */
83 unsigned long long num;
84 if ((0 == k) || (n== k))
85 {
86 printf("1\n");
87 }
88 else if (1 == k)
89 {
90 printf("%d\n", n);
91 }
92 else
93 {
94 /* work to do */
95 /* since k or n-k can be cancelled, send the smaller */
96 if ((n-k) < k)
97 num = choose(n, n-k);
98 else
99 num = choose(n, k);
100 DEBUG printf("%d choose %d: ", n, k);
101 printf("%llu\n", num);
102 } /* work to do */
103
104 } /* FUNCTION process */
105
106 int main()
107 {
108 /* main */
109 int moreToDo;
110
111 init();
112 moreToDo = getInput();
113 while (moreToDo)
114 {
115 /* while */
116 process();
117 moreToDo = getInput();
118 } /* while */
119
120 return EXIT_SUCCESS;
121 } /* main */
122