結果

問題 No.1334 Multiply or Add
コンテスト
ユーザー 👑 Nachia
提出日時 2021-01-28 17:44:15
言語 C++17
(gcc 15.2.0 + boost 1.89.0)
コンパイル:
g++-15 -O2 -lm -std=c++17 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 17 ms / 2,000 ms
コード長 1,177 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 2,045 ms
コンパイル使用メモリ 214,768 KB
実行使用メモリ 6,400 KB
最終ジャッジ日時 2026-06-17 10:22:21
合計ジャッジ時間 4,012 ms
ジャッジサーバーID
(参考情報)
judge2_0 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 71
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include<bits/stdc++.h>
using namespace std;
using LL=long long;
using ULL=unsigned long long;
#define rep(i,n) for(int i=0; i<(n); i++)

const ULL M=1000000007;

int N;
int A[200000];

int main(){
  scanf("%d",&N);
  rep(i,N) scanf("%d",&A[i]);
  ULL m=1;
  bool large=false;
  rep(i,N){ m*=A[i]; if(m>N*2) large=true; m%=M; }
  int ans_offset=0;
  rep(t,2){
    while((N==0)?false:(A[N-1]==1)){ ans_offset++; N--; }
    reverse(A,A+N);
  }
  if(!large){
    vector<int> V={0};
    rep(i,N){
      if(V.size()%2==1){
        if(A[i]==1) V.back()++;
        else V.push_back(A[i]);
      }
      else{
        if(A[i]==1) V.push_back(1);
        else V.back()*=A[i];
      }
    }
    int ans=0;
    struct Node{ int factor,term; } dp[32]={};
    int n=V.size()/2; int p=n-1;
    rep(i,1<<n){
      int p=n-1; while(p>=0&&(1<<(n-1-p))&~i) p--;
      if(p!=-1) dp[p+1]={dp[p].factor*V[p*2+1],dp[p].term}; p++;
      for(; p<n; p++) dp[p+1]={V[p*2+1],dp[p].term+dp[p].factor+V[p*2]};
      ans=max(ans,dp[n].factor+dp[n].term);
    }
    if(V.size()%2==1) ans+=V.back();
    printf("%d\n",(ans+ans_offset)%M);
  }
  else{
    printf("%llu\n",(m+ans_offset)%M);
  }
  return 0;
}
0