結果

問題 No.1334 Multiply or Add
ユーザー 夕叢霧香(ゆうむらきりか)夕叢霧香(ゆうむらきりか)
提出日時 2020-11-06 12:12:25
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,815 bytes
コンパイル時間 759 ms
コンパイル使用メモリ 80,272 KB
実行使用メモリ 6,964 KB
最終ジャッジ日時 2023-09-29 21:23:32
合計ジャッジ時間 4,749 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
4,376 KB
testcase_01 AC 3 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 26 ms
4,560 KB
testcase_04 AC 28 ms
4,792 KB
testcase_05 AC 28 ms
4,940 KB
testcase_06 AC 32 ms
6,788 KB
testcase_07 AC 26 ms
4,612 KB
testcase_08 AC 29 ms
4,796 KB
testcase_09 AC 30 ms
6,768 KB
testcase_10 AC 26 ms
4,532 KB
testcase_11 AC 26 ms
4,564 KB
testcase_12 AC 31 ms
6,844 KB
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 AC 25 ms
4,528 KB
testcase_17 AC 26 ms
4,760 KB
testcase_18 AC 25 ms
4,788 KB
testcase_19 AC 25 ms
4,544 KB
testcase_20 AC 25 ms
4,632 KB
testcase_21 AC 26 ms
4,624 KB
testcase_22 AC 26 ms
4,876 KB
testcase_23 AC 29 ms
6,876 KB
testcase_24 AC 29 ms
4,780 KB
testcase_25 AC 31 ms
6,964 KB
testcase_26 AC 31 ms
4,692 KB
testcase_27 AC 30 ms
4,684 KB
testcase_28 AC 31 ms
4,924 KB
testcase_29 AC 33 ms
6,908 KB
testcase_30 AC 30 ms
4,924 KB
testcase_31 AC 29 ms
4,792 KB
testcase_32 AC 32 ms
6,892 KB
testcase_33 WA -
testcase_34 AC 29 ms
4,708 KB
testcase_35 AC 30 ms
6,812 KB
testcase_36 AC 30 ms
6,796 KB
testcase_37 AC 30 ms
6,796 KB
testcase_38 AC 30 ms
6,772 KB
testcase_39 AC 2 ms
4,376 KB
testcase_40 AC 13 ms
4,376 KB
testcase_41 AC 7 ms
4,376 KB
testcase_42 AC 7 ms
4,376 KB
testcase_43 AC 20 ms
4,380 KB
testcase_44 AC 14 ms
4,380 KB
testcase_45 AC 7 ms
4,376 KB
testcase_46 AC 13 ms
4,380 KB
testcase_47 AC 18 ms
4,380 KB
testcase_48 AC 14 ms
4,380 KB
testcase_49 AC 25 ms
4,536 KB
testcase_50 AC 70 ms
4,788 KB
testcase_51 AC 67 ms
4,564 KB
testcase_52 AC 70 ms
4,596 KB
testcase_53 AC 67 ms
4,540 KB
testcase_54 AC 67 ms
4,600 KB
testcase_55 AC 72 ms
4,584 KB
testcase_56 AC 2 ms
4,376 KB
testcase_57 AC 4 ms
4,380 KB
testcase_58 AC 3 ms
4,380 KB
testcase_59 AC 4 ms
4,380 KB
testcase_60 AC 2 ms
4,376 KB
testcase_61 AC 3 ms
4,376 KB
testcase_62 AC 4 ms
4,380 KB
testcase_63 AC 3 ms
4,376 KB
testcase_64 AC 3 ms
4,380 KB
testcase_65 AC 3 ms
4,380 KB
testcase_66 AC 56 ms
4,476 KB
testcase_67 AC 36 ms
4,380 KB
testcase_68 AC 3 ms
4,380 KB
testcase_69 AC 62 ms
4,380 KB
testcase_70 AC 60 ms
4,376 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=5e5,blim=19;
//WA
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]);
  }
  //WA:ここで 2.5e6 以上なら全部掛け算としているが、2 1 ... 1 130000 のケースで足し算の方がよい
  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(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