結果

問題 No.1334 Multiply or Add
ユーザー HIR180HIR180
提出日時 2021-01-09 01:23:46
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 82 ms / 2,000 ms
コード長 1,904 bytes
コンパイル時間 3,332 ms
コンパイル使用メモリ 204,736 KB
実行使用メモリ 6,184 KB
最終ジャッジ日時 2023-08-10 14:29:23
合計ジャッジ時間 8,350 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,384 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 27 ms
4,376 KB
testcase_04 AC 28 ms
4,376 KB
testcase_05 AC 29 ms
4,384 KB
testcase_06 AC 29 ms
4,380 KB
testcase_07 AC 27 ms
4,376 KB
testcase_08 AC 28 ms
4,380 KB
testcase_09 AC 28 ms
4,376 KB
testcase_10 AC 27 ms
4,380 KB
testcase_11 AC 28 ms
4,380 KB
testcase_12 AC 28 ms
4,380 KB
testcase_13 AC 26 ms
4,380 KB
testcase_14 AC 27 ms
4,380 KB
testcase_15 AC 28 ms
4,380 KB
testcase_16 AC 28 ms
4,376 KB
testcase_17 AC 28 ms
4,380 KB
testcase_18 AC 26 ms
4,380 KB
testcase_19 AC 28 ms
4,384 KB
testcase_20 AC 28 ms
4,376 KB
testcase_21 AC 27 ms
4,376 KB
testcase_22 AC 27 ms
4,380 KB
testcase_23 AC 28 ms
4,380 KB
testcase_24 AC 27 ms
4,380 KB
testcase_25 AC 28 ms
4,380 KB
testcase_26 AC 27 ms
4,380 KB
testcase_27 AC 27 ms
4,380 KB
testcase_28 AC 27 ms
4,380 KB
testcase_29 AC 28 ms
4,376 KB
testcase_30 AC 29 ms
4,388 KB
testcase_31 AC 29 ms
4,380 KB
testcase_32 AC 29 ms
4,376 KB
testcase_33 AC 29 ms
4,380 KB
testcase_34 AC 29 ms
4,380 KB
testcase_35 AC 28 ms
4,376 KB
testcase_36 AC 28 ms
4,380 KB
testcase_37 AC 29 ms
4,376 KB
testcase_38 AC 28 ms
4,376 KB
testcase_39 AC 2 ms
4,376 KB
testcase_40 AC 12 ms
4,376 KB
testcase_41 AC 5 ms
4,376 KB
testcase_42 AC 6 ms
4,380 KB
testcase_43 AC 20 ms
4,380 KB
testcase_44 AC 12 ms
4,380 KB
testcase_45 AC 6 ms
4,380 KB
testcase_46 AC 13 ms
4,380 KB
testcase_47 AC 19 ms
4,380 KB
testcase_48 AC 14 ms
4,388 KB
testcase_49 AC 25 ms
4,380 KB
testcase_50 AC 74 ms
6,024 KB
testcase_51 AC 76 ms
5,916 KB
testcase_52 AC 78 ms
5,912 KB
testcase_53 AC 78 ms
5,848 KB
testcase_54 AC 76 ms
6,184 KB
testcase_55 AC 82 ms
5,848 KB
testcase_56 AC 2 ms
4,380 KB
testcase_57 AC 1 ms
4,380 KB
testcase_58 AC 1 ms
4,376 KB
testcase_59 AC 2 ms
4,376 KB
testcase_60 AC 1 ms
4,380 KB
testcase_61 AC 1 ms
4,380 KB
testcase_62 AC 1 ms
4,384 KB
testcase_63 AC 2 ms
4,380 KB
testcase_64 AC 2 ms
4,380 KB
testcase_65 AC 2 ms
4,380 KB
testcase_66 AC 60 ms
5,812 KB
testcase_67 AC 39 ms
4,588 KB
testcase_68 AC 3 ms
4,376 KB
testcase_69 AC 68 ms
5,964 KB
testcase_70 AC 65 ms
5,752 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

using ll=long long;

#define rng(i,a,b) for(int i=int(a);i<int(b);i++)
#define rep(i,b) rng(i,0,b)
#define repn(i, b) rng(i, 1, b+1)
#define pb push_back
#define eb emplace_back
#define all(x) x.begin(),x.end()
#define si(x) int(x.size())
#ifdef LOCAL
#define dmp(x) cerr<<__LINE__<<" "<<#x<<" "<<x<<endl
#else
#define dmp(x) void(0)
#endif

template<class t,class u>bool chmax(t&a,u b){
	if(a<b){
		a=b;return true;
	}else{
		return false;
	}
}
template<class t,class u>bool chmin(t&a,u b){
	if(b<a){
		a=b;return true;
	}else{
		return false;
	}
}

template<class t,class u>
ostream&operator<<(ostream&os,const pair<t,u>&p){
	return os<<"{"<<p.first<<","<<p.second<<"}";
}

template<class t>
ostream&operator<<(ostream&os,const vector<t>&v){
	os<<"{";
	for(auto e:v)os<<e<<",";
	return os<<"}";
}

template<class t> using vc = vector<t>;
#define a first
#define b second
using P=pair<int,int>;
const ll mod = 1000000007;
void add(int &a, int b){
	a += b;
	if(a >= mod) a-=mod;
}

int n, cnt;
vc<P>pos;

int main(){
	cin >> n;
	vc<int>v;
	rep(i, n){
		int a; cin >> a; v.pb(a);
	}
	while(v.back() == 1){
		cnt ++;
		v.pop_back();
	}
	reverse(all(v));
	while(v.back() == 1){
		cnt ++;
		v.pop_back();
	}
	reverse(all(v));
	
	if(v.empty()){
		cout << n << endl;
		return 0;
	}
	ll M = 1, mm = 1;
	rep(i, v.size()){
		if(v[i] > 1) {
			pos.eb(v[i], i);
			M = M * v[i];
			mm = mm * v[i] % mod;
			chmin(M, (ll)(1000007));
		}
	}
	ll ans;
	if(M >= 1000000){
		ans = mm;
	}
	else{
		int dp[22] = {};
		rep(i, pos.size()){
			dp[i+1] = 1;
			for(int j=0;j<=i;j++){
				dp[i+1] *= pos[j].a;
			}
		}
		for(int i=1;i<=pos.size();i++){
			int add = pos[i].b - pos[i-1].b - 1;
			int zan = 1;
			for(int j=i;j<pos.size();j++){
				zan = zan * pos[j].a;
				chmax(dp[j+1], dp[i] + add + zan);
			}
		}
		ans = dp[pos.size()];
	}
	cout << (ans + cnt) % mod << endl;
}
0