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

Submitted solutions:

code_snippet: #include <bits/stdc++.h>
using namespace std;

const int MOD = 1000003;

class modint {
private:
	int x;
public:
	modint() : x(0) {}
	modint(long long n) : x(n >= 0 ? n % MOD : (MOD - (-n) % MOD) % MOD) {}
	int get() const { return x; }
	modint& operator+=(const modint& m) { x = (x + m.x) % MOD; return *this; }
	modint& operator-=(const modint& m) { x = (x - m.x + MOD) % MOD; return *this; }
	modint& operator*=(const modint& m) { x = 1LL * x * m.x % MOD; return *this; }
	modint operator+(const modint& m) { return modint(*this) += m; }
	modint operator-(const modint& m) { return modint(*this) -= m; }
	modint operator*(const modint& m) { return modint(*this) *= m; }
	modint pow(long long b) const {
		modint a(*this);
		modint res(1);
		while (b >= 1) {
			if (b % 2 == 1) {
				res *= a;
			}
			a *= a;
			b /= 2;
		}
		return res;
	}
	modint inv() const {
		return pow(MOD - 2);
	}
};

modint solve(int N, int K) {
	// step #1. compute euler's totient function
	vector<int> euler(N + 1);
	for (int i = 1; i <= N; i++) {
		euler[i] = i - 1;
	}
	for (int i = 2; i <= N; i++) {
		if (euler[i] == i - 1) {
			for (int j = i * 2; j <= N; j += i) {
				euler[j] -= euler[j] / i;
			}
		}
	}
	euler[1] = 1;

	// step #2. compute mod inverses
	vector<modint> invs(N + 1);
	for (int i = 1; i <= N; i++) {
		invs[i] = modint(i).inv();
	}

	// step #3. dynamic programming
	modint ans = 0;
	vector<modint> dp(N + 1);
	dp[0] = 1;
	for (int i = 1; i <= N; i++) {
		vector<modint> ndp(N + 1);
		modint s = 0;
		for (int j = i - 1; j <= N; j++) {
			ndp[j] = s;
			s += dp[j];
			if (j >= K) {
				s -= dp[j - K];
			}
		}
		dp = ndp;
		if (i % 2 == 0) {
			for (int j = i; j <= N; j++) {
				if (N % j == 0) {
					ans += dp[j] * euler[N / j] * invs[(i / 2) * (N / j)];
				}
			}
		}
	}

	// step #4. consider cases of "all A" or "all B"
	if (K >= N) {
		ans += 2;
	}

	return ans;
}

