#define _CRT_SECURE_NO_WARNINGS #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i)) #define rer(i,l,u) for(int (i)=(int)(l);(i)<=(int)(u);++(i)) #define reu(i,l,u) for(int (i)=(int)(l);(i)<(int)(u);++(i)) #if defined(_MSC_VER) || __cplusplus > 199711L #define aut(r,v) auto r = (v) #else #define aut(r,v) typeof(v) r = (v) #endif #define each(it,o) for(aut(it, (o).begin()); it != (o).end(); ++ it) #define all(o) (o).begin(), (o).end() #define pb(x) push_back(x) #define mp(x,y) make_pair((x),(y)) #define mset(m,v) memset(m,v,sizeof(m)) #define INF 0x3f3f3f3f #define INFL 0x3f3f3f3f3f3f3f3fLL using namespace std; typedef vector vi; typedef pair pii; typedef vector > vpii; typedef long long ll; typedef vector vl; typedef pair pll; typedef vector > vpll; typedef vector vs; typedef long double ld; template inline void amin(T &x, U y) { if(y < x) x = y; } template inline void amax(T &x, U y) { if(x < y) x = y; } template struct ModInt { static const int Mod = MOD; unsigned x; ModInt(): x(0) { } ModInt(signed sig) { int sigt = sig % MOD; if(sigt < 0) sigt += MOD; x = sigt; } ModInt(signed long long sig) { int sigt = sig % MOD; if(sigt < 0) sigt += MOD; x = sigt; } int get() const { return (int)x; } ModInt &operator+=(ModInt that) { if((x += that.x) >= MOD) x -= MOD; return *this; } ModInt &operator-=(ModInt that) { if((x += MOD - that.x) >= MOD) x -= MOD; return *this; } ModInt &operator*=(ModInt that) { x = (unsigned long long)x * that.x % MOD; return *this; } ModInt &operator/=(ModInt that) { return *this *= that.inverse(); } ModInt operator+(ModInt that) const { return ModInt(*this) += that; } ModInt operator-(ModInt that) const { return ModInt(*this) -= that; } ModInt operator*(ModInt that) const { return ModInt(*this) *= that; } ModInt operator/(ModInt that) const { return ModInt(*this) /= that; } ModInt inverse() const { long long a = x, b = MOD, u = 1, v = 0; while(b) { long long t = a / b; a -= t * b; std::swap(a, b); u -= t * v; std::swap(u, v); } return ModInt(u); } bool operator==(ModInt that) const { return x == that.x; } bool operator!=(ModInt that) const { return x != that.x; } ModInt operator-() const { ModInt t; t.x = x == 0 ? 0 : Mod - x; return t; } }; typedef ModInt<1000000009> mint; struct Polynomial { typedef mint Coef; typedef Coef Val; vector coef; //... + coef[2] x^2 + coef[1] x + coef[0] Polynomial() {} explicit Polynomial(int n): coef(n) {} static Polynomial One() { Polynomial r(1); r.coef[0] = 1; return r; } bool iszero() const { return coef.empty(); } int degree1() const { return coef.size(); } //degree + 1 int resize(int d) { if(degree1() < d) coef.resize(d); return d; } const Coef operator[](int i) const { return i >= degree1() ? Coef() : coef[i]; } void canonicalize() { int i = coef.size(); while(i > 0 && coef[i-1] == Coef()) i --; coef.resize(i); } Val evalute(Val x) const { int d = degree1(); Val t = 0, y = 1; rep(i, d) { t += y * coef[i]; y *= x; } return t; } Polynomial &operator+=(const Polynomial &that) { int d = resize(that.degree1()); for(int i = 0; i < d; i ++) coef[i] += that[i]; canonicalize(); return *this; } Polynomial operator+(const Polynomial &that) const { return Polynomial(*this) += that; } Polynomial &operator-=(const Polynomial &that) { int d = resize(that.degree1()); for(int i = 0; i < d; i ++) coef[i] -= that[i]; canonicalize(); return *this; } Polynomial operator-(const Polynomial &that) const { return Polynomial(*this) -= that; } Polynomial operator-() const { int d = degree1(); Polynomial res(d); for(int i = 0; i < d; i ++) res.coef[i] = - coef[i]; return res; } //naive Polynomial operator*(const Polynomial &that) const { if(iszero() || that.iszero()) return Polynomial(); int x = degree1(), y = that.degree1(), d = x + y - 1; Polynomial res(d); rep(i, x) rep(j, y) res.coef[i+j] += coef[i] * that.coef[j]; res.canonicalize(); return res; } //long division pair divmod(const Polynomial &that) const { int x = degree1() - 1, y = that.degree1() - 1; int d = max(0, x - y); Polynomial q(d + 1), r = *this; for(int i = x; i >= y; i --) { Coef t = r.coef[i] / that.coef[y]; q.coef[i - y] = t; assert(t * that.coef[y] == r.coef[i]); r.coef[i] = 0; if(t == 0) continue; for(int j = 0; j < y; j ++) r.coef[i - y + j] -= t * that.coef[j]; } q.canonicalize(); r.canonicalize(); return make_pair(q, r); } Polynomial operator/(const Polynomial &that) const { return divmod(that).first; } Polynomial operator%(const Polynomial &that) const { return divmod(that).second; } static Polynomial interpolate(const vector > &points) { int n = points.size(); vector dp(n+1); dp[0] = 1; rep(i, n) for(int j = i; j >= 0; j --) { dp[j+1] += dp[j]; dp[j] *= -points[i].first; } Polynomial r(n); rep(i, n) { Coef den = 1; rep(j, n) if(i != j) den *= points[i].first - points[j].first; Coef iden = (Coef)1 / den, minus = 0; for(int j = n-1; j >= 0; j --) { minus = dp[j+1] + minus * points[i].first; r.coef[j] += minus * iden * points[i].second; } } r.canonicalize(); return r; } }; int main() { int T; scanf("%d", &T); const int N = 6; const int xs[N] = { 1, 5, 10, 50, 100, 500 }; const int X = 1000000, Y = 500, Z = 500 * 500, D = 500; vector dp(X+1); dp[0] = 1; rep(i, N) { int x = xs[i]; rer(j, 0, X-x) dp[j + x] += dp[j]; } vector polynomials(Y); rep(c, Y) { // cerr << c << "..." << endl; vector > points(D); rep(i, D) { int z = Z + i * Y; points[i] = mp(z, dp[z]); } polynomials[c] = Polynomial::interpolate(points); /* rep(i, D + 100) { int z = Z + i * Y; mint a = polynomials[c].evalute(z); mint b = dp[z]; if(a.get() != b.get()) cerr << c << ", " << z << ": " << a.get() << ", " << b.get() << endl; } // */ } rep(ii, T) { long long M; scanf("%lld", &M); mint ans; if(M <= X) ans = dp[(int)M]; else ans = polynomials[M % Y].evalute(M); printf("%d\n", ans.get()); } return 0; }