about summary refs log tree commit diff
path: root/codechef/maxprodu.c
blob: f9ebfff2672f65138208a300e3fd5d69132acc09 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <stdio.h>

int main()
{
	long long n, k, si, m, l, r, i, result;
	int t;
	scanf("%d\n", &t);

	while (t--) {
		scanf("%lld %lld\n", &n, &k);
		si = (k - 1) * k / 2;
		m = (n - si) / k;
		if (m < 1) {
			puts("-1");
			continue;
		} else if (m == 1) {
			puts("0");
			continue;
		}

		r = n - si - m * k;
		l = k - r;
		result = 1;

		for (i = 0; l--; i++) {
			result = result * (m + i) % 1000000007;
			result = result * (m + i - 1) % 1000000007;
		}
		for (; r--; i++) {
			result = result * (m + i) % 1000000007;
			result = result * (m + i + 1) % 1000000007;
		}

		printf("%lld\n", result);
	}

	return 0;
}