結果

問題 No.1334 Multiply or Add
ユーザー 夕叢霧香(ゆうむらきりか)夕叢霧香(ゆうむらきりか)
提出日時 2020-11-06 11:55:08
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 1,690 bytes
コンパイル時間 737 ms
コンパイル使用メモリ 79,840 KB
実行使用メモリ 5,048 KB
最終ジャッジ日時 2023-09-29 21:23:06
合計ジャッジ時間 6,808 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 30 ms
4,536 KB
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 AC 30 ms
4,600 KB
testcase_08 RE -
testcase_09 AC 30 ms
4,756 KB
testcase_10 AC 29 ms
4,452 KB
testcase_11 AC 29 ms
4,504 KB
testcase_12 RE -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 AC 29 ms
4,504 KB
testcase_17 AC 30 ms
4,524 KB
testcase_18 AC 29 ms
4,532 KB
testcase_19 AC 29 ms
4,524 KB
testcase_20 AC 29 ms
4,640 KB
testcase_21 AC 29 ms
4,464 KB
testcase_22 AC 29 ms
4,620 KB
testcase_23 RE -
testcase_24 RE -
testcase_25 RE -
testcase_26 RE -
testcase_27 RE -
testcase_28 RE -
testcase_29 AC 31 ms
4,756 KB
testcase_30 RE -
testcase_31 RE -
testcase_32 RE -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 AC 1 ms
4,380 KB
testcase_40 WA -
testcase_41 AC 6 ms
4,380 KB
testcase_42 AC 7 ms
4,376 KB
testcase_43 WA -
testcase_44 WA -
testcase_45 AC 7 ms
4,376 KB
testcase_46 AC 15 ms
4,380 KB
testcase_47 WA -
testcase_48 WA -
testcase_49 WA -
testcase_50 AC 77 ms
4,668 KB
testcase_51 AC 77 ms
4,452 KB
testcase_52 AC 77 ms
4,512 KB
testcase_53 AC 77 ms
4,508 KB
testcase_54 AC 77 ms
4,660 KB
testcase_55 AC 83 ms
4,588 KB
testcase_56 AC 2 ms
4,376 KB
testcase_57 WA -
testcase_58 AC 3 ms
4,380 KB
testcase_59 AC 4 ms
4,376 KB
testcase_60 AC 2 ms
4,380 KB
testcase_61 WA -
testcase_62 AC 4 ms
4,376 KB
testcase_63 WA -
testcase_64 AC 2 ms
4,380 KB
testcase_65 AC 3 ms
4,376 KB
testcase_66 AC 63 ms
4,460 KB
testcase_67 AC 41 ms
4,380 KB
testcase_68 AC 3 ms
4,376 KB
testcase_69 AC 71 ms
4,376 KB
testcase_70 AC 67 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<algorithm>
#include<cassert>
#include<iostream>
#include<vector>
using namespace std;
typedef long long lint;
typedef vector<int>vi;
typedef vector<lint>vl;
typedef pair<int,int>pii;
#define rep(i,n)for(int i=0;i<(int)(n);++i)

const lint mod=1e9+7;
lint cmul(lint a,lint b){
  a=min(a,mod);
  b=min(b,mod);
  return min(a*b,mod);
}

int n;
vl a;
vl o,num,numcap;
const lint lim=2.5e5,blim=18;
bool dp[blim+1][lim];
int main(){
  cin>>n;
  a.resize(n);
  rep(i,n)cin>>a[i];
  lint sum=0;
  int c1=0;
  lint pr=1,cap=1;
  int ph=0;
  rep(i,n){
    if(a[i]==1){
      if(ph==0){
        c1++;
      }else{
        o.push_back(c1);
        num.push_back(pr);
        numcap.push_back(cap);
        ph=0;
        pr=1;
        cap=1;
        c1=1;
      }
    }else{
      if(ph==0){
        ph=1;
      }
      pr=pr*a[i]%mod;
      cap=cmul(cap,a[i]);
    }
  }
  if(ph==0){
    sum=c1;
  }else{
    o.push_back(c1);
    num.push_back(pr);
    numcap.push_back(cap);
  }
  cerr<<sum<<endl;
  rep(i,o.size()){
    cerr<<" "<<o[i]<<" "<<num[i]<<" ("<<numcap[i]<<")"<<endl;
  }
  lint gpr=1;
  lint gcap=1;
  rep(i,o.size()){
    gpr=gpr*num[i]%mod;
    gcap=cmul(gcap,numcap[i]);
  }
  if(gcap>=lim/2){
    sum+=o[0];
    cout<<(sum+gpr)%mod<<endl;
    return 0;
  }
  assert(o.size()<=blim);
  dp[0][0]=1;
  for(int i=1;i<=(int)o.size();i++){
    rep(j,i){
      lint locpr=1;
      for(int k=j;k<i;k++)locpr*=num[k];
      rep(k,lim){
        if(!dp[j][k])continue;
        assert(k+locpr+o[j]<lim);
        dp[i][k+locpr+o[j]]=1;
        assert(cmul(k,locpr)<lim);
        dp[i][k*locpr]=1;
      }
    }
  }
  int ma=0;
  rep(i,lim)if(dp[o.size()][i])ma=i;
  cout<<ma+sum<<endl;
}
0