Problem:
Score : 1600 points

Problem Statement

There are N zeros and M ones written on a blackboard.
Starting from this state, we will repeat the following operation: select K of the rational numbers written on the blackboard and erase them, then write a new number on the blackboard that is equal to the arithmetic mean of those K numbers.
Here, assume that N + M - 1 is divisible by K - 1.

Then, if we repeat this operation until it is no longer applicable, there will be eventually one rational number left on the blackboard.

Find the number of the different possible values taken by this rational number, modulo 10^9 + 7.

Constraints

1 ≦ N, M ≦ 2000

2 ≦ K ≦ 2000

N + M - 1 is divisible by K - 1.

Input

The input is given from Standard Input in the following format:

N M K

Output

Print the number of the different possible values taken by the rational number that will be eventually left on the blackboard, modulo 10^9 + 7.

Sample Input 1

2 2 2

Sample Output 1

5

There are five possible values for the number that will be eventually left on the blackboard: \frac{1}{4}, \frac{3}{8}, \frac{1}{2}, \frac{5}{8} and \frac{3}{4}.

For example, \frac{3}{8} can be eventually left if we:

Erase 0 and 1, then write \frac{1}{2}.

Erase \frac{1}{2} and 1, then write \frac{3}{4}.

Erase 0 and \frac{3}{4}, then write \frac{3}{8}.

Sample Input 2

3 4 3

Sample Output 2

9

Sample Input 3

150 150 14

Sample Output 3

937426930

Solution in C++14 (GCC 5.4.1) (judge verdict: Accepted)

#include <cstdio>

const int maxn = 4020;
const int mod = 1e9 + 7;

int n, m, k, ans;
int dp[maxn][maxn], sum[maxn];

int S(int l, int r) {
	return l <= 0 ? sum[r] : (sum[r] - sum[l - 1] + mod) % mod;
}

int main() {
	scanf("%d%d%d", &n, &m, &k);
	dp[0][0] = 1;
	--k;
	for (int i = 0; i < n + m;) {
		for (int j = 0; j <= i; ++j) {
			sum[j] = dp[i - j][j];
		}
		for (int j = 1; j <= i; ++j) {
			(sum[j] += sum[j - 1]) %= mod;
		}
		if(i + k >= n + m) {
			break;
		}
		for (int j = i + 1; j <= i + k; ++j) {
			sum[j] = sum[j - 1];
		}
		i += k;
		for (int j = 0; j <= i; ++j) {
			dp[i - j][j] = S(j - k, j);
		}
	}
	for (int i = 0; i < n; ++i) {
		for (int j = 0; j < m; ++j) {
			if(dp[i][j]) {
				int x = (n - i) % k, y = (m - j) % k;
				if(!x) {
					x += k;
				}
				if(!y) {
					y += k;
				}
				while(x + y < k + 1) {
					if(x + i + k <= n) {
						x += k;
					}
					else {
						y += k;
					}
				}
				if(x + y == k + 1 && x + i <= n && y + j <= m) {
					(ans += dp[i][j]) %= mod;
				}
			}
		}
	}
	printf("%d\n", ans);
	return 0;
}