結果

問題 No.1334 Multiply or Add
ユーザー chocoruskchocorusk
提出日時 2021-01-09 20:58:12
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 81 ms / 2,000 ms
コード長 1,706 bytes
コンパイル時間 1,322 ms
コンパイル使用メモリ 134,188 KB
実行使用メモリ 8,364 KB
最終ジャッジ日時 2023-08-11 18:01:42
合計ジャッジ時間 6,575 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,600 KB
testcase_01 AC 2 ms
5,672 KB
testcase_02 AC 1 ms
4,384 KB
testcase_03 AC 40 ms
8,044 KB
testcase_04 AC 40 ms
8,020 KB
testcase_05 AC 39 ms
8,248 KB
testcase_06 AC 41 ms
8,120 KB
testcase_07 AC 39 ms
8,108 KB
testcase_08 AC 41 ms
8,116 KB
testcase_09 AC 40 ms
8,104 KB
testcase_10 AC 39 ms
8,056 KB
testcase_11 AC 39 ms
8,112 KB
testcase_12 AC 39 ms
8,000 KB
testcase_13 AC 43 ms
8,116 KB
testcase_14 AC 43 ms
8,208 KB
testcase_15 AC 43 ms
8,016 KB
testcase_16 AC 41 ms
8,016 KB
testcase_17 AC 41 ms
8,108 KB
testcase_18 AC 40 ms
8,060 KB
testcase_19 AC 39 ms
8,232 KB
testcase_20 AC 39 ms
8,124 KB
testcase_21 AC 39 ms
8,020 KB
testcase_22 AC 43 ms
8,176 KB
testcase_23 AC 38 ms
8,068 KB
testcase_24 AC 42 ms
8,116 KB
testcase_25 AC 40 ms
8,304 KB
testcase_26 AC 41 ms
8,180 KB
testcase_27 AC 41 ms
8,364 KB
testcase_28 AC 41 ms
8,124 KB
testcase_29 AC 39 ms
8,020 KB
testcase_30 AC 38 ms
8,052 KB
testcase_31 AC 38 ms
8,236 KB
testcase_32 AC 41 ms
8,116 KB
testcase_33 AC 44 ms
8,060 KB
testcase_34 AC 39 ms
8,128 KB
testcase_35 AC 39 ms
8,020 KB
testcase_36 AC 38 ms
8,056 KB
testcase_37 AC 38 ms
8,336 KB
testcase_38 AC 39 ms
8,020 KB
testcase_39 AC 2 ms
5,496 KB
testcase_40 AC 18 ms
6,224 KB
testcase_41 AC 7 ms
5,760 KB
testcase_42 AC 9 ms
6,128 KB
testcase_43 AC 30 ms
8,020 KB
testcase_44 AC 19 ms
6,536 KB
testcase_45 AC 8 ms
5,780 KB
testcase_46 AC 19 ms
6,664 KB
testcase_47 AC 28 ms
7,020 KB
testcase_48 AC 21 ms
6,872 KB
testcase_49 AC 39 ms
7,756 KB
testcase_50 AC 74 ms
5,744 KB
testcase_51 AC 77 ms
5,740 KB
testcase_52 AC 75 ms
6,700 KB
testcase_53 AC 75 ms
6,644 KB
testcase_54 AC 74 ms
6,192 KB
testcase_55 AC 81 ms
5,036 KB
testcase_56 AC 2 ms
5,544 KB
testcase_57 AC 2 ms
5,528 KB
testcase_58 AC 2 ms
5,600 KB
testcase_59 AC 2 ms
5,752 KB
testcase_60 AC 2 ms
5,540 KB
testcase_61 AC 3 ms
5,740 KB
testcase_62 AC 2 ms
5,476 KB
testcase_63 AC 2 ms
5,484 KB
testcase_64 AC 2 ms
5,548 KB
testcase_65 AC 3 ms
5,388 KB
testcase_66 AC 60 ms
5,652 KB
testcase_67 AC 41 ms
4,376 KB
testcase_68 AC 4 ms
4,376 KB
testcase_69 AC 69 ms
5,572 KB
testcase_70 AC 66 ms
4,744 KB
権限があれば一括ダウンロードができます

ソースコード

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;
}
0