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<cstring>
#include<algorithm>

using namespace std;

const long long mod=1000003;

long long dp[1010][1010][2];
long long sum[1010][1010][2];
int N,K;

long long gcd(long long a,long long b){
	if(a<b) return gcd(b,a);
	if(b==0) return a;
	return gcd(b,a%b);
}

long long get(int rot){
	//asm K<N
	long long res=0;
	int all=gcd(N,rot);
	for(int l=1;l<=min(all-1,K);l++){
		long long cur=sum[all-l][all-l][0];
		cur*=l;
		cur%=mod;
		res+=cur;
		res%=mod;
	}
	return (res*2)%mod;
}

long long solve(){
	memset(dp,0,sizeof(dp));
	memset(sum,0,sizeof(sum));
	dp[1][1][0]=1;
	for(int i=1;i<N;i++){
		for(int j=1;j<=min(i,K);j++) for(int k=0;k<=1;k++){
			if(j+1<=K){
				dp[i+1][j+1][k]+=dp[i][j][k];
				dp[i+1][j+1][k]%=mod;
			}
			dp[i+1][1][k^1]+=dp[i][j][k];
			dp[i+1][1][k^1]%=mod;
		}
	}
	for(int i=1;i<=N;i++){
		for(int j=1;j<=N;j++) for(int k=0;k<=1;k++){
			sum[i][j][k]=sum[i][j-1][k]+dp[i][j][k];
			sum[i][j][k]%=mod;
		}
	}
	long long ans=0;
	for(int l=0;l<N;l++){
		ans+=get(l);
		ans%=mod;
	}
	if(K>=N) ans+=N*2;
	while(true){
		if(ans%N==0){
			ans/=N;
			break;
		}else{
			ans+=mod;
		}
	}
	ans%=mod;
	ans+=mod;
	ans%=mod;
	return ans;
}

int main(){
	while(true){
		scanf("%d%d",&N,&K);
		if(N==0&&K==0) break;
		long long ans=solve();
		printf("%lld\n",ans);
	}
	return 0;
}