結果

問題 No.1084 積の積
ユーザー noisy_noiminnoisy_noimin
提出日時 2020-06-22 21:28:38
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 75 ms / 2,000 ms
コード長 2,196 bytes
コンパイル時間 1,055 ms
コンパイル使用メモリ 120,996 KB
実行使用メモリ 5,776 KB
最終ジャッジ日時 2023-09-16 19:27:22
合計ジャッジ時間 3,614 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 61 ms
5,752 KB
testcase_05 AC 7 ms
4,380 KB
testcase_06 AC 61 ms
5,668 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 12 ms
4,376 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 4 ms
4,376 KB
testcase_12 AC 32 ms
4,376 KB
testcase_13 AC 67 ms
5,476 KB
testcase_14 AC 24 ms
4,376 KB
testcase_15 AC 22 ms
4,376 KB
testcase_16 AC 74 ms
5,636 KB
testcase_17 AC 28 ms
4,380 KB
testcase_18 AC 48 ms
4,960 KB
testcase_19 AC 68 ms
5,512 KB
testcase_20 AC 16 ms
4,380 KB
testcase_21 AC 24 ms
4,380 KB
testcase_22 AC 19 ms
4,380 KB
testcase_23 AC 40 ms
4,380 KB
testcase_24 AC 36 ms
4,380 KB
testcase_25 AC 65 ms
5,432 KB
testcase_26 AC 73 ms
5,700 KB
testcase_27 AC 74 ms
5,652 KB
testcase_28 AC 75 ms
5,776 KB
testcase_29 AC 71 ms
5,760 KB
testcase_30 AC 71 ms
5,616 KB
testcase_31 AC 74 ms
5,748 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <iomanip>
#include <string>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <tuple>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cassert>
#include <cstdint>
#include <cctype>
#include <numeric>
#include <bitset>
#include <functional>

using namespace std;

using ll =  long long;
using Pll = pair<ll, ll>;
using Pii = pair<int, int>;

constexpr int INF = 1 << 30;
constexpr ll LINF = 1LL << 60;
constexpr ll MOD = 1000000007;
constexpr ll MAX = 1000000000;
constexpr long double EPS = 1e-10;
constexpr int dyx[4][2] = {
    { 0, 1}, {-1, 0}, {0,-1}, {1, 0}
};

ll modpow(ll a, ll t) {
    if(t < 0) assert(false);
    ll ret = 1LL;
    while(t){
        if(t & 1LL){
            ret *= a;
            ret %= MOD;
        }
        a *= a;
        a %= MOD;
        t >>= 1;
    }
    return ret;
}

ll modinv(ll a) {
    return modpow(a, MOD-2);
}

int main() {
    ios::sync_with_stdio(false); cin.tie(nullptr);
    int n;
    cin >> n;
    vector<ll> a(n+1, 1);
    bool includes_zero = false;
    for(int i=1;i<=n;++i) {
        cin >> a[i];
        includes_zero |= !a[i];
    }
    if(includes_zero) {
        cout << 0 << endl;
        return 0;
    }

    vector<int> r(n+1, 0);
    r[0] = 1;
    ll p = 1LL;
    for(int i=1;i<=n;++i) {
        r[i] = max(i, r[i-1]);
        if(r[i] == i) p = 1LL;
        while(r[i] <= n) {
            if(a[r[i]] * p >= MAX) {
                break;
            } else {
                p *= a[r[i]];
                ++r[i];
            }
        }
        p /= a[i];
    }

    vector<ll> weighted_product(n+1, 1);
    vector<ll> product(n+1, 1);
    for(int i=1;i<=n;++i) {
        weighted_product[i] = (weighted_product[i-1] * modpow(a[i], n+1-i)) % MOD;
        product[i] = (product[i-1] * a[i]) % MOD;
    }

    ll ans = 1LL;
    for(int i=1;i<=n;++i) {
        ll tmp = (weighted_product[r[i]-1] * modinv(weighted_product[i-1])) % MOD;
        tmp = (tmp * modpow(modinv((product[r[i]-1] * modinv(product[i-1])) % MOD), n+1-r[i])) % MOD;
        ans *= tmp;
        ans %= MOD;
    }

    cout << ans << endl;

}
0