結果
問題 | No.42 貯金箱の溜息 |
ユーザー | anta |
提出日時 | 2014-10-17 00:36:31 |
言語 | C++11 (gcc 11.4.0) |
結果 |
AC
|
実行時間 | 15 ms / 5,000 ms |
コード長 | 6,936 bytes |
コンパイル時間 | 909 ms |
コンパイル使用メモリ | 95,148 KB |
実行使用メモリ | 7,356 KB |
最終ジャッジ日時 | 2024-06-09 16:12:43 |
合計ジャッジ時間 | 1,298 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 13 ms
7,356 KB |
testcase_01 | AC | 15 ms
7,312 KB |
testcase_02 | AC | 14 ms
7,168 KB |
コンパイルメッセージ
main.cpp: In function ‘int main()’: main.cpp:182:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 182 | scanf("%d", &T); | ~~~~~^~~~~~~~~~ main.cpp:215:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 215 | scanf("%lld", &M); | ~~~~~^~~~~~~~~~~~
ソースコード
#define _CRT_SECURE_NO_WARNINGS #include <string> #include <vector> #include <algorithm> #include <numeric> #include <set> #include <map> #include <queue> #include <iostream> #include <sstream> #include <cstdio> #include <cmath> #include <ctime> #include <cstring> #include <cctype> #include <cassert> #include <limits> #include <unordered_set> #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<int> vi; typedef pair<int,int> pii; typedef vector<pair<int,int> > vpii; typedef long long ll; typedef vector<long long> vl; typedef pair<long long,long long> pll; typedef vector<pair<long long,long long> > vpll; typedef vector<string> vs; typedef long double ld; template<typename T, typename U> inline void amin(T &x, U y) { if(y < x) x = y; } template<typename T, typename U> inline void amax(T &x, U y) { if(x < y) x = y; } template<int MOD> 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; //... + 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<Polynomial, Polynomial> 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<pair<Coef,Val> > &points) { int n = points.size(); vector<Coef> 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 = 0, D = 10; vector<mint> dp(X+1); dp[0] = 1; rep(i, N) { int x = xs[i]; rer(j, 0, X-x) dp[j + x] += dp[j]; } vector<Polynomial> polynomials(Y); rep(c, Y) { // cerr << c << "..." << endl; vector<pair<mint,mint> > points(D); rep(i, D) { int z = Z + i * Y + c; points[i] = mp(z, dp[z]); } polynomials[c] = Polynomial::interpolate(points); // cerr << c << ": " << "deg: " << polynomials[c].degree1() << endl; /* rep(i, 100) { int z = Z + i * Y + c; 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(mint(M)); printf("%d\n", ans.get()); } return 0; }