結果

問題 No.1084 積の積
ユーザー HaarHaar
提出日時 2020-06-19 22:16:13
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 41 ms / 2,000 ms
コード長 5,368 bytes
コンパイル時間 2,112 ms
コンパイル使用メモリ 206,028 KB
実行使用メモリ 7,392 KB
最終ジャッジ日時 2023-09-16 14:35:08
合計ジャッジ時間 3,906 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 40 ms
7,236 KB
testcase_05 AC 15 ms
4,376 KB
testcase_06 AC 37 ms
7,392 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 34 ms
4,380 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 3 ms
4,376 KB
testcase_12 AC 18 ms
4,828 KB
testcase_13 AC 36 ms
6,644 KB
testcase_14 AC 14 ms
4,432 KB
testcase_15 AC 13 ms
4,376 KB
testcase_16 AC 41 ms
7,160 KB
testcase_17 AC 17 ms
4,564 KB
testcase_18 AC 26 ms
5,564 KB
testcase_19 AC 37 ms
6,720 KB
testcase_20 AC 9 ms
4,380 KB
testcase_21 AC 14 ms
4,520 KB
testcase_22 AC 12 ms
4,376 KB
testcase_23 AC 22 ms
5,228 KB
testcase_24 AC 20 ms
5,060 KB
testcase_25 AC 36 ms
6,636 KB
testcase_26 AC 25 ms
7,160 KB
testcase_27 AC 26 ms
7,220 KB
testcase_28 AC 26 ms
7,200 KB
testcase_29 AC 25 ms
7,196 KB
testcase_30 AC 27 ms
7,284 KB
testcase_31 AC 24 ms
7,236 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

#ifdef DEBUG
#include <Mylib/Debug/debug.cpp>
#else
#define dump(...)
#endif

/**
 * @title Modint
 * @docs mint.md
 */
template <uint32_t M> class ModInt{
public:
  constexpr static uint32_t MOD = M;
  uint64_t val;
  
  constexpr ModInt(): val(0){}
  constexpr ModInt(int64_t n){
    if(n >= M) val = n % M;
    else if(n < 0) val = n % M + M;
    else val = n;
  }
  
  inline constexpr auto operator+(const ModInt &a) const {return ModInt(val + a.val);}
  inline constexpr auto operator-(const ModInt &a) const {return ModInt(val - a.val);}
  inline constexpr auto operator*(const ModInt &a) const {return ModInt(val * a.val);}
  inline constexpr auto operator/(const ModInt &a) const {return ModInt(val * a.inv().val);}
  
  inline constexpr auto& operator=(const ModInt &a){val = a.val; return *this;}
  inline constexpr auto& operator+=(const ModInt &a){if((val += a.val) >= M) val -= M; return *this;}
  inline constexpr auto& operator-=(const ModInt &a){if(val < a.val) val += M; val -= a.val; return *this;}
  inline constexpr auto& operator*=(const ModInt &a){(val *= a.val) %= M; return *this;}
  inline constexpr auto& operator/=(const ModInt &a){(val *= a.inv().val) %= M; return *this;}
  
  inline constexpr bool operator==(const ModInt &a) const {return val == a.val;}
  inline constexpr bool operator!=(const ModInt &a) const {return val != a.val;}
  
  inline constexpr auto& operator++(){*this += 1; return *this;}
  inline constexpr auto& operator--(){*this -= 1; return *this;}
  
  inline constexpr auto operator++(int){auto t = *this; *this += 1; return t;}
  inline constexpr auto operator--(int){auto t = *this; *this -= 1; return t;}
  
  inline constexpr static ModInt power(int64_t n, int64_t p){
    if(p < 0) return power(n, -p).inv();
    
    int64_t ret = 1, e = n % M;
    for(; p; (e *= e) %= M, p >>= 1) if(p & 1) (ret *= e) %= M;
    return ret;
  }
  
  inline constexpr static ModInt inv(int64_t a){
    int64_t b = M, u = 1, v = 0;
    
    while(b){
      int64_t t = a / b;
      a -= t * b; std::swap(a,b);
      u -= t * v; std::swap(u,v);
    }
    
    u %= M;
    if(u < 0) u += M;
    
    return u;
  }
  
  inline constexpr static auto frac(int64_t a, int64_t b){return ModInt(a) / ModInt(b);}
  
  inline constexpr auto power(int64_t p) const {return power(val, p);}
  inline constexpr auto inv() const {return inv(val);}
  
  friend inline constexpr auto operator-(const ModInt &a){return ModInt(-a.val);}
  
  friend inline constexpr auto operator+(int64_t a, const ModInt &b){return ModInt(a) + b;}
  friend inline constexpr auto operator-(int64_t a, const ModInt &b){return ModInt(a) - b;}
  friend inline constexpr auto operator*(int64_t a, const ModInt &b){return ModInt(a) * b;}
  friend inline constexpr auto operator/(int64_t a, const ModInt &b){return ModInt(a) / b;}
  
  friend std::istream& operator>>(std::istream &s, ModInt<M> &a){s >> a.val; return s;}
  friend std::ostream& operator<<(std::ostream &s, const ModInt<M> &a){s << a.val; return s;}

  template <int N>
  inline static auto div(){
    static auto value = inv(N);
    return value;
  }

  explicit operator int32_t() const noexcept {return val;}
  explicit operator int64_t() const noexcept {return val;}
};

