結果

問題 No.129 お年玉(2)
ユーザー krotonkroton
提出日時 2015-01-17 00:01:57
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 16 ms / 5,000 ms
コード長 2,685 bytes
コンパイル時間 1,333 ms
コンパイル使用メモリ 163,344 KB
実行使用メモリ 18,816 KB
最終ジャッジ日時 2024-05-05 21:35:58
合計ジャッジ時間 3,232 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 15 ms
18,688 KB
testcase_01 AC 15 ms
18,688 KB
testcase_02 AC 15 ms
18,560 KB
testcase_03 AC 16 ms
18,560 KB
testcase_04 AC 16 ms
18,560 KB
testcase_05 AC 15 ms
18,560 KB
testcase_06 AC 15 ms
18,560 KB
testcase_07 AC 15 ms
18,816 KB
testcase_08 AC 16 ms
18,688 KB
testcase_09 AC 16 ms
18,688 KB
testcase_10 AC 15 ms
18,688 KB
testcase_11 AC 16 ms
18,688 KB
testcase_12 AC 15 ms
18,688 KB
testcase_13 AC 16 ms
18,632 KB
testcase_14 AC 16 ms
18,560 KB
testcase_15 AC 16 ms
18,560 KB
testcase_16 AC 16 ms
18,688 KB
testcase_17 AC 15 ms
18,560 KB
testcase_18 AC 15 ms
18,560 KB
testcase_19 AC 15 ms
18,560 KB
testcase_20 AC 15 ms
18,560 KB
testcase_21 AC 15 ms
18,688 KB
testcase_22 AC 16 ms
18,560 KB
testcase_23 AC 16 ms
18,688 KB
testcase_24 AC 16 ms
18,688 KB
testcase_25 AC 15 ms
18,688 KB
testcase_26 AC 15 ms
18,560 KB
testcase_27 AC 15 ms
18,688 KB
testcase_28 AC 15 ms
18,688 KB
testcase_29 AC 15 ms
18,560 KB
testcase_30 AC 15 ms
18,432 KB
testcase_31 AC 15 ms
18,560 KB
testcase_32 AC 16 ms
18,688 KB
testcase_33 AC 15 ms
18,688 KB
testcase_34 AC 15 ms
18,560 KB
testcase_35 AC 15 ms
18,688 KB
testcase_36 AC 15 ms
18,560 KB
testcase_37 AC 16 ms
18,560 KB
testcase_38 AC 15 ms
18,560 KB
testcase_39 AC 15 ms
18,560 KB
testcase_40 AC 15 ms
18,688 KB
testcase_41 AC 16 ms
18,688 KB
testcase_42 AC 16 ms
18,560 KB
testcase_43 AC 16 ms
18,688 KB
testcase_44 AC 15 ms
18,688 KB
testcase_45 AC 15 ms
18,688 KB
testcase_46 AC 16 ms
18,560 KB
testcase_47 AC 16 ms
18,560 KB
testcase_48 AC 15 ms
18,560 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;

const ll MOD = 1e9;
const int sz = 2;
const int mod_ps[sz] = {2, 5};
const int mod_pk[sz] = {512, 1953125};

ll fact[sz][2000000];
void init(){
    for(int i=0;i<sz;i++){
        int p  = mod_ps[i];
        int pk = mod_pk[i];
        
        fact[i][0] = 1;
        for(int j=1;j<pk;j++){
            if(j % p == 0){
                fact[i][j] = fact[i][j-1];
            } else {
                fact[i][j] = fact[i][j-1] * j % pk;
            }
        }
    }
}

ll mod_inverse(ll a, ll m){
    ll b = m, u = 1, v = 0;
    while (b) {
        ll t = a / b;
        swap(a -= t * b, b);
        swap(u -= t * v, v);
    }
    return (u % m + m) % m;
}

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

ll mod_pow(ll x, ll e, ll m){
    ll v = 1;
    for(;e;e>>=1){
        if(e&1){
            v = (v * x) % m;
        }
        x = (x * x) % m;
    }
    return v;
}

ll mod_fact(int n, int i, int p, int pk, int &e1, int &e2){
    e1 = e2 = 0;
    if(n == 0)return 1;
    
    ll res = mod_fact(n / p, i, p, pk, e1, e2);
    
    e1 += n / p;
    e2 += n / pk;
    res = res * fact[i][n % pk] % pk;
    
    return res;
}

pair<ll, ll> linear_congruence(const vector<ll>& A, const vector<ll> &B, const vector<ll>& M){
    ll x = 0, m = 1;
    
    for(int i=0;i<A.size();i++){
        ll a = A[i] * m, b = B[i] - A[i] * x, d = gcd(M[i], a);
        if(b % d != 0)return make_pair(0, -1);
        ll t = b / d * mod_inverse(a / d, M[i] / d) % (M[i] / d);
        x = x + m * t;
        m *= M[i] / d;
    }
    
    x = (x % m + m) % m;
    return make_pair(x, m);
}

ll C(int n, int r, int i){
    const int p  = mod_ps[i];
    const int pk = mod_pk[i];
    
    int e1, e2, e3;
    int k1, k2, k3;
    
    ll a1 = mod_fact(n,   i, p, pk, e1, k1);
    ll a2 = mod_fact(r,   i, p, pk, e2, k2);
    ll a3 = mod_fact(n-r, i, p, pk, e3, k3);
    
    int e = e1 - e2 - e3;
    int k = k1 - k2 - k3;
    
    ll res = 1;
    res = (res * mod_pow(p, e, pk)) % pk;
    res = (res * mod_pow(fact[i][pk-1], k, pk)) % pk;
    res = (res * a1) % pk;
    res = (res * mod_inverse(a2 * a3 % pk, pk)) % pk;
    
    return res;
}

ll C(int n, int r){
    vector<ll> A(sz, 1);
    vector<ll> B(sz), M(sz);
    
    for(int i=0;i<sz;i++){
        B[i] = C(n, r, i);
        M[i] = mod_pk[i];
    }
    
    pair<ll, ll> ps = linear_congruence(A, B, M);
    return ps.first;
}

int main(){
    init();
    
    ll N, M;
    cin >> N >> M;
    
    N /= 1000;
    N %= M;
    
    cout << C(M, N) << endl;
    return 0;
}
0