I would be really grateful to you guys if you could let me know of some method to reduce the execution time.
limit time : 0.5sec
Algorithm Question : I want to bridge the site in the west to the site in the east. (At this point, only one bridge can be connected to one site.) Because I try to build as many bridges as possible, I try to build as many (N) bridges as possible as the number of sites in the west. bridges cannot overlap each other. At this point, write a program to figure out how many cases you can build a bridge.
Input : The first line of the input is given the number of test cases, 'T'. From the next line, each test case is given an integer (0 < N ≤ M < 30) for the number of sites west and east of the river.
Output : For each test case, print the number of cases in which the bridge can be built under the given conditions.
Example :
Input Output
3
2 2 1
1 5 5
13 29 67863915
Here is my code :
#include <stdio.h>
int combination(int n, int r) {
if (n == r || r == 0) return 1;
else return combination(n - 1, r - 1) + combination(n - 1, r);
}
int main(void)
{
int Tcase;
int N, M;
scanf("%d", &Tcase);
for (int i = 0; i < Tcase; i++) {
int total;
scanf("%d %d", &N, &M);
if (M - N == 0)
total = 1;
else
total = combination(M, N);
printf("%d\n", total);
}
return 0;
}