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)

// AOJ 2164 Revenge of the Round Table
// 2018.3.22 bal4u

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

//#define getchar_unlocked()  getchar()
int in()
{
	int n = 0, c = getchar_unlocked();
	do n = 10*n + (c & 0xf), c = getchar_unlocked(); while (c >= '0');
	return n;
}

int gcd(int a, int b)
{
	int r;
	while (b != 0) r = a % b, a = b, b = r;
	return a;
}

#define M 1000003

int n, k;
int inv[1002];
int dpa[1002][1002], dpb[1002][1002], dp[1002];

void calc()
{
	int i, j, t, sa, sb;

    memset(dpa, 0, sizeof(dpa));
    memset(dpb, 0, sizeof(dpb));
    memset(dp, 0, sizeof(dp));

    sa = 1, sb = 0;
    dpa[1][1] = 1;
    dpb[1][1] = 0;
 
    for (i = 2; i <= n; i++) {
        dpa[i][1] = sb, dpb[i][1] = sa;
		t = sa, sa = sb, sb = t;
        for (j = 2; j <= k; j++) {
            dpa[i][j] = dpa[i-1][j-1];
            sa = sa + dpa[i][j]; if (sa >= M) sa -= M;
            dpb[i][j] = dpb[i-1][j-1];
            sb = sb + dpb[i][j]; if (sb >= M) sb -= M;
        }
    }

    for (i = 1; i <= n; i++) for (j = 1; j <= k; j++) {
		dp[i] += dpb[i][j]; if (dp[i] >= M) dp[i] -= M;
	}
 
    for (i = 1; i <= n; i++) for (j = 1; j < k; j++) {
		dpb[i][j+1] += dpb[i][j]; 
		if (dpb[i][j+1] >= M) dpb[i][j+1] -= M;
    }
 
    for (i = 1; i <= n; i++) {
		t = i; if (t > k) t = k; 
        for (j = 1; j <= t; j++) {
            dp[i] += dpb[i-j][k-j]; if (dp[i] >= M) dp[i] -= M;
        }
    }
}

int main()
{
	int s, i;
	long long ans;

    inv[1] = 1; for (i = 2; i <= 1000; i++)
		inv[i] = M - (long long)(M/i) * inv[M%i] % M;

	while (n = in()) {
		k = in();

		s = 0;
	    if (k >= n) k = n-1, s = 2;
		calc();
		ans = 0; for (i = 0; i < n; i++) ans = (ans + (dp[gcd(i, n)] << 1)) % M;
        printf("%d\n", (int)((s + ans * inv[n]) % M));
	}
	return 0;
}