結果

問題 No.1084 積の積
ユーザー 👑 はまやんはまやんはまやんはまやん
提出日時 2020-06-20 14:42:12
言語 C++17(1z)
(gcc 10.2.0 + boost 1.73.0)
結果
AC  
実行時間 85 ms / 2,000 ms
コード長 6,101 Byte
コンパイル時間 2,330 ms
使用メモリ 33,968 KB
最終ジャッジ日時 2020-09-29 11:53:14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
使用メモリ
testcase_00 AC 2 ms
4,824 KB
testcase_01 AC 2 ms
6,876 KB
testcase_02 AC 2 ms
6,876 KB
testcase_03 AC 2 ms
6,876 KB
testcase_04 AC 85 ms
31,452 KB
testcase_05 AC 8 ms
6,876 KB
testcase_06 AC 85 ms
31,352 KB
testcase_07 AC 2 ms
6,876 KB
testcase_08 AC 2 ms
6,872 KB
testcase_09 AC 12 ms
6,876 KB
testcase_10 AC 2 ms
6,880 KB
testcase_11 AC 4 ms
4,824 KB
testcase_12 AC 38 ms
17,420 KB
testcase_13 AC 70 ms
33,852 KB
testcase_14 AC 23 ms
10,540 KB
testcase_15 AC 21 ms
10,288 KB
testcase_16 AC 76 ms
33,968 KB
testcase_17 AC 32 ms
17,476 KB
testcase_18 AC 45 ms
17,892 KB
testcase_19 AC 69 ms
33,420 KB
testcase_20 AC 17 ms
10,140 KB
testcase_21 AC 22 ms
10,596 KB
testcase_22 AC 19 ms
10,200 KB
testcase_23 AC 39 ms
17,756 KB
testcase_24 AC 37 ms
17,556 KB
testcase_25 AC 69 ms
32,620 KB
testcase_26 AC 73 ms
32,628 KB
testcase_27 AC 74 ms
32,668 KB
testcase_28 AC 74 ms
33,836 KB
testcase_29 AC 74 ms
32,672 KB
testcase_30 AC 74 ms
33,832 KB
testcase_31 AC 74 ms
33,712 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<b;i++)
#define rrep(i,a,b) for(int i=a;i>=b;i--)
#define fore(i,a) for(auto &i:a)
#define all(x) (x).begin(),(x).end()
//#pragma GCC optimize ("-O3")
using namespace std; void _main(); int main() { cin.tie(0); ios::sync_with_stdio(false); _main(); }
typedef long long ll; const int inf = INT_MAX / 2; const ll infl = 1LL << 60;
template<class T>bool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; }
//---------------------------------------------------------------------------------------------------
ll mul(ll a, ll b) { if (a == 0) return 0; if (infl / a < b) return infl; return min(infl, a * b); }
// スパーステーブルをまずは検討しよう
#ifdef _MSC_VER
inline unsigned int __builtin_clz(unsigned int x){unsigned long r;_BitScanReverse(&r,x);return 31-r;}
#endif // _MSC_VER
template<class V> struct SparseTable { // [L,R)
    const V def = 1;
    inline V comp(V a, V b) { return mul(a, b); }

    int n; vector<V> a, b[22]; inline int __lg(int x) { return 32 - 1 - __builtin_clz(x); }
    void init(V* start, V* end) {
        vector<V> v;
        for (auto ite = start; ite != end; ite++) v.push_back(*ite);
        init(v);
    }
    void init(vector<V> v) {
        int nn = v.size(); n = 1; while (n < nn) n *= 2; a.resize(n);
        rep(i, 0, 22) b[i].resize(n); rep(i, 0, nn) a[i] = v[i];

        int d = 1 << __lg(n - 1), e = d << 1;
        for (int h = 0, f; (f = 1 << h) <= d; ++h) {
            for (int i = f, r = f << 1; i < e; i += r) {
                b[h][i - 1] = a[i - 1];
                for (int j = i - 2; j >= i - f; --j) b[h][j] = comp(b[h][j + 1], a[j]);
                b[h][i] = a[i];
                for (int j = i + 1; j < i + f; ++j) b[h][j] = comp(b[h][j - 1], a[j]);
            }
        }
    }

    V get(int L,int R){assert(0<=L&&L<=R);if(L==R)return def;R--;if(L==R)return a[L];int h=__lg(L^R);
        return comp(b[h][L],b[h][R]);}};
template <typename T> struct ImosLinear {
    vector<T> v;
    vector<tuple<int, int, T>> buf;
    ImosLinear(int n) { init(n); } ImosLinear() {}
    void init(int n) { v.resize(n, 0); buf.clear(); }
    // [l,r)に傾きaで、切片bの一次関数を足す
    void set(int l, int r, T a, T b) {
        if (l + 1 < v.size()) v[l + 1] += a; if (r < v.size()) v[r] -= a;
        if (r < v.size()) buf.push_back(make_tuple(r, v.size(), (T)-(r - l - 1) * a));
        buf.push_back(make_tuple(l, r, b));
    }
    void build() {
        rep(i, 1, v.size()) v[i] += v[i - 1];
        for (auto t : buf) {
            int l, r; T b; tie(l, r, b) = t;
            v[l] += b;
            if (r < v.size()) v[r] -= b;
        }
        rep(i, 1, v.size()) v[i] += v[i - 1];
    }
    T operator[](int i) { return v[i]; }
    void dump() {
        cout << "dump : [";
        for (T i : v) cout << " " << i;
        cout << " ]" << endl;
    }
};
template<int MOD> struct ModInt {
    static const int Mod = MOD; unsigned x; ModInt() : x(0) { }
    ModInt(signed sig) { x = sig < 0 ? sig % MOD + MOD : sig % MOD; }
    ModInt(signed long long sig) { x = sig < 0 ? sig % MOD + MOD : sig % MOD; }
    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; }
};
template<int MOD> ostream& operator<<(ostream& st, const ModInt<MOD> a) { st << a.get(); return st; };
template<int MOD> ModInt<MOD> operator^(ModInt<MOD> a, unsigned long long k) {
    ModInt<MOD> r = 1; while (k) { if (k & 1) r *= a; a *= a; k >>= 1; } return r; }
typedef ModInt<1000000007> mint;
/*---------------------------------------------------------------------------------------------------
            ∧_∧
      ∧_∧  (´<_` )  Welcome to My Coding Space!
     ( ´_ゝ`) /  ⌒i     @hamayanhamayan0
    /   \     | |
    /   / ̄ ̄ ̄ ̄/  |
  __(__ニつ/     _/ .| .|____
     \/____/ (u ⊃
---------------------------------------------------------------------------------------------------*/









int N; ll A[101010];
SparseTable<ll> st;
ImosLinear<ll> imos;
int limit = 1000000000;
//---------------------------------------------------------------------------------------------------
void _main() {
	cin >> N;
	rep(i, 0, N) cin >> A[i];

    rep(i, 0, N) if (A[i] == 0) {
        cout << 0 << endl;
        return;
    }

    st.init(A, A + N);
    imos.init(N);

    rep(l, 0, N) {
        int ok = l + 1, ng = N + 1;
        while (ok + 1 != ng) {
            int md = (ok + ng) / 2;
            if (st.get(l, md) < limit) ok = md;
            else ng = md;
        }
        imos.set(l, ok, -1, ok - l);
    }

    imos.build();

    mint ans = 1;
    rep(i, 0, N) ans *= mint(A[i]) ^ imos[i];
    cout << ans << endl;
}





0