int main() {
	while (true) {
		int N, K;
		cin >> N >> K;
		if (N == 0 && K == 0) {
			break;
		}
		modint ans = solve(N, K);
		cout << ans.get() << endl;
	}
	return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 3360
final_rank: 3

code_snippet: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define mp make_pair
#define si(x) int(x.size())
const int mod=1000000007,MAX=1005,INF=1<<30;

//modintのみ

// from: https://gist.github.com/yosupo06/ddd51afb727600fd95d9d8ad6c3c80c9
// (based on AtCoder STL)

#include <cassert>
#include <numeric>
#include <type_traits>

namespace atcoder {
    
    namespace internal {
        
#ifndef _MSC_VER
        template <class T>
        using is_signed_int128 =
        typename std::conditional<std::is_same<T, __int128_t>::value ||
        std::is_same<T, __int128>::value,
        std::true_type,
        std::false_type>::type;
        
        template <class T>
        using is_unsigned_int128 =
        typename std::conditional<std::is_same<T, __uint128_t>::value ||
        std::is_same<T, unsigned __int128>::value,
        std::true_type,
        std::false_type>::type;
        
        template <class T>
        using make_unsigned_int128 =
        typename std::conditional<std::is_same<T, __int128_t>::value,
        __uint128_t,
        unsigned __int128>;
        
        template <class T>
        using is_integral = typename std::conditional<std::is_integral<T>::value ||
        is_signed_int128<T>::value ||
        is_unsigned_int128<T>::value,
        std::true_type,
        std::false_type>::type;
        
        template <class T>
        using is_signed_int = typename std::conditional<(is_integral<T>::value &&
                                                         std::is_signed<T>::value) ||
        is_signed_int128<T>::value,
        std::true_type,
        std::false_type>::type;
        
        template <class T>
        using is_unsigned_int =
        typename std::conditional<(is_integral<T>::value &&
                                   std::is_unsigned<T>::value) ||
        is_unsigned_int128<T>::value,
        std::true_type,
        std::false_type>::type;
        
        template <class T>
        using to_unsigned = typename std::conditional<
        is_signed_int128<T>::value,
        make_unsigned_int128<T>,
        typename std::conditional<std::is_signed<T>::value,
        std::make_unsigned<T>,
        std::common_type<T>>::type>::type;
        
#else
        
        template <class T> using is_integral = typename std::is_integral<T>;
        
        template <class T>
        using is_signed_int =
        typename std::conditional<is_integral<T>::value && std::is_signed<T>::value,
        std::true_type,
        std::false_type>::type;
        
        template <class T>
        using is_unsigned_int =
        typename std::conditional<is_integral<T>::value &&
        std::is_unsigned<T>::value,
        std::true_type,
        std::false_type>::type;
        
        template <class T>
        using to_unsigned = typename std::conditional<is_signed_int<T>::value,
        std::make_unsigned<T>,
        std::common_type<T>>::type;
        
#endif
        
        template <class T>
        using is_signed_int_t = std::enable_if_t<is_signed_int<T>::value>;
        
        template <class T>
        using is_unsigned_int_t = std::enable_if_t<is_unsigned_int<T>::value>;
        
        template <class T> using to_unsigned_t = typename to_unsigned<T>::type;
        
    }  // namespace internal
    
}  // namespace atcoder

#include <utility>

namespace atcoder {
    
    namespace internal {
        
        constexpr long long safe_mod(long long x, long long m) {
            x %= m;
            if (x < 0) x += m;
            return x;
        }
        
        struct barrett {
            unsigned int _m;
            unsigned long long im;
            
            barrett(unsigned int m) : _m(m), im((unsigned long long)(-1) / m + 1) {}
            
            unsigned int umod() const { return _m; }
            
            unsigned int mul(unsigned int a, unsigned int b) const {
                
                unsigned long long z = a;
                z *= b;
#ifdef _MSC_VER
                unsigned long long x;
                _umul128(z, im, &x);
#else
                unsigned long long x =
                (unsigned long long)(((unsigned __int128)(z)*im) >> 64);
#endif
                unsigned int v = (unsigned int)(z - x * _m);
                if (_m <= v) v += _m;
                return v;
            }
        };
        
        constexpr long long pow_mod_constexpr(long long x, long long n, int m) {
            if (m == 1) return 0;
            unsigned int _m = (unsigned int)(m);
            unsigned long long r = 1;
            unsigned long long y = safe_mod(x, m);
            while (n) {
                if (n & 1) r = (r * y) % _m;
                y = (y * y) % _m;
                n >>= 1;
            }
            return r;
        }
        
        constexpr bool is_prime_constexpr(int n) {
            if (n <= 1) return false;
            if (n == 2 || n == 7 || n == 61) return true;
            if (n % 2 == 0) return false;
            long long d = n - 1;
            while (d % 2 == 0) d /= 2;
            for (long long a : {2, 7, 61}) {
                long long t = d;
                long long y = pow_mod_constexpr(a, t, n);
                while (t != n - 1 && y != 1 && y != n - 1) {
                    y = y * y % n;
                    t <<= 1;
                }
                if (y != n - 1 && t % 2 == 0) {
                    return false;
                }
            }
            return true;
        }
        template <int n> constexpr bool is_prime = is_prime_constexpr(n);
        
        constexpr std::pair<long long, long long> inv_gcd(long long a, long long b) {
            a = safe_mod(a, b);
            if (a == 0) return {b, 0};
            
            long long s = b, t = a;
            long long m0 = 0, m1 = 1;
            
            while (t) {
                long long u = s / t;
                s -= t * u;
                m0 -= m1 * u;  // |m1 * u| <= |m1| * s <= b
                
                
                auto tmp = s;
                s = t;
                t = tmp;
                tmp = m0;
                m0 = m1;
                m1 = tmp;
            }
            if (m0 < 0) m0 += b / s;
            return {s, m0};
        }
        
        constexpr int primitive_root_constexpr(int m) {
            if (m == 2) return 1;
            if (m == 167772161) return 3;
            if (m == 469762049) return 3;
            if (m == 754974721) return 11;
            if (m == 998244353) return 3;
            int divs[20] = {};
            divs[0] = 2;
            int cnt = 1;
            int x = (m - 1) / 2;
            while (x % 2 == 0) x /= 2;
            for (int i = 3; (long long)(i)*i <= x; i += 2) {
                if (x % i == 0) {
                    divs[cnt++] = i;
                    while (x % i == 0) {
                        x /= i;
                    }
                }
            }
            if (x > 1) {
                divs[cnt++] = x;
            }
            for (int g = 2;; g++) {
                bool ok = true;
                for (int i = 0; i < cnt; i++) {
                    if (pow_mod_constexpr(g, (m - 1) / divs[i], m) == 1) {
                        ok = false;
                        break;
                    }
                }
                if (ok) return g;
            }
        }
        template <int m> constexpr int primitive_root = primitive_root_constexpr(m);
        
    }  // namespace internal
    
}  // namespace atcoder

#include <cassert>
#include <numeric>
#include <type_traits>

#ifdef _MSC_VER
#include <intrin.h>
#endif

namespace atcoder {
    
    namespace internal {
        
        struct modint_base {};
        struct static_modint_base : modint_base {};
        
        template <class T> using is_modint = std::is_base_of<modint_base, T>;
        template <class T> using is_modint_t = std::enable_if_t<is_modint<T>::value>;
        
    }  // namespace internal
    
    template <int m, std::enable_if_t<(1 <= m)>* = nullptr>
    struct static_modint : internal::static_modint_base {
        using mint = static_modint;
        
    public:
        static constexpr int mod() { return m; }
        static mint raw(int v) {
            mint x;
            x._v = v;
            return x;
        }
        
        static_modint() : _v(0) {}
        template <class T, internal::is_signed_int_t<T>* = nullptr>
        static_modint(T v) {
            long long x = (long long)(v % (long long)(umod()));
            if (x < 0) x += umod();
            _v = (unsigned int)(x);
        }
        template <class T, internal::is_unsigned_int_t<T>* = nullptr>
        static_modint(T v) {
            _v = (unsigned int)(v % umod());
        }
        static_modint(bool v) { _v = ((unsigned int)(v) % umod()); }
        
        unsigned int val() const { return _v; }
        
        mint& operator++() {
            _v++;
            if (_v == umod()) _v = 0;
            return *this;
        }
        mint& operator--() {
            if (_v == 0) _v = umod();
            _v--;
            return *this;
        }
        mint operator++(int) {
            mint result = *this;
            ++*this;
            return result;
        }
        mint operator--(int) {
            mint result = *this;
            --*this;
            return result;
        }
        
        mint& operator+=(const mint& rhs) {
            _v += rhs._v;
            if (_v >= umod()) _v -= umod();
            return *this;
        }
        mint& operator-=(const mint& rhs) {
            _v -= rhs._v;
            if (_v >= umod()) _v += umod();
            return *this;
        }
        mint& operator*=(const mint& rhs) {
            unsigned long long z = _v;
            z *= rhs._v;
            _v = (unsigned int)(z % umod());
            return *this;
        }
        mint& operator/=(const mint& rhs) { return *this = *this * rhs.inv(); }
        
        mint operator+() const { return *this; }
        mint operator-() const { return mint() - *this; }
        
        mint pow(long long n) const {
            assert(0 <= n);
            mint x = *this, r = 1;
            while (n) {
                if (n & 1) r *= x;
                x *= x;
                n >>= 1;
            }
            return r;
        }
        mint inv() const {
            if (prime) {
                assert(_v);
                return pow(umod() - 2);
            } else {
                auto eg = internal::inv_gcd(_v, m);
                assert(eg.first == 1);
                return eg.second;
            }
        }
        
        friend mint operator+(const mint& lhs, const mint& rhs) {
            return mint(lhs) += rhs;
        }
        friend mint operator-(const mint& lhs, const mint& rhs) {
            return mint(lhs) -= rhs;
        }
        friend mint operator*(const mint& lhs, const mint& rhs) {
            return mint(lhs) *= rhs;
        }
        friend mint operator/(const mint& lhs, const mint& rhs) {
            return mint(lhs) /= rhs;
        }
        friend bool operator==(const mint& lhs, const mint& rhs) {
            return lhs._v == rhs._v;
        }
        friend bool operator!=(const mint& lhs, const mint& rhs) {
            return lhs._v != rhs._v;
        }
        
    private:
        unsigned int _v;
        static constexpr unsigned int umod() { return m; }
        static constexpr bool prime = internal::is_prime<m>;
    };
    
    template <int id> struct dynamic_modint : internal::modint_base {
        using mint = dynamic_modint;
        
    public:
        static int mod() { return (int)(bt.umod()); }
        static void set_mod(int m) {
            assert(1 <= m);
            bt = internal::barrett(m);
        }
        static mint raw(int v) {
            mint x;
            x._v = v;
            return x;
        }
        
        dynamic_modint() : _v(0) {}
        template <class T, internal::is_signed_int_t<T>* = nullptr>
        dynamic_modint(T v) {
            long long x = (long long)(v % (long long)(mod()));
            if (x < 0) x += mod();
            _v = (unsigned int)(x);
        }
        template <class T, internal::is_unsigned_int_t<T>* = nullptr>
        dynamic_modint(T v) {
            _v = (unsigned int)(v % mod());
        }
        dynamic_modint(bool v) { _v = ((unsigned int)(v) % mod()); }
        
        unsigned int val() const { return _v; }
        
        mint& operator++() {
            _v++;
            if (_v == umod()) _v = 0;
            return *this;
        }
        mint& operator--() {
            if (_v == 0) _v = umod();
            _v--;
            return *this;
        }
        mint operator++(int) {
            mint result = *this;
            ++*this;
            return result;
        }
        mint operator--(int) {
            mint result = *this;
            --*this;
            return result;
        }
        
        mint& operator+=(const mint& rhs) {
            _v += rhs._v;
            if (_v >= umod()) _v -= umod();
            return *this;
        }
        mint& operator-=(const mint& rhs) {
            _v += mod() - rhs._v;
            if (_v >= umod()) _v -= umod();
            return *this;
        }
        mint& operator*=(const mint& rhs) {
            _v = bt.mul(_v, rhs._v);
            return *this;
        }
        mint& operator/=(const mint& rhs) { return *this = *this * rhs.inv(); }
        
        mint operator+() const { return *this; }
        mint operator-() const { return mint() - *this; }
        
        mint pow(long long n) const {
            assert(0 <= n);
            mint x = *this, r = 1;
            while (n) {
                if (n & 1) r *= x;
                x *= x;
                n >>= 1;
            }
            return r;
        }
        mint inv() const {
            auto eg = internal::inv_gcd(_v, mod());
            assert(eg.first == 1);
            return eg.second;
        }
        
        friend mint operator+(const mint& lhs, const mint& rhs) {
            return mint(lhs) += rhs;
        }
        friend mint operator-(const mint& lhs, const mint& rhs) {
            return mint(lhs) -= rhs;
        }
        friend mint operator*(const mint& lhs, const mint& rhs) {
            return mint(lhs) *= rhs;
        }
        friend mint operator/(const mint& lhs, const mint& rhs) {
            return mint(lhs) /= rhs;
        }
        friend bool operator==(const mint& lhs, const mint& rhs) {
            return lhs._v == rhs._v;
        }
        friend bool operator!=(const mint& lhs, const mint& rhs) {
            return lhs._v != rhs._v;
        }
        
    private:
        unsigned int _v;
        static internal::barrett bt;
        static unsigned int umod() { return bt.umod(); }
    };
    template <int id> internal::barrett dynamic_modint<id>::bt = 998244353;
    
    using modint998244353 = static_modint<998244353>;
    using modint1000000007 = static_modint<1000000007>;
    using modint = dynamic_modint<-1>;
    
    namespace internal {
        
        template <class T>
        using is_static_modint = std::is_base_of<internal::static_modint_base, T>;
        
        template <class T>
        using is_static_modint_t = std::enable_if_t<is_static_modint<T>::value>;
        
        template <class> struct is_dynamic_modint : public std::false_type {};
        template <int id>
        struct is_dynamic_modint<dynamic_modint<id>> : public std::true_type {};
        
        template <class T>
        using is_dynamic_modint_t = std::enable_if_t<is_dynamic_modint<T>::value>;
        
    }  // namespace internal
    
}  // namespace atcoder

using mint=atcoder::modint;

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

mint dp[MAX][MAX][2];

int main(){
    
    std::ifstream in("text.txt");
    std::cin.rdbuf(in.rdbuf());
    cin.tie(0);
    ios::sync_with_stdio(false);
    
    mint::set_mod(1000003);
    
    while(1){
        int N,K;cin>>N>>K;
        if(N==0) break;
        mint ans=0;
        
        for(int t=1;t<=N;t++){
            ll g=gcd(t,N);
            if(g==1){
                if(K>=N) ans++;
                continue;
            }
            for(int i=0;i<=g;i++) for(int j=0;j<=K;j++) dp[i][j][0]=dp[i][j][1]=0;
            for(int i=1;i<=g;i++){
                if(i<=K){
                    if(i<g||N<=K) dp[i][i][0]=1;
                }
                for(int j=1;j<=K;j++){
                    for(int f=0;f<2;f++){
                        if(i<g||f){
                            dp[i][j][f]+=dp[i-1][j][f^1];
                            if(K+1<i) dp[i][j][f]-=dp[i-(K+1)][j][f^1];
                        }else{
                            dp[i][j][f]+=dp[i-1][j][f^1];
                            if((K-j)+1<i) dp[i][j][f]-=dp[i-((K-j)+1)][j][f^1];
                        }
                        
                        if(i<g) dp[i][j][f]+=dp[i-1][j][f];
                    }
                }
            }
            
            for(int j=1;j<=K;j++) for(int f=0;f<2;f++) ans+=dp[g][j][f];
        }
        
        ans*=2;
        ans/=N;
        
        cout<<ans.val()<<"\n";
    }
}
accuracy: 1
time_ms: 240
memory_kb: 11248
final_rank: 10

code_snippet: #pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<unordered_set>
#include<utility>
#include<cassert>
#include<complex>
#include<numeric>
#include<array>
using namespace std;

//#define int long long
typedef long long ll;

typedef unsigned long long ul;
typedef unsigned int ui;
constexpr ll mod = 1000003;
const ll INF = mod * mod;
typedef pair<int, int>P;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
#define all(v) (v).begin(),(v).end()
typedef pair<ll, ll> LP;
typedef double ld;
typedef pair<ld, ld> LDP;
const ld eps = 1e-8;
const ld pi = acosl(-1.0);

ll mod_pow(ll x, ll n, ll m = mod) {
	if (n < 0) {
		ll res = mod_pow(x, -n, m);
		return mod_pow(res, m - 2, m);
	}
	if (abs(x) >= m)x %= m;
	if (x < 0)x += m;
	ll res = 1;
	while (n) {
		if (n & 1)res = res * x % m;
		x = x * x % m; n >>= 1;
	}
	return res;
}
struct modint {
	ll n;
	modint() :n(0) { ; }
	modint(ll m) :n(m) {
		if (n >= mod)n %= mod;
		else if (n < 0)n = (n % mod + mod) % mod;
	}
	operator int() { return n; }
};
bool operator==(modint a, modint b) { return a.n == b.n; }
modint operator+=(modint& a, modint b) { a.n += b.n; if (a.n >= mod)a.n -= mod; return a; }
modint operator-=(modint& a, modint b) { a.n -= b.n; if (a.n < 0)a.n += mod; return a; }
modint operator*=(modint& a, modint b) { a.n = ((ll)a.n * b.n) % mod; return a; }
modint operator+(modint a, modint b) { return a += b; }
modint operator-(modint a, modint b) { return a -= b; }
modint operator*(modint a, modint b) { return a *= b; }
modint operator^(modint a, ll n) {
	if (n == 0)return modint(1);
	modint res = (a * a) ^ (n / 2);
	if (n % 2)res = res * a;
	return res;
}

ll inv(ll a, ll p) {
	return (a == 1 ? 1 : (1 - p * inv(p % a, a)) / a + p);
}
modint operator/(modint a, modint b) { return a * modint(inv(b, mod)); }
modint operator/=(modint& a, modint b) { a = a / b; return a; }
const int max_n = 1 << 11;
modint fact[max_n], factinv[max_n];
void init_f() {
	fact[0] = modint(1);
	for (int i = 0; i < max_n - 1; i++) {
		fact[i + 1] = fact[i] * modint(i + 1);
	}
	factinv[max_n - 1] = modint(1) / fact[max_n - 1];
	for (int i = max_n - 2; i >= 0; i--) {
		factinv[i] = factinv[i + 1] * modint(i + 1);
	}
}
modint comb(int a, int b) {
	if (a < 0 || b < 0 || a < b)return 0;
	return fact[a] * factinv[b] * factinv[a - b];
}
modint combP(int a, int b) {
	if (a < 0 || b < 0 || a < b)return 0;
	return fact[a] * factinv[a - b];
}

bool isp[1005];
int meb[1005];
void init() {
	fill(isp + 2, isp + 1005, true);
	fill(meb + 1, meb + 1005, 1);
	for (int i = 2; i < 1005; i++) {
		if (!isp[i])continue;
		meb[i] *= -1;
		for (int j = 2*i; j < 1005; j += i) {
			isp[j] = false;
			meb[j] *= -1;
			if ((j / i) % i == 0)meb[j] = 0;
		}
	}
}

int n, k;
void solve() {
	modint ans = 0;
	vector<int> ds;
	for (int i = 1; i <= n; i++)if (n % i == 0)ds.push_back(i);
	vector<modint> ori(ds.size());
	/*{
		vector<modint> dp(k + 1);
		vector<modint> cop(k + 1);
		dp[1] = 2;
		rep(i, n - 1) {
			fill(all(cop), 0);
			rep1(j, k) {
				//another
				cop[1] += dp[j];
				//same
				if (j + 1 <= k) {
					cop[j + 1] += dp[j];
				}
			}
			swap(dp, cop);
		}
		rep1(j, k)ori[0] += dp[j];
	}*/
	vector<modint> mem(n + 1);
	vector<vector<modint>> dp(k + 1, vector<modint>(2));
	dp[1][0] = 1;
	mem[1] = 1;
	auto cop = dp;
	for (int i = 2; i <= n; i++) {
		rep1(j, k)rep(t, 2)cop[j][t] = 0;
		rep1(j, k)rep(t, 2) {
			//same
			if (j + 1 <= k) {
				cop[j + 1][t] += dp[j][t];
			}
			//another
			cop[1][t ^ 1] += dp[j][t];
		}
		swap(dp, cop);
		rep1(j, k)mem[i] += dp[j][0];
	}
	for (int i = 0; i < ds.size(); i++) {
		if (k >= n) {
			ori[i] += 2;
		}
		int b = n / ds[i];
		for (int cl = 1; cl < b; cl++) {
			for (int cr = 0; cr < b; cr++) {
				if (cl + cr >= b||cl+cr>k)break;
				ori[i] += (modint)2 * mem[b - (cl + cr)];
			}
		}
	}
	rep(i, ds.size()) {
		//cout << ds[i] << " " << ori[i] << "\n";
		modint sum = 0;
		for (int j = i; j < ds.size(); j++) {
			if (ds[j] % ds[i] == 0) {
				sum += (modint)meb[ds[j] / ds[i]] * ori[j];
			}
		}
		sum /= (modint)(n / ds[i]);
		ans += sum;
	}
	cout << ans << "\n";
}

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	//cout << fixed << setprecision(10);
	init_f();
	init();
	//expr();
	//int t; cin >> t;rep(i, t)
	while (cin >> n >> k, n)solve();
	return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 3536
final_rank: 4

code_snippet: //#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#define rep(i, n) for(int i=0; i<n; ++i)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
using namespace std;
using ll = int64_t;
using ld = long double;
using P = pair<int, int>;
using vs = vector<string>;
using vi = vector<int>;
using vvi = vector<vi>;
template<class T> using PQ = priority_queue<T>;
template<class T> using PQG = priority_queue<T, vector<T>, greater<T> >;
const int INF = 0xccccccc;
const ll LINF = 0xcccccccccccccccLL;
template<typename T1, typename T2>
inline bool chmax(T1 &a, T2 b) {return a < b && (a = b, true);}
template<typename T1, typename T2>
inline bool chmin(T1 &a, T2 b) {return a > b && (a = b, true);}
template<typename T1, typename T2>
istream &operator>>(istream &is, pair<T1, T2> &p) { return is >> p.first >> p.second;}
template<typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) { return os << p.first << ' ' << p.second;}

