結果

問題 No.1334 Multiply or Add
ユーザー 夕叢霧香(ゆうむらきりか)夕叢霧香(ゆうむらきりか)
提出日時 2020-11-08 16:46:51
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 167 ms / 2,000 ms
コード長 1,868 bytes
コンパイル時間 728 ms
コンパイル使用メモリ 79,392 KB
実行使用メモリ 21,192 KB
最終ジャッジ日時 2023-09-29 21:23:19
合計ジャッジ時間 6,180 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
4,380 KB
testcase_01 AC 6 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 40 ms
8,860 KB
testcase_04 AC 36 ms
6,724 KB
testcase_05 AC 37 ms
6,760 KB
testcase_06 AC 41 ms
8,924 KB
testcase_07 AC 40 ms
8,900 KB
testcase_08 AC 37 ms
6,728 KB
testcase_09 AC 39 ms
6,856 KB
testcase_10 AC 49 ms
10,888 KB
testcase_11 AC 40 ms
9,112 KB
testcase_12 AC 43 ms
8,832 KB
testcase_13 AC 34 ms
6,724 KB
testcase_14 AC 34 ms
6,724 KB
testcase_15 AC 35 ms
6,808 KB
testcase_16 AC 30 ms
4,888 KB
testcase_17 AC 30 ms
4,584 KB
testcase_18 AC 30 ms
4,688 KB
testcase_19 AC 167 ms
21,132 KB
testcase_20 AC 167 ms
21,180 KB
testcase_21 AC 167 ms
21,192 KB
testcase_22 AC 31 ms
4,784 KB
testcase_23 AC 39 ms
6,788 KB
testcase_24 AC 37 ms
6,908 KB
testcase_25 AC 39 ms
6,816 KB
testcase_26 AC 37 ms
6,764 KB
testcase_27 AC 36 ms
6,728 KB
testcase_28 AC 37 ms
6,724 KB
testcase_29 AC 40 ms
8,804 KB
testcase_30 AC 36 ms
6,760 KB
testcase_31 AC 37 ms
7,072 KB
testcase_32 AC 43 ms
8,928 KB
testcase_33 AC 34 ms
7,068 KB
testcase_34 AC 36 ms
7,092 KB
testcase_35 AC 40 ms
8,960 KB
testcase_36 AC 39 ms
8,832 KB
testcase_37 AC 40 ms
8,832 KB
testcase_38 AC 40 ms
8,900 KB
testcase_39 AC 2 ms
4,380 KB
testcase_40 AC 17 ms
5,960 KB
testcase_41 AC 9 ms
4,380 KB
testcase_42 AC 10 ms
4,380 KB
testcase_43 AC 26 ms
6,392 KB
testcase_44 AC 18 ms
5,800 KB
testcase_45 AC 10 ms
4,384 KB
testcase_46 AC 18 ms
5,840 KB
testcase_47 AC 24 ms
6,336 KB
testcase_48 AC 19 ms
5,992 KB
testcase_49 AC 31 ms
6,600 KB
testcase_50 AC 76 ms
4,708 KB
testcase_51 AC 77 ms
4,692 KB
testcase_52 AC 77 ms
4,572 KB
testcase_53 AC 78 ms
4,744 KB
testcase_54 AC 77 ms
4,588 KB
testcase_55 AC 83 ms
4,608 KB
testcase_56 AC 3 ms
4,376 KB
testcase_57 AC 8 ms
5,448 KB
testcase_58 AC 5 ms
4,380 KB
testcase_59 AC 9 ms
5,412 KB
testcase_60 AC 4 ms
4,376 KB
testcase_61 AC 5 ms
4,376 KB
testcase_62 AC 8 ms
5,580 KB
testcase_63 AC 5 ms
4,380 KB
testcase_64 AC 3 ms
4,380 KB
testcase_65 AC 5 ms
4,380 KB
testcase_66 AC 63 ms
4,476 KB
testcase_67 AC 42 ms
4,376 KB
testcase_68 AC 4 ms
4,376 KB
testcase_69 AC 71 ms
4,428 KB
testcase_70 AC 68 ms
4,620 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=1e6,blim=20;
bool dp[blim+1][lim];
//AC
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]);
  }
  //ここで積が 2n 以上であれば、掛け算が最適。
  //1 の個数を x とすると a+x+b>ab であることが必要だが、これはx>=(a-1)*(b-1) と同値で、
  //ab>=2n であれば (a-1)*(b-1)>=n-1 である。
  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;
      }
    }
  }
  int ma=0;
  rep(i,lim)if(dp[o.size()][i])ma=i;
  cout<<ma+sum<<endl;
}
0