/**
 * @title 1D Imos algorithm (Linear addition)
 * @docs linear_imos_1d.md
 */
template <typename T> struct LinearImos1D{
  std::vector<T> vec_a, vec_a_end, vec_b, vec;
  int n;

  LinearImos1D(int n): vec_a(n+1), vec_a_end(n+1), vec_b(n+1), vec(n+1), n(n){}

  void add(int s, int t, const T &a, const T &b){ // x∈[s,t)にax+bを加算する。
    vec_a[s+1] += a;
    vec_a[t] -= a;
    
    vec_a_end[t] -= a * (t-s-1);

    vec_b[s] += a * s + b;
    vec_b[t] -= a * s + b;
  }

  void build(){
    for(int i = 0; i < n; ++i) vec_a[i+1] += vec_a[i];
    for(int i = 0; i <= n; ++i) vec_a[i] += vec_a_end[i];
    for(int i = 0; i < n; ++i) vec_a[i+1] += vec_a[i];

    for(int i = 0; i < n; ++i) vec_b[i+1] += vec_b[i];

    for(int i = 0; i <= n; ++i) vec[i] = vec_a[i] + vec_b[i];
  }

  inline const T operator[](size_t i) const {return vec[i];}
};


using mint = ModInt<1000000007>;

const int MAX = 1000000000;


int main(){
  int N;

  while(std::cin >> N){
    std::vector<int64_t> A(N);
    for(int i = 0; i < N; ++i) std::cin >> A[i];

    mint ans = 0;


    if(std::count(A.begin(), A.end(), 0) == 0){
      //std::vector<int64_t> p(N);
      LinearImos1D<int64_t> p(N);

      std::vector<int> next(N);
      for(int i = N-1; i >= 0; --i){
        if(A[i] == 1){
          if(i == N-1) next[i] = N;
          else{
            if(A[i + 1] == 1){
              next[i] = next[i + 1];
            }else{
              next[i] = i + 1;
            }
          }
        }else{
          next[i] = i + 1;
        }
      }
      
      dump(next);

      for(int l = 0; l < N; ++l){
        int64_t prod = 1;

        int r = l;

        for(int i = 0; i < 100; ++i){
          if(r == N or prod * A[r] >= MAX) break;
          prod *= A[r];
          r = next[r];
        }
        
        //for(int i = l; i < r; ++i){
        //  p[i] += r - i;
        //}
        
        p.add(l, r, -1, r);
      }

      p.build();
      
      ans = 1;
      for(int i = 0; i < N; ++i){
        ans *= mint::power(A[i], p[i]);
      }
    }

    std::cout << ans << "\n";
  }
  
  return 0;
}
0