constexpr unsigned mod = 1000003U;

struct mint {
  unsigned int x;
  mint():x(0) {}
  mint(int64_t x_) {
    int64_t v = int64_t(x_ % mod);
    if(v < 0) v += mod;
    x = (unsigned int)v;
  }
  static mint row(int v) {
    mint v_;
    v_.x = v;
    return v_;
  }
  mint operator-() const { return mint(-int64_t(x));}
  mint& operator+=(const mint a) {
    if ((x += a.x) >= mod) x -= mod;
    return *this;
  }
  mint& operator-=(const mint a) {
    if ((x += mod-a.x) >= mod) x -= mod;
    return *this;
  }
  mint& operator*=(const mint a) {
    uint64_t z = x;
    z *= a.x;
    x = (unsigned int)(z % mod);
    return *this;
  }
  mint operator+(const mint a) const { return mint(*this) += a;}
  mint operator-(const mint a) const { return mint(*this) -= a;}
  mint operator*(const mint a) const { return mint(*this) *= a;}
  friend bool operator==(const mint &a, const mint &b) {return a.x == b.x;}
  friend bool operator!=(const mint &a, const mint &b) {return a.x != b.x;}
  mint &operator++() {
    x++;
    if(x == mod) x = 0;
    return *this;
  }
  mint &operator--() {
    if(x == 0) x = mod;
    x--;
    return *this;
  }
  mint operator++(int) {
    mint result = *this;
    ++*this;
    return result;
  }
  mint operator--(int) {
    mint result = *this;
    --*this;
    return result;
  }
  mint pow(int64_t t) const {
    mint x_ = *this, r = 1;
    while(t) {
      if(t&1) r *= x_;
      x_ *= x_;
      t >>= 1;
    }
    return r;
  }
  //for prime mod
  mint inv() const { return pow(mod-2);}
  mint& operator/=(const mint a) { return *this *= a.inv();}
  mint operator/(const mint a) {return mint(*this) /= a;}
};

istream& operator>>(istream& is, mint& a) { return is >> a.x;}
ostream& operator<<(ostream& os, const mint& a) { return os << a.x;}

string to_string(mint a) {
  return to_string(a.x);
}

//head



int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, k;
  while(cin >> n >> k, n) {
    vector<vector<mint>> dp(n+1, vector<mint>(k+1)), dp1(n+1, vector<mint>(k+1));
    vector<mint> pref(n+1), sum(n+1);
    dp[0][0] = 1;
    rep(i, n) {
      rep(j, k) {
        dp1[i+1][0] += dp[i][j];
        dp[i+1][0] += dp1[i][j];
        dp[i+1][j+1] += dp[i][j];
        dp1[i+1][j+1] += dp1[i][j];
      }
      pref[i+1] = pref[i] + dp1[i][0];
    }
    rep(i, n) {
      sum[i+1] += sum[i] + pref[i+1];
      if(i+1 < k) continue;
      sum[i+1] -= pref[i+1-k];
      if(i < k) continue;
      sum[i+1] -= dp1[i-k][0]*k;
    }
    mint ans;
    for(int i = 1; i <= n; i++) {
      int g = gcd(i, n);
      ans += sum[g]*2;
    }
    ans /= n;
    cout << ans + (n <= k?2:0) << '\n';
  }
}
accuracy: 1
time_ms: 20
memory_kb: 10964
final_rank: 5

code_snippet: /*
与Necklace_of_Beads如出一辙。
*/ 
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <utility>
#include <queue>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ldouble;
const int LEN = 100000;

struct fastio {
  int it, len;
  char s[LEN + 5];
  fastio() {
    it = len = 0;
  }
  char get() {
    if (it < len) return s[it++];
    it = 0, len = fread(s, 1, LEN, stdin);
    return len ? s[it++] : EOF;
  }
  bool notend() {
    char c;
    for (c = get(); c == ' ' || c == '\n' || c == '\r'; c = get());
    if (it) it--;
    return c != EOF;
  }
  void put(char c) {
    if (it == LEN) fwrite(s,1,LEN,stdout), it = 0;
    s[it++] = c;
  }
  void flush() {
    fwrite(s, 1, it, stdout);
  }
}buff, bufo;
inline int getint() {
  char c; int res = 0, sig = 1;
  for (c = buff.get(); c < '0' || c > '9'; c = buff.get()) if(c == '-') sig = -1;
  for (; c >= '0' && c <= '9'; c = buff.get()) res = res * 10 + (c - '0');
  return sig * res;
}
inline ll getll() {
  char c; ll res = 0, sig = 1;
  for (c = buff.get(); c < '0' || c > '9'; c = buff.get()) if (c == '-') sig = -1;
  for (; c >= '0' && c <= '9'; c = buff.get()) res = res * 10 + (c - '0');
  return sig * res;
}
inline void putint(int x, char suf) {
  if (!x) bufo.put('0');
  else {
    if (x < 0) bufo.put('-'), x = -x;
    int k = 0; char s[15];
    while (x) {
      s[++k] = x % 10 + '0';
      x /= 10;
    }
    for (; k; k--) bufo.put(s[k]);
  }
  bufo.put(suf);
}
inline void putll(ll x, char suf) {
  if (!x) bufo.put('0');
  else {
    if (x < 0) bufo.put('-'), x = -x;
    int k = 0; char s[25];
    while (x) {
      s[++k] = x % 10 + '0';
      x /= 10;
    }
    for (; k; k--) bufo.put(s[k]);
  }
  bufo.put(suf);
}
inline char get_char() {
  char c;
  for (c = buff.get(); c == ' ' || c == '\n' || c == '\r'; c = buff.get());
  return c;
}

#define maxn 1005
const ll mod = 1000003;

ll modpow(ll x, ll y) {
  ll res = 1;
  while (y) {
    if (y & 1) (res *= x) %= mod;
    y >>= 1;
    (x *= x) %= mod;
  }
  return res;
}

int n, k;
ll pow2[maxn], f[maxn], g[maxn][2], ans;

