結果

問題 No.42 貯金箱の溜息
ユーザー なおなお
提出日時 2014-11-07 17:23:14
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 57 ms / 5,000 ms
コード長 1,603 bytes
コンパイル時間 1,325 ms
コンパイル使用メモリ 150,028 KB
実行使用メモリ 11,228 KB
最終ジャッジ日時 2023-08-30 02:36:45
合計ジャッジ時間 2,183 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 52 ms
11,176 KB
testcase_01 AC 56 ms
11,228 KB
testcase_02 AC 57 ms
11,180 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define REP(i, n)           for(int(i)=0;(i)<(n);++(i))
#define RREP(i, n)          for(int(i)=(n)-1;(i)>=0;--(i))

const int MOD = (int)(1e9+9);
const int coins[] = {5,10,50,100,500};

ll dp[1000001];

ll extgcd(ll a,ll b,ll &m,ll &n){ll g=a;m=1;n=0;if(b)g=extgcd(b,a%b,n,m),n-=(a/b)*m;return g;}

ll divmod(ll n, ll m, ll mod){
    ll a,b;
    extgcd((m+mod)%mod,mod,a,b);
    return (n * a) % mod;
}

template<class _T>
_T Lagrange(const vector<pair<_T,_T> > &fj, _T x, ll mod = 0){
    int n = fj.size();
    _T res = 0;
    for(int j = 0; j < n; j++){
        _T ljx = 1;
        for(int k = 0; k < n; k++){
            if(j == k) continue;
            if(!mod) ljx *= (x - fj[k].first);
            else ljx = (ljx * ((x - fj[k].first) % mod)) % mod;
        }
        for(int k = 0; k < n; k++){
            if(j == k) continue;
            if(!mod) ljx /= (fj[j].first - fj[k].first);
            else ljx = divmod(ljx, fj[j].first - fj[k].first, mod);
        }
        res += fj[j].second * ljx; res %= mod;
    }
    if(mod) res = (res + mod) % mod;
    return res;
}

int main(){
    REP(i,1000001) dp[i] = 1;
    REP(i,5) REP(j,1000001){
        int k = j-coins[i];
        if(k >= 0) dp[j] = (dp[j] + dp[k]) % MOD;
    }

    int T; cin >> T;
    REP(i,T){
        ll M; cin >> M;
        ll x = M / 500, r = M % 500;
        vector<pair<ll,ll> > v;
        REP(i,6) v.push_back(make_pair(i, dp[i*500+r]));
        cout << Lagrange<ll>(v, x, MOD) << endl;
    }
}
0