Problem:
Problem J: Revenge of the Round Table

Two contries A and B have decided to make a meeting to get acquainted with each other. n ambassadors
from A and B will attend the meeting in total.

A round table is prepared for in the meeting. The ambassadors are getting seated at the round table, but
they have agreed that more than k ambassadors from the same country does not sit down at the round
table in a row for deeper exchange.

Your task is to write a program that reports the number of possible arrangements when rotations are not
counted. Your program should report the number modulo M = 1000003.

Let us provide an example. Suppose n = 4 and k = 2. When rotations are counted as different arrangements, the following six arrangements are possible.

AABB
ABBA
BBAA
BAAB
ABAB
BABA

However, when rotations are regarded as same, the following two arrangements are possible.

AABB
ABAB

Therefore the program should report 2.

Input

The input consists of multiple datasets. Each dataset consists of two integers n (1 ≤ n ≤ 1000) and k (1 ≤ k ≤ 1000) in one line.

It does not always hold k < n. This means the program should consider cases in which the ambassadors
from only one country attend the meeting.

The last dataset is followed by a line containing two zeros. This line is not a part of any dataset and should not be processed.

Output

For each dataset, print the number of possible arrangements modulo M = 1000003 in one line.

Sample Input

3 1
3 2
3 3
4 2
10 5
1000 500
0 0

Output for the Sample Input

0
2
4
2
90
570682

Solution in C++ (judge verdict: Accepted)

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <assert.h>
using namespace std;

#ifdef ONLINE_JUDGE
#define ep(...) ;
#else
#define ep(...) fprintf(stderr, __VA_ARGS__)
#endif

typedef long long i64;

const int MOD = (int) 1e6 + 3;
const int MAXN = 1003;

int gcd(int a, int b) {
	if (! b) return a;
	return gcd(b, a % b);
}

int pow_mod(i64 a, int b, int p) {
	i64 ret = 1;
	for (; b; b >>= 1) {
		if (b & 1) ret = ret * a % p;
		a = a  * a % p;
	}
	return ret;
}

int n, k;
int f[MAXN][2][MAXN];
int sum[MAXN];

inline void addIt(int &a, int b) {
	a += b;
	if (a >= MOD) a -= MOD;
}

void calcf() {
	for (int i = 1; i <= n; i ++)
		for (int num = 0; num < 2; num ++)
			fill(f[i][num], f[i][num] + k + 1, 0);
	f[1][0][1] = 1;
	for (int i = 1; i < n; i ++)
		for (int num = 0; num < 2; num ++)
			for (int len = 1; len <= k; len ++) {
				int &cur = f[i][num][len];
				if (! cur) continue;
				if (len < k) 
					addIt(f[i + 1][num][len + 1], cur);
				addIt(f[i + 1][num ^ 1][1], cur);
			}
	for (int i = 1; i <= n; i ++) {
		int &cur = sum[i];
		cur = 0;
		for (int j = 1; j <= k; j ++)
			addIt(cur, f[i][0][j]);
	}
}

int calc(int m) {
	int ret = 0;
	for (int len = 1, _len = min(m, k); len <= _len; len ++) {
		addIt(ret, (i64) sum[m - len] * len * 2 % MOD);
	}
	if (k >= n) addIt(ret, 2);
	return ret;
}

int main() {
#ifndef ONLINE_JUDGE
#endif
	for (int cases = 1; true; cases ++) {
		scanf("%d%d", &n, &k);
		if (n == 0 && k == 0) break;
		calcf();
		int ans = 0;
		for (int i = 0; i < n; i ++) {
			int d = gcd(n, i);
			addIt(ans, calc(d));
		}
		printf("%d\n", (int) ((i64) ans * pow_mod(n, MOD - 2, MOD) % MOD));
	}

	return 0;
}