void solve() {
  if (n == 1) {
    puts("2");
    return;
  }
  ans = 0;
  if (k >= n) {
    ans += n * 2;
    k = n - 1;
  }
  memset(g, 0, sizeof(g));
  g[0][0] = 2;
  for (int i = 1; i <= n; i++) {
    for (int j = max(0, i - k); j < i; j++) {
      (g[i][0] += g[j][1]) %= mod;
      (g[i][1] += g[j][0]) %= mod;
    }
  }
  for (int i = 1; i <= k; i++) f[i] = pow2[i] - 2;
  for (int i = k + 1; i <= n; i++) {
    f[i] = g[i][0];
    for (int j = 1; j < k; j++) {
      (f[n] += j * g[n - j - 1][1]) %= mod;
    }
  }
  for (int i = 0; i < n; i++) {
    (ans += f[__gcd(i, n)]) %= mod;
  }
  printf("%lld\n", ans * modpow(n, mod - 2) % mod);
}

int main() {
  pow2[0] = 1;
  for (int i = 1; i <= 1000; i++) pow2[i] = (pow2[i - 1] << 1) % mod;
  while (n = getint(), k = getint()) solve();
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3156
final_rank: 1

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define inc(i, l, r) for (int i = l; i <= r; i++)

const int maxn = 1005;
const int mod = 1000003;

int n, k;
int dp_a[maxn][maxn], dp_b[maxn][maxn];  //一定以a开头,总长为i, 结尾是j个a/b
int sum_a[maxn], sum_b[maxn];

inline ll ksm(ll a, ll n) {
    ll r = 1;
    while (n) {
        if (n & 1) r = r * a % mod;
        a = a * a % mod;
        n >>= 1;
    }
    return r;
}

int main() {
    while (cin >> n >> k && n) {
        memset(dp_a, 0, sizeof(dp_a));
        memset(dp_b, 0, sizeof(dp_b));
        dp_a[1][1] = 1;
        inc(i, 1, n - 1) {
            inc(j, 1, min(i, k)) {
                if (j + 1 <= k)
                    dp_a[i + 1][j + 1] =
                        (dp_a[i + 1][j + 1] + dp_a[i][j]) % mod;
                dp_b[i + 1][1] = (dp_b[i + 1][1] + dp_a[i][j]) % mod;
            }
            inc(j, 1, min(i - 1, k)) {
                dp_a[i + 1][1] = (dp_a[i + 1][1] + dp_b[i][j]) % mod;
                if (j + 1 <= k)
                    dp_b[i + 1][j + 1] =
                        (dp_b[i + 1][j + 1] + dp_b[i][j]) % mod;
            }
        }

        memset(sum_a, 0, sizeof(sum_a));
        memset(sum_b, 0, sizeof(sum_b));
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= min(i, k); j++) {
                sum_a[i] = (sum_a[i] + dp_a[i][j]) % mod;
                sum_b[i] = (sum_b[i] + dp_b[i][j]) % mod;
            }
        }
        ll res = 0;
        for (int i = 1; i <= n; i++) {
            int l = __gcd(i, n);
            //循环节长度
            for (int j = 2; j <= min(k, l - 1); j++) {
                //首尾a之和
                res = (res + (j - 1) * sum_a[l - j]) % mod;
            }
            //结尾b
            res = (res + sum_b[l]) % mod;
            if (k >= n) res++;  //全a
        }
        printf("%lld\n", res * 2 * ksm((ll)n, mod - 2LL) % mod);
    }
}
accuracy: 1
time_ms: 40
memory_kb: 11116
final_rank: 6

code_snippet: #include <bits/stdc++.h>
using namespace std;

using ll = long long;

constexpr int mod = 1000003;

ll inv(ll n) {
    ll a = n, b = mod, u = 1, v = 0;
    while(b) {
        ll t = a / b;
        a -= t * b; swap(a, b);
        u -= t * v; swap(u, v);
    }
    u %= mod;
    if(u < 0) u += mod;
    return u;
}

int main() {
    int n, k;
    while(cin >> n >> k, n) {
        if(n == 1) {
            cout << 2 << endl;
            continue;
        }
        ll ans = 0;
        if(k >= n) {
            ans += 2;
            k = n - 1;
        }

        vector<vector<vector<int>>> dp1(n + 1, vector<vector<int>>(k + 1, vector<int>(2)));
        dp1[1][1][0] = 1; // start with A
        for(int i = 1; i < n; ++i) {
            for(int j = 1; j <= k; ++j) {
                for(int pre = 0; pre < 2; ++pre) {
                    if(j + 1 <= k) {
                        (dp1[i + 1][j + 1][pre] += dp1[i][j][pre]) %= mod;
                    }
                    (dp1[i + 1][1][!pre] += dp1[i][j][pre]) %= mod;
                }
            }
        }

        vector<ll> dp2(n + 1), sum(n + 1);
        for(int i = 1; i <= n; ++i) {
            for(int j = 0; j <= k; ++j) {
                (dp2[i] += dp1[i][j][1]) %= mod; // ends with B
                (sum[i] += dp1[i][j][0]) %= mod;
            }
            for(int loop = 2; loop <= min(k, i); ++loop) {
                (dp2[i] += 1LL * sum[i - loop] * (loop - 1)) %= mod;
            }
            (dp2[i] += dp2[i]) %= mod; // consider starting with B
        }

        vector<int> dp3(n + 1);
        for(int i = 1; i <= n; ++i) {
            dp3[i] = dp2[i];
            for(int j = 1; j < i; ++j) {
                if(i % j == 0) {
                    (dp3[i] += mod - dp3[j]) %= mod;
                }
            }
        }
        for(int i = 1; i <= n; ++i) {
            if(n % i != 0) continue;
            (ans += dp3[i] * inv(i)) %= mod;
        }

        cout << ans << endl;
    }
}
accuracy: 1
time_ms: 340
memory_kb: 57700
final_rank: 18

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>
#include <cstdio>
#include <set>
#include <cstring>
#include <queue>
#include <map>
#include <list>
#include <cmath>
#include <iomanip>
#define INF 0x3f3f3f3f
#define EPS 10e-5
#define mod 1000003

const int dx[]={-1,0,0,1,0},dy[]={0,1,-1,0,0};
using namespace std;
int n,k,nmod,kl;
int dpa[1005][1005],dpb[1005][1005];
int dp[1005];

int gcd(int a,int b){
    return b?gcd(b,a%b):a;
}


int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);

   while(scanf("%d%d",&n,&k)){
        if(n==0&&k==0)break;
        memset(dpa,0,sizeof(dpa));
        memset(dpb,0,sizeof(dpb));
        memset(dp,0,sizeof(dp));
        int all = 0;
        if(k>=n){
            k = n-1;
            all = 2;
        }
        nmod = n * mod;

        int asum = 1;
        int bsum = 0;

        dpa[1][1] = 1;
        dpb[1][1] = 0;

        for(int i=2;i<=n;++i){
            dpa[i][1] = bsum;
            dpb[i][1] = asum;
            swap(asum,bsum);
            for(int j=2;j<=k;++j){
                dpa[i][j] = dpa[i-1][j-1];
                asum += dpa[i][j];
                asum %= nmod;
                dpb[i][j] = dpb[i-1][j-1];
                bsum += dpb[i][j];
                bsum %= nmod;
            }
        }

        for(int i=1;i<=n;++i){
            for(int j=1;j<=k;++j){
                dp[i] += dpb[i][j];
                dp[i] %= nmod;
            }
        }

        for(int i=1;i<=n;++i){
            for(int j=1;j<k;++j){
                dpb[i][j+1] += dpb[i][j];
                dpb[i][j+1] %= nmod;
            }
        }

        for(int i=1;i<=n;++i){
            for(int p = 1;p<=k&&p<=i;++p){
                dp[i] += dpb[i-p][k-p];
                dp[i] %= nmod;
            }
        }

        long long cnt = 0;

        for(int i=0;i<n;++i){
            cnt += dp[gcd(n,i)];
            cnt %= nmod;
        }

        printf("%lld\n",(2*cnt/n+all)%mod);
    }
    return 0;
}
accuracy: 1
time_ms: 150
memory_kb: 11140
final_rank: 8

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

long long dp[1009][1009][2], dp2[1009], mod = 1000003;

long long modpow(long long a, long long b, long long m) {
	long long p = 1, q = a;
	for (int i = 0; i < 30; i++) {
		if ((b / (1LL << i)) % 2 == 1) { p *= q; p %= m; }
		q *= q; q %= m;
	}
	return p;
}
long long Div(long long a, long long b, long long m) {
	return (a * modpow(b, m - 2, m)) % m;
}

long long solve(int N, int K) {
	K = min(K, N);
	for (int i = 0; i < 1009; i++) { for (int j = 0; j < 1009; j++) { dp[i][j][0] = 0; dp[i][j][1] = 0; } }

	dp[0][0][1] = 1;
	dp[1][1][1] = 1; // 最初が A と仮定しているので、A -> B
	for (int i = 1; i < N; i++) {
		for (int j = 1; j <= K; j++) {
			if (dp[i][j][0] >= 1) {
				if (j < K) { dp[i + 1][j + 1][0] += dp[i][j][0]; if (dp[i + 1][j + 1][0] >= mod) dp[i + 1][j + 1][0] -= mod; }
				dp[i + 1][1][1] += dp[i][j][0]; if (dp[i + 1][1][1] >= mod) dp[i + 1][1][1] -= mod;
			}
			if (dp[i][j][1] >= 1) {
				if (j < K) { dp[i + 1][j + 1][1] += dp[i][j][1]; if (dp[i + 1][j + 1][1] >= mod) dp[i + 1][j + 1][1] -= mod; }
				dp[i + 1][1][0] += dp[i][j][1]; if (dp[i + 1][1][0] >= mod) dp[i + 1][1][0] -= mod;
			}
		}
	}

	long long ans = 0;
	for (int i = 1; i <= K; i++) {
		// i 回連続の場合
		for (int j = 0; j <= K; j++) ans += dp[N - i][j][1];
		for (int j = 0; j <= K - i; j++) ans += dp[N - i][j][0];
		ans %= mod;
	}
	return (ans * 2) % mod;
}

long long val[1009];

