結果
問題 | No.1334 Multiply or Add |
ユーザー |
![]() |
提出日時 | 2021-01-09 01:23:46 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 100 ms / 2,000 ms |
コード長 | 1,904 bytes |
コンパイル時間 | 2,022 ms |
コンパイル使用メモリ | 199,012 KB |
最終ジャッジ日時 | 2025-01-17 15:00:27 |
ジャッジサーバーID (参考情報) |
judge2 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 71 |
ソースコード
#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; }