結果

問題 No.1084 積の積
ユーザー yukudoyukudo
提出日時 2020-06-19 22:08:15
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 37 ms / 2,000 ms
コード長 1,825 bytes
コンパイル時間 1,554 ms
コンパイル使用メモリ 170,484 KB
実行使用メモリ 5,692 KB
最終ジャッジ日時 2023-09-16 14:28:44
合計ジャッジ時間 3,395 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 37 ms
5,692 KB
testcase_05 AC 9 ms
4,380 KB
testcase_06 AC 37 ms
5,468 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 14 ms
4,376 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 3 ms
4,376 KB
testcase_12 AC 15 ms
4,376 KB
testcase_13 AC 31 ms
5,252 KB
testcase_14 AC 12 ms
4,380 KB
testcase_15 AC 11 ms
4,376 KB
testcase_16 AC 35 ms
5,400 KB
testcase_17 AC 14 ms
4,380 KB
testcase_18 AC 23 ms
4,592 KB
testcase_19 AC 31 ms
5,156 KB
testcase_20 AC 8 ms
4,376 KB
testcase_21 AC 12 ms
4,376 KB
testcase_22 AC 9 ms
4,376 KB
testcase_23 AC 19 ms
4,380 KB
testcase_24 AC 18 ms
4,380 KB
testcase_25 AC 31 ms
5,136 KB
testcase_26 AC 31 ms
5,464 KB
testcase_27 AC 31 ms
5,524 KB
testcase_28 AC 31 ms
5,544 KB
testcase_29 AC 31 ms
5,688 KB
testcase_30 AC 31 ms
5,420 KB
testcase_31 AC 31 ms
5,468 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

typedef long long ll;
#define REP(i,n) for(int i=0,_n=(int)(n);i<_n;++i)
#define ALL(v) (v).begin(),(v).end()
#define CLR(t,v) memset(t,(v),sizeof(t))
template<class T1,class T2>ostream& operator<<(ostream& os,const pair<T1,T2>&a){return os<<"("<<a.first<<","<<a.second<< ")";}
template<class T>void pv(T a,T b){for(T i=a;i!=b;++i)cout<<(*i)<<" ";cout<<endl;}
template<class T>void chmin(T&a,const T&b){if(a>b)a=b;}
template<class T>void chmax(T&a,const T&b){if(a<b)a=b;}

ll nextLong() { ll x; scanf("%lld", &x); return x;}

const ll MOD = 1e9 + 7;
const ll BIG = 1e9;


ll mod_pow(ll a, ll b, ll p = MOD) {
  ll res = 1;
  while (b > 0) {
    if (b & 1) res = (res * a) % p;
    a = (a * a) % p;
    b >>= 1;
  }
  return res;
}
ll mod_inv(ll a, ll p = MOD) {
  return mod_pow(a % p, p - 2, p);
}


ll solve(vector<ll> A) {
  A.push_back(BIG+1);
  // pv(ALL(A));
  const int N = A.size();
  ll res = 1;

  ll w = 1; // A[a]^1 * a[a+1]^2 * ... * A[b-1]^(b-a)
  ll prod = 1;
  int a = 0, b = 0;
  while (a < N) {
    int type = 0;
    if (b == N) {
      type = 1;
    } else if (prod >= BIG) {
      type = 1;
    }

    if (type == 0) {
      prod *= A[b];
      w *= mod_pow(A[b], (b - a + 1));
      w %= MOD;
      b++;
    } else {
      w *= mod_inv(prod);
      w %= MOD;
      prod /= A[a];
      a++;
    }

    // cout << "[" << a << " " << b << "] " << prod << " " << w << endl;

    if (prod < BIG) {
      res = (res * w) % MOD;
    }
  }

  return res;
}

int main2() {
  int N = nextLong();
  vector<ll> A(N);
  REP(i, N) A[i] = nextLong();


  if (count(ALL(A), 0) > 0) {
    cout << 0 << endl;
    return 0;
  }

  ll ans = solve(A);
  cout << ans << endl;
  return 0;
}

int main() {

#ifdef LOCAL
  for (;!cin.eof();cin>>ws)
#endif
    main2();
  return 0;
}
0