long long calc(int N, int K) {
	if (N < K) K = N;
	for (int i = 0; i < 1009; i++) val[i] = 0;
	vector<int>vec; for (int i = 1; i <= N; i++) { if (N%i == 0) vec.push_back(i); }

	for (int i = 0; i < vec.size(); i++) {
		val[i] = solve(vec[i], K);
		if (vec[i] <= K && K < N) val[i] -= 2;
		for (int j = 0; j < i; j++) { if (vec[i] % vec[j] == 0) val[i] -= val[j]; }
		val[i] += 100000000LL * mod; val[i] %= mod;
	}

	long long sum = 0;
	for (int i = 0; i < vec.size(); i++) {
		sum += Div(val[i], vec[i], mod); sum %= mod;
	}
	return sum;
}

int main() {
	while (true) {
		long long N, K;
		cin >> N >> K; if (N + K == 0) break;
		cout << calc(N, K) << endl;
	}
	return 0;
}
accuracy: 1
time_ms: 660
memory_kb: 19024
final_rank: 17

code_snippet: #include<bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 99999999999999999
//#define MOD 1000000007
#define EPS 0.000000001
using namespace std;


#define NUM 1005

enum Type{
	finish_same,
	finish_another,
};

ll N,K;
ll const MOD = 1000003;
ll dp1[2][NUM][NUM];
ll dp2[NUM];
ll dp3[NUM];
ll fact[NUM];

ll extgcd(ll a,ll b,ll &x,ll &y){
	ll d = a;
	if(b != 0){
		d = extgcd(b,a%b,y,x);
		y -= (a/b)*x;
	}else{
		x = 1;
		y = 0;
	}
	return d;
}

ll mod_inverse(ll a,ll m){
    ll x,y;
    extgcd(a,m,x,y);
    return (m+x%m)%m;
}

ll mod_fact(ll n,ll p,ll &e){
	e = 0;
	if(n == 0)return 1;

	int res = mod_fact(n/p,p,e);
	e += n/p;

	if(n/p%2 != 0)return res*(p-fact[n%p])%p;
	return res*fact[n%p]%p;
}


void init_dp(){

	for(int i = 0; i <= N; i++){
		dp2[i] = 0;
		dp3[i] = 0;
		for(int k = 0; k <= N; k++){
			dp1[finish_same][i][k] = 0;
			dp1[finish_another][i][k] = 0;
		}
	}
}

void func(){

	ll ans = 0;

	if(K >= N){
		K = N-1;
		ans = 2;
	}

	init_dp();

	dp1[finish_same][1][1] = 1;
	dp1[finish_another][1][1] = 0;

	for(int len = 2; len <= N; len++){

		for(int i = 1; i <= K; i++){
			dp1[finish_another][len][1] += dp1[finish_same][len-1][i];
			dp1[finish_another][len][1] %= MOD;
		}
		for(int i = 2; i <= K; i++){
			dp1[finish_another][len][i] += dp1[finish_another][len-1][i-1];
			dp1[finish_another][len][i] %= MOD;
		}

		for(int i = 1; i <= K; i++){
			dp1[finish_same][len][1] += dp1[finish_another][len-1][i];
			dp1[finish_same][len][1] %= MOD;
		}
		for(int i = 2; i <= K; i++){
			dp1[finish_same][len][i] += dp1[finish_same][len-1][i-1];
			dp1[finish_same][len][i] %= MOD;
		}
	}

	for(int len = 1; len <= N; len++){

		for(int i = 1; i <= K; i++){
			dp2[len] += dp1[finish_another][len][i];
			dp2[len] %= MOD;
		}
	}

	for(int len = 1; len <= N; len++){
		for(int i = 1; i < K; i++){
			dp1[finish_another][len][i+1] += dp1[finish_another][len][i];
			dp1[finish_another][len][i+1] %= MOD;
		}
	}

	for(ll len = 1; len <= N; len++){
		for(int p = 1; p <= min(len,K); p++){
			dp2[len] += dp1[finish_another][len-p][K-p];
			dp2[len] %= MOD;
		}
	}

	for(int len = 1; len <= N; len++){
		dp3[len] = (2*dp2[len])%MOD;
		for(int i = 2; i < len; i++){
			if(len%i != 0)continue;

			dp3[len] -= dp3[i];
			dp3[len] = (dp3[len]+MOD)%MOD;
		}
	}


	for(int cycle_len = 1; cycle_len <= N; cycle_len++){
		if(N%cycle_len != 0)continue;

		ans += dp3[cycle_len]*mod_inverse(cycle_len,MOD);
		ans %= MOD;
	}

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

int main(){

	fact[0] = 1;
	for(ll i = 1; i < NUM; i++){
		fact[i] = i*fact[i-1];
		fact[i] %= MOD;
	}

	while(true){
		scanf("%lld %lld",&N,&K);
		if(N == 0 && K == 0)break;

		func();
	}

	return 0;
}
accuracy: 1
time_ms: 140
memory_kb: 18876
final_rank: 11

code_snippet: #include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<queue>
#include<stack>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<cmath>
#include<sstream>
#include<fstream>
using namespace std;
const int MAXN=1011;
const double pi=acos(-1.0);
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef double dbl;
const dbl eps = 1e-10;
const int MOD =  1000003;
#define For(a,b,c) for (int (a)=(b);(a)<(c);(a)++)
#define foreach(iter,V) for (__typeof((V.begin()) iter=(V).begin();iter!=(V).end();iter++)
#define mp make_pair
#define fi first
#define se second
#define pb push_back
ll dp_a[MAXN][MAXN],dp_b[MAXN][MAXN],dp[MAXN];
int n,k;
ll INV;
int gcd(int a,int b){
	return (b == 0 ? a : gcd(b,a % b));
}
ll qpow(int a,int b){
	ll res = 1;
	ll base = a;
	while(b){
		if (b & 1)	res = (res * base) % MOD;
		base = (base * base) % MOD;
		b >>= 1;
	}
	return res;
}
int all;
inline void init(){
	INV = qpow(n,MOD - 2);
	if (k >= n)	{k = n - 1;all = 2;}
	memset(dp_a,0,sizeof(dp_a));
	memset(dp_b,0,sizeof(dp_b));
	int asum,bsum;
	dp_a[1][1] = 1;
	dp_b[1][1] = 0;
	asum = 1;
	bsum = 0;
	For(i,2,n + 1){
		dp_a[i][1] = bsum;
		dp_b[i][1] = asum;
		swap(asum,bsum);
		For(j,2,k + 1){
			dp_a[i][j] = dp_a[i - 1][j - 1];
			asum = (asum + dp_a[i][j]) % MOD;
			dp_b[i][j] = dp_b[i - 1][j - 1];
			bsum = (bsum + dp_b[i][j]) % MOD;
		}
	}
}
int main(){ 
//	freopen("in.txt","r",stdin);
	while(~scanf("%d%d",&n,&k) && (n || k)){
		all = 0;
	init();
	For(i,1,n + 1){
		dp[i] = 0;
		For(j,1,k + 1)	dp[i] = (dp[i] + dp_b[i][j]) % MOD;
		For(j,1,k)	dp_b[i][j + 1] = (dp_b[i][j + 1] + dp_b[i][j]) % MOD;
		For(p,1,min(k,i) + 1)	dp[i] = (dp[i] + dp_b[i - p][k - p]) % MOD;
	}
	int ans = 0;
	For(i,0,n)
		ans = (ans + dp[gcd(n,i)] * 2) % MOD;
	printf("%lld\n",(ll)(ans * INV + all ) % MOD);
	}
	return 0;
}
accuracy: 1
time_ms: 100
memory_kb: 19100
final_rank: 9

code_snippet: #include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;

typedef long long ll;

const int MAX_N = 1000;
const int MOD = 1000003;

// input
int n, k;

int dp_a[MAX_N + 1][MAX_N + 1];
int dp_b[MAX_N + 1][MAX_N + 1];
int dp[MAX_N + 1];
ll inv[MAX_N + 1];

void init_inv()
{
  inv[1] = 1;
  for (int i = 2; i <= MAX_N; i++)
    inv[i] = (MOD - (MOD / i) * inv[MOD % i] % MOD) % MOD;
}

int gcd(int a, int b)
{
  if (b > a)
    swap(a, b);

  return b == 0 ? a : gcd(b, a % b);
}

void solve()
{
  ll ans = 0, all = 0;

  memset(dp_a, 0, sizeof dp_a);
  memset(dp_b, 0, sizeof dp_b);
  memset(dp, 0, sizeof dp);

  int a_sum = 1, b_sum = 0;

  if (k >= n)
  {
    k = n - 1;
    all = 2;
  }
  dp_a[1][1] = 1;
  dp_b[1][1] = 0;

  for (int i = 2; i <= n; i++)
  {
    dp_a[i][1] = b_sum;
    dp_b[i][1] = a_sum;
    swap(a_sum, b_sum);
    for (int j = 2; j <= k; j++)
    {
      dp_a[i][j] = dp_a[i - 1][j - 1];
      a_sum = (a_sum + dp_a[i][j]) % MOD;

      dp_b[i][j] = dp_b[i - 1][j - 1];
      b_sum = (b_sum + dp_b[i][j]) % MOD;
    }
  }

  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= k; j++)
      dp[i] = (dp[i] + dp_b[i][j]) % MOD;

  for (int i = 1; i <= n; i++)
    for (int j = 1; j < k; j++)
      dp_b[i][j + 1] = (dp_b[i][j + 1] + dp_b[i][j]) % MOD;

  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= min(i, k); j++)
      dp[i] = (dp[i] + dp_b[i - j][k - j]) % MOD;

  for (int i = 0; i < n; i++)
    ans = (ans + 2 * dp[gcd(i, n)]) % MOD;

  ans = (ans * inv[n]) % MOD;

  printf("%lld\n", (ans + all) % MOD);
}

int main()
{
  init_inv();
  
  while (scanf("%d%d", &n, &k) != EOF)
  {
    if (n == 0 && k == 0)
      break;

    solve();
  }
  return 0;
}
accuracy: 1
time_ms: 80
memory_kb: 10520
final_rank: 7

code_snippet: #include<bits/stdc++.h>
using namespace std;
using Int = long long;

Int mod_pow(Int x,Int y,Int mod){
  Int res = 1;
  while(y){
    if(y&1) res = res * x % mod;
    x = x * x % mod;
    y /= 2;
  }
  return res;
}

Int extgcd(Int a,Int b,Int &x,Int &y){
  Int d = a;
  if(b != 0){
    d = extgcd(b, a%b, y,x);
    y -= (a/b) * x;
  } else{
    x = 1, y = 0;
  }
  return d;
}

