結果

問題 No.1334 Multiply or Add
ユーザー chocorusk
提出日時 2021-01-09 20:58:12
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 103 ms / 2,000 ms
コード長 1,706 bytes
コンパイル時間 1,303 ms
コンパイル使用メモリ 130,568 KB
最終ジャッジ日時 2025-01-17 15:52:24
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 71
権限があれば一括ダウンロードができます

ソースコード

diff #
プレゼンテーションモードにする

#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <cmath>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#include <complex>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <cassert>
#include <fstream>
#include <utility>
#include <functional>
#include <stack>
#include <array>
#include <list>
#define popcount __builtin_popcount
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
const ll MOD=1e9+7;
int main()
{
int n; cin>>n;
ll a[200020];
ll tot=1;
for(int i=0; i<n; i++){
cin>>a[i];
tot=min(MOD, tot*a[i]);
}
if(tot==MOD){
ll ans=1;
for(int i=0; i<n; i++){
ans=ans*a[i]%MOD;
}
cout<<ans<<endl;
return 0;
}
ll p[200020];
p[0]=1;
for(int i=0; i<n; i++){
p[i+1]=p[i]*a[i];
}
using Pl=pair<ll, ll>;
deque<Pl> deq;
ll dp[200020];
dp[0]=0;
deq.push_back({1, 0});
for(int i=0; i<n; i++){
ll x=p[i+1];
while(deq.size()>1){
ll a1=deq[0].first, b1=deq[0].second;
ll a2=deq[1].first, b2=deq[1].second;
if(x/a1+b1>x/a2+b2) break;
deq.pop_front();
}
dp[i+1]=x/deq[0].first+deq[0].second;
ll a0=p[i+1], b0=dp[i+1];
while(deq.size()>1){
ll a1=deq[0].first, b1=deq[0].second;
ll a2=deq[1].first, b2=deq[1].second;
if((b1-b2)*a2*(a0-a1)>(b0-b1)*a0*(a1-a2)) break;
deq.pop_front();
}
deq.push_front({a0, b0});
}
cout<<dp[n]%MOD<<endl;
return 0;
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0