結果

問題 No.1334 Multiply or Add
ユーザー chocoruskchocorusk
提出日時 2021-01-12 09:22:35
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 83 ms / 2,000 ms
コード長 1,956 bytes
コンパイル時間 1,478 ms
コンパイル使用メモリ 133,784 KB
実行使用メモリ 8,340 KB
最終ジャッジ日時 2023-08-13 13:49:50
合計ジャッジ時間 9,653 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,444 KB
testcase_01 AC 2 ms
7,516 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 40 ms
8,152 KB
testcase_04 AC 42 ms
8,012 KB
testcase_05 AC 41 ms
8,156 KB
testcase_06 AC 43 ms
8,084 KB
testcase_07 AC 41 ms
8,104 KB
testcase_08 AC 43 ms
8,024 KB
testcase_09 AC 41 ms
8,336 KB
testcase_10 AC 42 ms
8,096 KB
testcase_11 AC 41 ms
8,100 KB
testcase_12 AC 43 ms
8,076 KB
testcase_13 AC 45 ms
8,160 KB
testcase_14 AC 43 ms
8,036 KB
testcase_15 AC 44 ms
8,160 KB
testcase_16 AC 41 ms
8,044 KB
testcase_17 AC 40 ms
8,080 KB
testcase_18 AC 41 ms
8,156 KB
testcase_19 AC 40 ms
8,144 KB
testcase_20 AC 41 ms
8,032 KB
testcase_21 AC 41 ms
8,096 KB
testcase_22 AC 30 ms
6,500 KB
testcase_23 AC 41 ms
8,036 KB
testcase_24 AC 44 ms
8,144 KB
testcase_25 AC 41 ms
8,072 KB
testcase_26 AC 43 ms
8,096 KB
testcase_27 AC 43 ms
8,080 KB
testcase_28 AC 41 ms
8,220 KB
testcase_29 AC 41 ms
8,080 KB
testcase_30 AC 43 ms
8,044 KB
testcase_31 AC 41 ms
8,028 KB
testcase_32 AC 41 ms
7,992 KB
testcase_33 AC 45 ms
7,996 KB
testcase_34 AC 41 ms
8,340 KB
testcase_35 AC 41 ms
8,036 KB
testcase_36 AC 41 ms
8,028 KB
testcase_37 AC 41 ms
7,996 KB
testcase_38 AC 41 ms
8,100 KB
testcase_39 AC 2 ms
4,376 KB
testcase_40 AC 19 ms
6,384 KB
testcase_41 AC 8 ms
5,896 KB
testcase_42 AC 10 ms
6,000 KB
testcase_43 AC 33 ms
7,532 KB
testcase_44 AC 20 ms
6,700 KB
testcase_45 AC 8 ms
6,052 KB
testcase_46 AC 21 ms
7,052 KB
testcase_47 AC 30 ms
7,376 KB
testcase_48 AC 22 ms
6,344 KB
testcase_49 AC 41 ms
7,756 KB
testcase_50 AC 78 ms
5,936 KB
testcase_51 AC 78 ms
5,036 KB
testcase_52 AC 77 ms
5,240 KB
testcase_53 AC 76 ms
4,956 KB
testcase_54 AC 77 ms
4,976 KB
testcase_55 AC 83 ms
5,040 KB
testcase_56 AC 2 ms
5,532 KB
testcase_57 AC 2 ms
5,368 KB
testcase_58 AC 2 ms
7,428 KB
testcase_59 AC 2 ms
5,720 KB
testcase_60 AC 2 ms
5,412 KB
testcase_61 AC 2 ms
5,472 KB
testcase_62 AC 2 ms
5,368 KB
testcase_63 AC 2 ms
5,368 KB
testcase_64 AC 2 ms
5,596 KB
testcase_65 AC 2 ms
5,468 KB
testcase_66 AC 64 ms
5,560 KB
testcase_67 AC 42 ms
4,376 KB
testcase_68 AC 3 ms
4,376 KB
testcase_69 AC 71 ms
5,800 KB
testcase_70 AC 68 ms
4,760 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]);
    }
    int c=0;
    for(int i=0; i<n; i++){
        if(a[i]!=1) break;
        c++;
    }
    if(c==n){
        cout<<n<<endl;
        return 0;
    }
    for(int i=n-1; i>=0; i--){
        if(a[i]!=1) break;
        c++;
    }
    if(tot==MOD){
        ll ans=1;
        for(int i=0; i<n; i++){
            ans=ans*a[i]%MOD;
        }
        ans=(ans+c)%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