結果

問題 No.2396 等差二項展開
ユーザー milanis48663220milanis48663220
提出日時 2023-07-28 22:37:57
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 997 ms / 6,000 ms
コード長 3,780 bytes
コンパイル時間 1,402 ms
コンパイル使用メモリ 127,436 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-16 06:35:22
合計ジャッジ時間 10,025 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 2 ms
6,944 KB
testcase_07 AC 2 ms
6,944 KB
testcase_08 AC 2 ms
6,944 KB
testcase_09 AC 2 ms
6,944 KB
testcase_10 AC 2 ms
6,940 KB
testcase_11 AC 2 ms
6,940 KB
testcase_12 AC 2 ms
6,940 KB
testcase_13 AC 2 ms
6,940 KB
testcase_14 AC 2 ms
6,940 KB
testcase_15 AC 10 ms
6,940 KB
testcase_16 AC 119 ms
6,944 KB
testcase_17 AC 519 ms
6,940 KB
testcase_18 AC 831 ms
6,944 KB
testcase_19 AC 997 ms
6,940 KB
testcase_20 AC 2 ms
6,940 KB
testcase_21 AC 2 ms
6,944 KB
testcase_22 AC 2 ms
6,944 KB
testcase_23 AC 437 ms
6,940 KB
testcase_24 AC 683 ms
6,940 KB
testcase_25 AC 831 ms
6,944 KB
testcase_26 AC 616 ms
6,940 KB
testcase_27 AC 683 ms
6,944 KB
testcase_28 AC 745 ms
6,940 KB
testcase_29 AC 682 ms
6,940 KB
testcase_30 AC 499 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <tuple>
#include <cmath>
#include <numeric>
#include <functional>
#include <cassert>
#include <atcoder/modint>

#define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl;
#define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

using namespace std;
typedef long long ll;

template<typename T>
vector<vector<T>> vec2d(int n, int m, T v){
    return vector<vector<T>>(n, vector<T>(m, v));
}

template<typename T>
vector<vector<vector<T>>> vec3d(int n, int m, int k, T v){
    return vector<vector<vector<T>>>(n, vector<vector<T>>(m, vector<T>(k, v)));
}

template<typename T>
void print_vector(vector<T> v, char delimiter=' '){
    if(v.empty()) {
        cout << endl;
        return;
    }
    for(int i = 0; i+1 < v.size(); i++) cout << v[i] << delimiter;
    cout << v.back() << endl;
}

using mint = atcoder::modint;

ostream& operator<<(ostream& os, const mint& m){
    os << m.val();
    return os;
}


ll mod_pow(ll a, ll n, ll mod) {
    ll ans = 1;
    
    return ans;
}

mint solve(ll n, ll m, int l, int k, int b){
    mint::set_mod(b);
    vector<mint> ans(l), a(l);
    ans[0] = 1;
    a[0] = 1;
    if(l == 1){
        a[0] += m; 
    }else{
        a[1] = 1;        
    }
    while (n > 0) {
        if (n&1) {
            vector<mint> nx(l);
            for(int i = 0; i < l; i++){
                for(int j = 0; j < l; j++){
                    if(i+j >= l){
                        nx[(i+j)%l] += ans[i]*a[j]*m;
                    }else{
                        nx[(i+j)%l] += ans[i]*a[j];
                    }
                }
            }
            ans = nx;
        }
        vector<mint> nx(l);
        for(int i = 0; i < l; i++){
            for(int j = 0; j < l; j++){
               if(i+j >= l){
                    nx[(i+j)%l] += a[i]*a[j]*m;
                }else{
                    nx[(i+j)%l] += a[i]*a[j];
                }
            }
        }
        a = nx;
        n >>= 1;
    }
    return ans[k];
}

mint naive(ll n, ll m, int l, int k, int b){
    mint::set_mod(b);
    auto comb = vec2d(n+1, n+1, mint(0));
    for(int i = 0; i <= n; i++) {
        comb[i][0] = 1;
        for(int j = 1; j <= i; j++){
            comb[i][j] = comb[i-1][j]+comb[i-1][j-1];
        }
    }
    mint ans = 0;
    // cout << "====";
    for(int i = 0; i*l+k <= n; i++){
        ans += comb[n][i*l+k]*mint(m).pow(i);
        // cout << comb[n][i*l+k]*mint(m).pow(i) << ' ';
    }
    // cout << endl;
    return ans;
}

void test(){
    for(int n = 1; n <= 10; n++){
        for(int m = 1; m <= 10; m++){
            for(int l = 1; l <= 10; l++){
                for(int k = 0; k < l; k++){
                    for(int b = 1; b <= 10; b++){
                        mint a0 = solve(n, m, l, k, b);
                        mint a1 = naive(n, m, l, k, b);
                        if(a0 != a1){
                            cout << n << ' ' << m << ' ' << l << ' ' << k << ' ' << b << ':';
                            cout << a0 << ' ' << a1 << endl;
                        }
                    }
                }
            }
        }
    }
    cout << "OK" << endl;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << setprecision(10) << fixed;
    // test();
    ll n, m; int l, k, b; cin >> n >> m >> l >> k >> b;
    cout << solve(n, m, l, k, b) << endl;   
    // cout << naive(n, m, l, k, b) << endl;   
}
0