Int mod_inverse(Int a,Int mod){
  Int x,y;
  extgcd(a,mod,x,y);
  return (mod+x%mod)%mod;
}

const Int MOD = 1e6+3;
const Int MAX_P = 200000;
Int fact[MAX_P],inv[MAX_P],finv[MAX_P];
  
void init(Int mod){
  fact[0] = 1;
  for(Int i=1;i<MAX_P;i++)
    fact[i] = (fact[i-1]*i)%mod;

    inv[1] = 1;
    for(Int i=2;i<MAX_P;i++)
      inv[i] = inv[mod%i]*(mod-mod/i)%mod;

    
    finv[0] = 1;
    for(Int i=1;i<MAX_P;i++)
      finv[i] = finv[i-1] * inv[i]%mod;
}

Int mod_comb3(Int n,Int k,Int mod){
  if(k < 0|| k > n) return 0;
  return fact[n] * finv[k] % mod * finv[n-k]%mod;
}

const Int MAX = 2020;
vector<vector<Int> > ds(MAX);

Int dp[MAX][MAX];
Int memo[MAX][MAX];

Int dfs(Int p,Int n){
  Int &res=memo[p][n];
  if(~res) return res;

  res=dp[p][n];
 
  for(Int l:ds[n]){
    Int a=(n/l);
    if(p%a) continue;
    res+=MOD-dfs(p/a,l)*(p/a)%MOD;
  }
  
  res%=MOD;
  res*=mod_inverse(p,MOD);
  res%=MOD;
  return res;
}

signed main(){  
  cin.tie(0);
  ios::sync_with_stdio(0);
  for(Int i=1;i<MAX;i++)
    for(Int j=i+i;j<MAX;j+=i)
      ds[j].emplace_back(i);

  init(MOD);
  
  Int n,k;
  while(cin>>n>>k,n||k){

    vector<Int> co(MAX,0);
    for(Int i=1;i<=k;i++)
      for(Int j=1;j<=k;j++)
	co[i+j]++;
    
    memset(dp,0,sizeof(dp));
    dp[0][0]=1;
    for(Int p=0;p<n;p++){
      for(Int i=0;i<=n;i++){
	dp[p][i]%=MOD;
	//cout<<p<<":"<<i<<":"<<dp[p][i]<<endl;
	if(!dp[p][i]) continue;       
	for(Int a=2;i+a<=n;a++){
	  dp[p+1][i+a]+=dp[p][i]*co[a]%MOD;
	}
      }
    }
    
    memset(memo,-1,sizeof(memo));
    Int ans=0;
    for(Int i=0;i<=n;i++) dfs(i,n);
    if(n<=k) ans+=2;
    
    for(Int p=0;p<=n;p++)
      for(Int i=0;i<=n;i++)
	if(~memo[p][i]) ans+=memo[p][i];
    
    ans%=MOD;
    cout<<ans<<endl;
  }
  
  return 0;
}
accuracy: 1
time_ms: 1100
memory_kb: 71904
final_rank: 20

code_snippet: #include<bits/stdc++.h>
using namespace std;
using Int = long long;

Int mod_pow(Int x,Int y,Int mod){
  Int res = 1;
  while(y){
    if(y&1) res = res * x % mod;
    x = x * x % mod;
    y /= 2;
  }
  return res;
}

Int extgcd(Int a,Int b,Int &x,Int &y){
  Int d = a;
  if(b != 0){
    d = extgcd(b, a%b, y,x);
    y -= (a/b) * x;
  } else{
    x = 1, y = 0;
  }
  return d;
}

Int mod_inverse(Int a,Int mod){
  Int x,y;
  extgcd(a,mod,x,y);
  return (mod+x%mod)%mod;
}

const Int MOD = 1e6+3;
const Int MAX_P = 200000;
Int fact[MAX_P],inv[MAX_P],finv[MAX_P];
  
void init(Int mod){
  fact[0] = 1;
  for(Int i=1;i<MAX_P;i++)
    fact[i] = (fact[i-1]*i)%mod;

    inv[1] = 1;
    for(Int i=2;i<MAX_P;i++)
      inv[i] = inv[mod%i]*(mod-mod/i)%mod;

    
    finv[0] = 1;
    for(Int i=1;i<MAX_P;i++)
      finv[i] = finv[i-1] * inv[i]%mod;
}

Int mod_comb3(Int n,Int k,Int mod){
  if(k < 0|| k > n) return 0;
  return fact[n] * finv[k] % mod * finv[n-k]%mod;
}

const Int MAX = 2020;
vector<vector<Int> > ds(MAX);

Int dp[MAX][MAX];
Int memo[MAX][MAX];

Int dfs(Int p,Int n){
  Int &res=memo[p][n];
  if(~res) return res;

  res=dp[p][n];
 
  for(Int l:ds[n]){
    Int a=(n/l);
    if(p%a) continue;
    res+=MOD-dfs(p/a,l)*(p/a)%MOD;
  }
  
  res%=MOD;
  res*=mod_inverse(p,MOD);
  res%=MOD;
  return res;
}

signed main(){  
  cin.tie(0);
  ios::sync_with_stdio(0);
  for(Int i=1;i<MAX;i++)
    for(Int j=i+i;j<MAX;j+=i)
      ds[j].emplace_back(i);

  init(MOD);
  
  Int n,k;
  while(cin>>n>>k,n||k){

    vector<Int> co(MAX,0);
    for(Int i=1;i<=k;i++)
      for(Int j=1;j<=k;j++)
	co[i+j]++;
    
    memset(dp,0,sizeof(dp));
    dp[0][0]=1;
    for(Int p=0;p<n;p++){
      for(Int i=0;i<=n;i++){
	dp[p][i]%=MOD;
	//cout<<p<<":"<<i<<":"<<dp[p][i]<<endl;
	if(!dp[p][i]) continue;       
	for(Int a=2;i+a<=n;a++){
	  dp[p+1][i+a]+=dp[p][i]*co[a]%MOD;
	}
      }
    }
    
    memset(memo,-1,sizeof(memo));
    Int ans=0;
    for(Int i=0;i<=n;i++) dfs(i,n);
    if(n<=k) ans+=2;
    
    for(Int p=0;p<=n;p++)
      for(Int i=0;i<=n;i++)
	if(~memo[p][i]) ans+=memo[p][i];
    
    ans%=MOD;
    cout<<ans<<endl;
  }
  
  return 0;
}
accuracy: 1
time_ms: 1090
memory_kb: 71832
final_rank: 19

code_snippet: /*
 * AOJ 2164: Revenge of the Round Table
 * ?¢?????????\????????????n????????´??????????????????????±????????????¶?????¨?????????????¶????k?????????????°??§???????????????¬???????±??????????????§????????§???????
 * ?±???????P??lya?????°+DP
 * ??????????????¬i?????????????????°???gcd(n,i)???????????¨????±????????????????????????°i????????????d[i]?????¶?????£??\??¬???L=|G|*(d[c(p1)]+d[c(p2)]+...+d[c(pk)])???t[0/1][i][j]??¨?????\??????????§???????i?????\j???0???????°?????????°???d[i]?????\0????????´??????????????????0????????°???p??????????????±t[1][i-p][0~k-p]??¬?§??????°????????\d[i]=Sum(t[1][i-p][0~k-p])+t[1][i][1~k]???
 */


#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

typedef long long LL;

const LL MOD = 1000003ll;
LL t[2][1010][1010], s[2][1010][1010], d[1010];

void Dp(LL n, LL k) {
  memset(t, 0, sizeof(t));
  memset(s, 0, sizeof(s));
  memset(d, 0, sizeof(d));


  t[0][1][1] = 1;
  s[0][1][1] = 1;

  for (LL i = 2; i <= n; ++i) {
    for (int c = 0; c < 2; ++c) {
      t[c][i][1] = s[c][i][1] = s[1 - c][i - 1][min(k, i - 1)];
    }
    for (LL j = 2; j <= min(i, k); ++j) {
      for (int c = 0; c < 2; ++c) {
        t[c][i][j] = t[c][i - 1][j - 1];
        s[c][i][j] = (s[c][i][j - 1] + t[c][i][j]) % MOD;
      }
    }
    for (LL j = min(i, k) + 1; j <= k; ++j) {
      for (int c = 0; c < 2; ++c) {
        s[c][i][j] = s[c][i][j - 1];
      }
    }
  }

//  for (LL i = 1; i <= n; ++i) {
//    for (LL j = 1; j <= k; ++j) {
//      for (int c = 0; c < 1; ++c) {
//        printf("%d %lld %lld  : %lld %lld\n", c, i, j, t[c][i][j], s[c][i][j]);
//      }
//    }
//  }

  for (LL i = 1; i <= n; ++i) {
    d[i] = s[1][i][min(i, k)];
//    printf("B - d[%lld] = %lld\n", i, d[i]);
//    if (i <= k) ++d[i];
    for (LL p = 1; p <= min(k - 1, i - 2); ++p) {
      d[i] = (d[i] + s[1][i - p][min(k - p, i - 1 - p)]) % MOD;
    }
//    printf("d[%lld] = %lld\n", i, d[i]);
  }
}

LL Inverse(LL a, LL p) {
  if (a == 1) return 1;
  return ((-p / a * Inverse(p % a, p)) % p + p) % p;
}

LL Gcd(LL a, LL b) {
  for (LL t; t = b;) {
    b = a % b;
    a = t;
  }
  return a;
}

int main() {
//  freopen("/Users/yogy/acm-challenge-workbook/db.in", "r", stdin);
  LL n, k;
  while (scanf("%lld%lld", &n, &k) != EOF && n + k > 0) {
    if (n < k) k = n;
    LL ans = 0;
    Dp(n, k);
    for (LL i = 0; i < n; ++i) {
      ans = (ans + d[Gcd(n, i)]) % MOD;
    }
//    printf("%lld\n", ans);
    ans = (ans * 2 * Inverse(n, MOD)) % MOD;
    if (k >= n) ans+=2; // ????¬???¨??¨??????
    printf("%lld\n", ans);
  }
  return 0;
}
accuracy: 1
time_ms: 130
memory_kb: 34620
final_rank: 12

code_snippet: #include "bits/stdc++.h"
using namespace std;

const int mod = 1000003;
struct Mod {
public:
	int num;
	Mod() : Mod(0) { ; }
	Mod(long long int n) : num((n % mod + mod) % mod) {
		static_assert(mod<INT_MAX / 2, "mod is too big, please make num 'long long int' from 'int'");
	}
	Mod(int n) : Mod(static_cast<long long int>(n)) { ; }
	operator int() { return num; }
};

Mod operator+(const Mod a, const Mod b) { return Mod((a.num + b.num) % mod); }
Mod operator+(const long long int a, const Mod b) { return Mod(a) + b; }
Mod operator+(const Mod a, const long long int  b) { return b + a; }
Mod operator++(Mod &a) { return a + Mod(1); }
Mod operator-(const Mod a, const Mod b) { return Mod((mod + a.num - b.num) % mod); }
Mod operator-(const long long int a, const Mod b) { return Mod(a) - b; }
Mod operator--(Mod &a) { return a - Mod(1); }
Mod operator*(const Mod a, const Mod b) { return Mod(((long long)a.num * b.num) % mod); }
Mod operator*(const long long int a, const Mod b) { return Mod(a)*b; }
Mod operator*(const Mod a, const long long int b) { return Mod(b)*a; }
Mod operator*(const Mod a, const int b) { return Mod(b)*a; }
Mod operator+=(Mod &a, const Mod b) { return a = a + b; }
Mod operator+=(long long int &a, const Mod b) { return a = a + b; }
Mod operator-=(Mod &a, const Mod b) { return a = a - b; }
Mod operator-=(long long int &a, const Mod b) { return a = a - b; }
Mod operator*=(Mod &a, const Mod b) { return a = a * b; }
Mod operator*=(long long int &a, const Mod b) { return a = a * b; }
Mod operator*=(Mod& a, const long long int &b) { return a = a * b; }
Mod operator^(const Mod a, const int n) {
	if (n == 0) return Mod(1);
	Mod res = (a * a) ^ (n / 2);
	if (n % 2) res = res * a;
	return res;
}
Mod mod_pow(const Mod a, const long long  int n) {
	if (n == 0) return Mod(1);
	Mod res = mod_pow((a * a), (n / 2));
	if (n % 2) res = res * a;
	return res;
}
Mod inv(const Mod a) { return a ^ (mod - 2); }
Mod operator/(const Mod a, const Mod b) {
	assert(b.num != 0);
	return a * inv(b);
}
Mod operator/(const long long int a, const Mod b) {
	return Mod(a) / b;
}
Mod operator/=(Mod &a, const Mod b) {
	return a = a / b;
}

#define MAX_MOD_N 1024000

Mod fact[MAX_MOD_N], factinv[MAX_MOD_N];
void init(const int amax = MAX_MOD_N) {
	fact[0] = Mod(1); factinv[0] = 1;
	for (int i = 0; i < amax - 1; ++i) {
		fact[i + 1] = fact[i] * Mod(i + 1);
		factinv[i + 1] = factinv[i] / Mod(i + 1);
	}
}
Mod comb(const int a, const int b) {
	return fact[a] * factinv[b] * factinv[a - b];
}
long long int gcd(long long int l, long long int r) {
	assert(l > 0 && r > 0);
	if (l > r)return gcd(r, l);
	else {
		const long long int num = r%l;
		if (num) {
			return gcd(l, num);
		}
		else {
			return l;
		}
	}
}

Mod dp[1002][1002][2];
int main() {
	
	while (1) {
		int N, K; cin >> N >> K;
		if (!N)break;
		for (int i = 0; i < 1002; ++i) {
			for (int j = 0; j < 1002; ++j) {
				for (int k = 0; k < 2; ++k) {
					dp[i][j][k] = 0;
				}
			}
		}
		dp[1][1][0] = 1;
		for (int i = 1; i <= 1000; ++i) {
			for (int j = 1; j <= 1000; ++j) {
				for (int k = 0; k < 2; ++k) {
					if (j + 1 <= K) {
						dp[i + 1][j + 1][k] += dp[i][j][k];
					}
					dp[i + 1][1][!k] += dp[i][j][k];
				}
			}
		}
		Mod ans = 0;
		map<int, Mod>memo;
		for (int l = 0; l < N; ++l) {
			int num;
			if (l)num = gcd(l, N);
			else num = N;

			if (memo.find(num) != memo.end()) {
			}
			else {
				Mod nans = 0;
				for (int i = 0; i < min(num, K); ++i) {
					for (int j = 1; j <= min(num, K - i); ++j) {
						nans += dp[num - i][j][1];
					}
				}
				memo[num] = nans;
			}
			
			ans += memo[num];
		}
		ans *= 2;
		ans /= N;
		if (N <= K)ans += 2;
		cout << ans << endl;
	}
	return 0;
}
accuracy: 1
time_ms: 410
memory_kb: 18868
final_rank: 13

code_snippet: #include "bits/stdc++.h"
#include<unordered_map>
#include<unordered_set>
#pragma warning(disable:4996)
using namespace std;
using ld = long double;
template<class t>
using table = vector<vector<t>>;
const ld eps = 1e-9;



const int mod = 1000003;
struct Mod {
public:
	int num;
	Mod() : Mod(0) { ; }
	Mod(long long int n) : num((n % mod + mod) % mod) {
		static_assert(mod<INT_MAX / 2, "mod is too big, please make num 'long long int' from 'int'");
	}
	Mod(int n) : Mod(static_cast<long long int>(n)) { ; }
	operator int() { return num; }
};

Mod operator+(const Mod a, const Mod b) { return Mod((a.num + b.num) % mod); }
Mod operator+(const long long int a, const Mod b) { return Mod(a) + b; }
Mod operator+(const Mod a, const long long int  b) { return b + a; }
Mod operator++(Mod &a) { return a + Mod(1); }
Mod operator-(const Mod a, const Mod b) { return Mod((mod + a.num - b.num) % mod); }
Mod operator-(const long long int a, const Mod b) { return Mod(a) - b; }
Mod operator--(Mod &a) { return a - Mod(1); }
Mod operator*(const Mod a, const Mod b) { return Mod(((long long)a.num * b.num) % mod); }
Mod operator*(const long long int a, const Mod b) { return Mod(a)*b; }
Mod operator*(const Mod a, const long long int b) { return Mod(b)*a; }
Mod operator*(const Mod a, const int b) { return Mod(b)*a; }
Mod operator+=(Mod &a, const Mod b) { return a = a + b; }
Mod operator+=(long long int &a, const Mod b) { return a = a + b; }
Mod operator-=(Mod &a, const Mod b) { return a = a - b; }
Mod operator-=(long long int &a, const Mod b) { return a = a - b; }
Mod operator*=(Mod &a, const Mod b) { return a = a * b; }
Mod operator*=(long long int &a, const Mod b) { return a = a * b; }
Mod operator*=(Mod& a, const long long int &b) { return a = a * b; }
Mod operator^(const Mod a, const int n) {
	if (n == 0) return Mod(1);
	Mod res = (a * a) ^ (n / 2);
	if (n % 2) res = res * a;
	return res;
}
Mod mod_pow(const Mod a, const long long  int n) {
	if (n == 0) return Mod(1);
	Mod res = mod_pow((a * a), (n / 2));
	if (n % 2) res = res * a;
	return res;
}
Mod inv(const Mod a) { return a ^ (mod - 2); }
Mod operator/(const Mod a, const Mod b) {
	assert(b.num != 0);
	return a * inv(b);
}
Mod operator/(const long long int a, const Mod b) {
	return Mod(a) / b;
}
Mod operator/=(Mod &a, const Mod b) {
	return a = a / b;
}

#define MAX_MOD_N 1024000

Mod fact[MAX_MOD_N], factinv[MAX_MOD_N];
void init(const int amax = MAX_MOD_N) {
	fact[0] = Mod(1); factinv[0] = 1;
	for (int i = 0; i < amax - 1; ++i) {
		fact[i + 1] = fact[i] * Mod(i + 1);
		factinv[i + 1] = factinv[i] / Mod(i + 1);
	}
}
Mod comb(const int a, const int b) {
	return fact[a] * factinv[b] * factinv[a - b];
}
long long int gcd(long long int l, long long int r) {
	assert(l > 0 && r > 0);
	if (l > r)return gcd(r, l);
	else {
		const long long int num = r%l;
		if (num) {
			return gcd(l, num);
		}
		else {
			return l;
		}
	}
}

Mod dp[1002][1002][2];
int main() {
	
	while (1) {
		int N, K; cin >> N >> K;
		if (!N)break;
		for (int i = 0; i < 1002; ++i) {
			for (int j = 0; j < 1002; ++j) {
				for (int k = 0; k < 2; ++k) {
					dp[i][j][k] = 0;
				}
			}
		}
		dp[1][1][0] = 1;
		for (int i = 1; i <= 1000; ++i) {
			for (int j = 1; j <= 1000; ++j) {
				for (int k = 0; k < 2; ++k) {
					if (j + 1 <= K) {
						dp[i + 1][j + 1][k] += dp[i][j][k];
					}
					dp[i + 1][1][!k] += dp[i][j][k];
				}
			}
		}
		Mod ans = 0;
		for (int l = 0; l < N; ++l) {
			int num;
			if (l)num = gcd(l, N);
			else num = N;

			Mod nans = 0;
			for (int i = 0; i < min(num,K); ++i) {
				for (int j = 1; j <= min(num,K-i); ++j) {
					nans += dp[num - i][j][1];
				}
			}
			ans += nans;
		}
		ans *= 2;
		ans /= N;
		if (N <= K)ans += 2;
		cout << ans << endl;

	}
	return 0;
}
accuracy: 1
time_ms: 420
memory_kb: 18896
final_rank: 14

code_snippet: #include "bits/stdc++.h"
#include<unordered_map>
#include<unordered_set>
#pragma warning(disable:4996)
using namespace std;
using ld = long double;
template<class t>
using table = vector<vector<t>>;
const ld eps = 1e-9;



const int mod = 1000003;
struct Mod {
public:
	int num;
	Mod() : Mod(0) { ; }
	Mod(long long int n) : num((n % mod + mod) % mod) {
		static_assert(mod<INT_MAX / 2, "mod is too big, please make num 'long long int' from 'int'");
	}
	Mod(int n) : Mod(static_cast<long long int>(n)) { ; }
	operator int() { return num; }
};

Mod operator+(const Mod a, const Mod b) { return Mod((a.num + b.num) % mod); }
Mod operator+(const long long int a, const Mod b) { return Mod(a) + b; }
Mod operator+(const Mod a, const long long int  b) { return b + a; }
Mod operator++(Mod &a) { return a + Mod(1); }
Mod operator-(const Mod a, const Mod b) { return Mod((mod + a.num - b.num) % mod); }
Mod operator-(const long long int a, const Mod b) { return Mod(a) - b; }
Mod operator--(Mod &a) { return a - Mod(1); }
Mod operator*(const Mod a, const Mod b) { return Mod(((long long)a.num * b.num) % mod); }
Mod operator*(const long long int a, const Mod b) { return Mod(a)*b; }
Mod operator*(const Mod a, const long long int b) { return Mod(b)*a; }
Mod operator*(const Mod a, const int b) { return Mod(b)*a; }
Mod operator+=(Mod &a, const Mod b) { return a = a + b; }
Mod operator+=(long long int &a, const Mod b) { return a = a + b; }
Mod operator-=(Mod &a, const Mod b) { return a = a - b; }
Mod operator-=(long long int &a, const Mod b) { return a = a - b; }
Mod operator*=(Mod &a, const Mod b) { return a = a * b; }
Mod operator*=(long long int &a, const Mod b) { return a = a * b; }
Mod operator*=(Mod& a, const long long int &b) { return a = a * b; }
Mod operator^(const Mod a, const int n) {
	if (n == 0) return Mod(1);
	Mod res = (a * a) ^ (n / 2);
	if (n % 2) res = res * a;
	return res;
}
Mod mod_pow(const Mod a, const long long  int n) {
	if (n == 0) return Mod(1);
	Mod res = mod_pow((a * a), (n / 2));
	if (n % 2) res = res * a;
	return res;
}
Mod inv(const Mod a) { return a ^ (mod - 2); }
Mod operator/(const Mod a, const Mod b) {
	assert(b.num != 0);
	return a * inv(b);
}
Mod operator/(const long long int a, const Mod b) {
	return Mod(a) / b;
}
Mod operator/=(Mod &a, const Mod b) {
	return a = a / b;
}

#define MAX_MOD_N 1024000

Mod fact[MAX_MOD_N], factinv[MAX_MOD_N];
void init(const int amax = MAX_MOD_N) {
	fact[0] = Mod(1); factinv[0] = 1;
	for (int i = 0; i < amax - 1; ++i) {
		fact[i + 1] = fact[i] * Mod(i + 1);
		factinv[i + 1] = factinv[i] / Mod(i + 1);
	}
}
Mod comb(const int a, const int b) {
	return fact[a] * factinv[b] * factinv[a - b];
}
long long int gcd(long long int l, long long int r) {
	assert(l > 0 && r > 0);
	if (l > r)return gcd(r, l);
	else {
		const long long int num = r%l;
		if (num) {
			return gcd(l, num);
		}
		else {
			return l;
		}
	}
}

Mod dp[1002][1002][2];
int main() {
	
	while (1) {
		int N, K; cin >> N >> K;
		if (!N)break;
		for (int i = 0; i < 1002; ++i) {
			for (int j = 0; j < 1002; ++j) {
				for (int k = 0; k < 2; ++k) {
					dp[i][j][k] = 0;
				}
			}
		}
		dp[1][1][0] = 1;
		for (int i = 1; i <= 1000; ++i) {
			for (int j = 1; j <= 1000; ++j) {
				for (int k = 0; k < 2; ++k) {
					if (j + 1 <= K) {
						dp[i + 1][j + 1][k] += dp[i][j][k];
					}
					dp[i + 1][1][!k] += dp[i][j][k];
				}
			}
		}
		Mod ans = 0;
		for (int l = 0; l < N; ++l) {
			int num;
			if (l)num = gcd(l, N);
			else num = N;

			Mod nans = 0;
			for (int i = 0; i < num; ++i) {
				nans += dp[num][i][1];
			}
			for (int i = 1; i < min(num,K); ++i) {
				for (int j = 1; j <= min(num,K-i); ++j) {
					nans += dp[num - i][j][1];
				}
			}
			ans += nans;
		}
		ans *= 2;
		ans /= N;
		if (N <= K)ans += 2;
		cout << ans << endl;

	}
	return 0;
}
accuracy: 1
time_ms: 420
memory_kb: 18896
final_rank: 14

code_snippet: #include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define rep1(i,n) for(int i=1;i<=(int)(n);i++)
#define all(c) c.begin(),c.end()
#define pb push_back
#define fs first
#define sc second
#define show(x) cout << #x << " = " << x << endl
#define chmin(x,y) x=min(x,y)
#define chmax(x,y) x=max(x,y)
using namespace std;
typedef long long ll;
ll mod=1e6+3;
void add(ll &x,ll y){
	x+=y;
	x%=mod;
}
ll ex(ll x,ll p){
	ll a=1;
	while(p){
		if(p%2) a=a*x%mod;
		x=x*x%mod;
		p/=2;
	}
	return a;
}
ll inv(ll x){
	return ex(x,mod-2);
}
void solve(int N,int K){
	ll dp[1001][2]={};
	dp[0][0]=1;
	rep(i,N) rep(j,2){
		rep1(k,K){
			if(i+k>N) break;
			add(dp[i+k][1-j],dp[i][j]);
		}
	}
	vector<int> ds;
	rep1(i,N) if(N%i==0) ds.pb(i);
	vector<ll> ans;
	rep(x,ds.size()){
		int d=ds[x];
		ll v=0;
		rep(i,K+1){
			if(d==i){
				if(N<=K) add(v,2);
				break;
			}
			add(v,2*i*dp[d-i][1]);
		}
		rep(y,x){
			if(d%ds[y]==0) add(v,-ans[y]*ds[y]);
		}
		v=v*inv(d)%mod;
		ans.pb(v);
	}
	ll ret=0;
	for(ll x:ans) add(ret,x);
	cout<<(ret+mod)%mod<<endl;
}
int main(){
	while(true){
		int N,K;
		cin>>N>>K;
		if(N==0) break;
		solve(N,K);
	}
}
accuracy: 1
time_ms: 60
memory_kb: 1228
final_rank: 2

code_snippet: #include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <ctype.h>
#include <limits.h>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
#include <stack>
#include <set>
#include <bitset>
#define CLR(a) memset(a, 0, sizeof(a))
#define REP(i, a, b) for(ll i = a;i < b;i++)
#define REP_D(i, a, b) for(ll i = a;i <= b;i++)

typedef long long ll;

using namespace std;

const ll maxn = 1000 + 10;
const ll MOD = 1000003;
ll dp[maxn], dp_a[maxn][maxn], dp_b[maxn][maxn];
ll sum_a[maxn][maxn], sum_b[maxn][maxn];
ll n, k;

ll pow_mod(ll x, ll n)
{
    if(n==0)
    {
        ll t = 1;
        return t;
    }
    x %= MOD;
    ll xx = (x*x)%MOD;
    ll nn = n/2;
    ll res = pow_mod(xx, nn);
    if(n%2==1)
    {
        res = (res*x)%MOD;
    }
    return res;
}
ll gcd(ll x, ll y)
{
    if(y==0)
    {
        return x;
    }
    return gcd(y, x%y);
}

void getDp()
{
    CLR(dp_a);
    CLR(dp_b);
    CLR(sum_a);
    CLR(sum_b);

    dp_a[1][1] = 1;
    sum_a[1][1] = 1;

    for(ll i = 2; i <= n; i++)
    {
        if(i<=k)
        {
            dp_a[i][i] = 1;
        }
        ll key = min(i-2, k);
        for(ll j = 1; j<=key; j++)
        {
            ll limit = i - k - 1;
            limit = max(0LL, limit);

            dp_a[i][j] = ((sum_b[i-1][j] - sum_b[limit][j])%MOD + MOD)%MOD;

            //dp_b[i][j] = ((sum_a[i-1][j] - sum_a[limit][j])%MOD + MOD)%MOD;
            //printf("i is %lld j is %lld dp_b is %lld\n", i, j, dp_b[i][j]);
        }
        key = min(i-1, k);
        for(ll j = 1; j<=key; j++)
        {
            ll limit = i - k - 1;
            limit = max(0LL, limit);
            dp_b[i][j] = ((sum_a[i-1][j] - sum_a[limit][j])%MOD + MOD)%MOD;
            //printf("i is %lld j is %lld dp_b is %lld\n", i, j, dp_b[i][j]);
        }
        for(ll j = 1; j<=i; j++)
        {
            sum_a[i][j] = (sum_a[i-1][j] + dp_a[i][j])%MOD;
            sum_b[i][j] = (sum_b[i-1][j] + dp_b[i][j])%MOD;
        }
    }
    CLR(sum_b);
    for(ll i = 2;i <= n;i++)
    {
        //sum_b[i][0] = 0;
        for(ll j = 1;j <= n;j++)
        {
            sum_b[i][j] = (sum_b[i][j-1]+dp_b[i][j])%MOD;
        }
    }
    CLR(dp);
    for(ll i = 1; i <= n; i++)
    {
        for(ll j= 1;j<=min(k, i-1);j++)
            dp[i] = (dp[i]+dp_b[i][j])%MOD;
//        if(i<=k)
//        {
//            dp[i] = (dp[i]+1)%MOD;
//        }
        for(ll j = 1; j <= min(k,i - 2); j++)
        {
            ll lft = k - j;
            lft = min(lft, n);
            dp[i] = (dp[i]+sum_b[i-j][lft])%MOD;
        }
//        if(i <= k)
//        {
//            dp[i] = (dp[i]+1)%MOD;
//        }
        //printf(" i is %lld   %lld\n", i, dp[i]);
        dp[i] = (2*dp[i])%MOD;

    }
}

void solve()
{
    int all = 0;
    if(k >= n)
    {
        all = 2;
        //k = n - 1;
    }
    getDp();
    ll ans = dp[n];
    for(ll i = 1;i<n;i++)
    {
        ll t = gcd(i, n);
        ans = (ans + dp[t])%MOD;
    }
    ans = (ans*pow_mod(n, MOD-2))%MOD;

        ans = (ans + all)%MOD;

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

int main()
{
    //freopen("8Hin.txt", "r", stdin);
    //freopen("8Hout.txt", "w", stdout);
    while(scanf("%lld%lld", &n, &k) != EOF)
    {
        if(!n && !k)
            break;
        solve();
    }
    return 0;
}
accuracy: 1
time_ms: 240
memory_kb: 33080
final_rank: 16