結果
問題 | No.1334 Multiply or Add |
ユーザー | mtsd |
提出日時 | 2021-01-08 22:40:08 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 11,063 bytes |
コンパイル時間 | 1,644 ms |
コンパイル使用メモリ | 134,260 KB |
実行使用メモリ | 13,680 KB |
最終ジャッジ日時 | 2024-11-16 13:53:26 |
合計ジャッジ時間 | 4,861 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 3 ms
5,632 KB |
testcase_01 | AC | 3 ms
5,760 KB |
testcase_02 | AC | 3 ms
5,760 KB |
testcase_03 | AC | 16 ms
7,168 KB |
testcase_04 | AC | 17 ms
7,168 KB |
testcase_05 | AC | 15 ms
7,296 KB |
testcase_06 | AC | 16 ms
7,296 KB |
testcase_07 | AC | 16 ms
7,296 KB |
testcase_08 | AC | 15 ms
7,168 KB |
testcase_09 | AC | 16 ms
7,168 KB |
testcase_10 | AC | 17 ms
7,296 KB |
testcase_11 | AC | 17 ms
7,168 KB |
testcase_12 | AC | 16 ms
7,168 KB |
testcase_13 | AC | 16 ms
7,168 KB |
testcase_14 | AC | 16 ms
7,296 KB |
testcase_15 | AC | 16 ms
7,296 KB |
testcase_16 | AC | 16 ms
7,296 KB |
testcase_17 | AC | 16 ms
7,296 KB |
testcase_18 | AC | 16 ms
7,296 KB |
testcase_19 | WA | - |
testcase_20 | WA | - |
testcase_21 | WA | - |
testcase_22 | AC | 16 ms
7,168 KB |
testcase_23 | AC | 16 ms
7,296 KB |
testcase_24 | AC | 15 ms
7,296 KB |
testcase_25 | AC | 15 ms
7,296 KB |
testcase_26 | AC | 15 ms
7,168 KB |
testcase_27 | AC | 16 ms
7,296 KB |
testcase_28 | AC | 15 ms
7,296 KB |
testcase_29 | AC | 16 ms
7,168 KB |
testcase_30 | AC | 15 ms
7,168 KB |
testcase_31 | AC | 16 ms
7,296 KB |
testcase_32 | AC | 16 ms
7,296 KB |
testcase_33 | AC | 15 ms
7,168 KB |
testcase_34 | AC | 16 ms
7,296 KB |
testcase_35 | AC | 16 ms
7,168 KB |
testcase_36 | AC | 16 ms
7,296 KB |
testcase_37 | AC | 16 ms
7,296 KB |
testcase_38 | AC | 16 ms
7,296 KB |
testcase_39 | AC | 4 ms
5,760 KB |
testcase_40 | AC | 8 ms
6,272 KB |
testcase_41 | AC | 5 ms
5,888 KB |
testcase_42 | AC | 5 ms
5,888 KB |
testcase_43 | AC | 12 ms
6,912 KB |
testcase_44 | AC | 9 ms
6,272 KB |
testcase_45 | AC | 5 ms
5,796 KB |
testcase_46 | AC | 9 ms
6,400 KB |
testcase_47 | AC | 11 ms
6,784 KB |
testcase_48 | AC | 9 ms
6,272 KB |
testcase_49 | AC | 13 ms
7,040 KB |
testcase_50 | AC | 33 ms
13,556 KB |
testcase_51 | AC | 34 ms
13,552 KB |
testcase_52 | AC | 35 ms
13,552 KB |
testcase_53 | AC | 34 ms
13,548 KB |
testcase_54 | AC | 35 ms
13,548 KB |
testcase_55 | AC | 34 ms
13,680 KB |
testcase_56 | AC | 3 ms
5,760 KB |
testcase_57 | AC | 3 ms
5,760 KB |
testcase_58 | AC | 3 ms
5,760 KB |
testcase_59 | AC | 4 ms
5,760 KB |
testcase_60 | AC | 3 ms
5,760 KB |
testcase_61 | AC | 3 ms
5,632 KB |
testcase_62 | AC | 4 ms
5,760 KB |
testcase_63 | AC | 4 ms
5,760 KB |
testcase_64 | AC | 4 ms
5,760 KB |
testcase_65 | AC | 3 ms
5,760 KB |
testcase_66 | AC | 29 ms
12,196 KB |
testcase_67 | AC | 20 ms
9,908 KB |
testcase_68 | AC | 4 ms
5,888 KB |
testcase_69 | AC | 31 ms
13,156 KB |
testcase_70 | AC | 29 ms
12,688 KB |
ソースコード
#include <algorithm> #include <bitset> #include <cassert> #include <chrono> #include <climits> #include <cmath> #include <complex> #include <cstring> #include <deque> #include <functional> #include <iostream> #include <iomanip> #include <list> #include <map> #include <numeric> #include <queue> #include <random> #include <set> #include <stack> #include <unordered_map> #include <unordered_set> #include <vector> #include <cstdint> using namespace std; typedef long long ll; typedef vector<int> vi; typedef pair<int,int> pii; #define MP make_pair #define PB push_back #define inf 1000000007 #define rep(i,n) for(int i = 0; i < (int)(n); ++i) #define all(x) (x).begin(),(x).end() template<typename A, size_t N, typename T> void Fill(A (&array)[N], const T &val){ std::fill( (T*)array, (T*)(array+N), val ); } template<class T> inline bool chmax(T &a, T b){ if(a<b){ a = b; return true; } return false; } template<class T> inline bool chmin(T &a, T b){ if(a>b){ a = b; return true; } return false; } template <unsigned int mod> class ModInt { private: unsigned int v; static unsigned int norm(const unsigned int& x){ return x < mod ? x : x - mod; } static ModInt make(const unsigned int& x){ ModInt m; return m.v = x, m; } static ModInt inv(const ModInt& x){ return make(inverse(x.v, mod)); } static unsigned int inverse(int a, int m){ int u[] = {a, 1, 0}, v[] = {m, 0, 1}, t; while(*v){ t = *u / *v; swap(u[0] -= t * v[0], v[0]), swap(u[1] -= t * v[1], v[1]), swap(u[2] -= t * v[2], v[2]); } return (u[1] % m + m) % m; } public: ModInt() : v{0}{} ModInt(const long long val) : v{norm(val % mod + mod)} {} ModInt(const ModInt<mod>& n) : v{n()} {} explicit operator bool() const noexcept { return v != 0; } bool operator!() const noexcept { return !static_cast<bool>(*this); } ModInt& operator=(const ModInt& n){ return v = n(), (*this); } ModInt& operator=(const long long val){ return v = norm(val % mod + mod), (*this); } ModInt operator+() const { return *this; } ModInt operator-() const { return v == 0 ? make(0) : make(mod - v); } ModInt operator+(const ModInt& val) const { return make(norm(v + val())); } ModInt operator-(const ModInt& val) const { return make(norm(v + mod - val())); } ModInt operator*(const ModInt& val) const { return make((long long)v * val() % mod); } ModInt operator/(const ModInt& val) const { return *this * inv(val); } ModInt& operator+=(const ModInt& val){ return *this = *this + val; } ModInt& operator-=(const ModInt& val){ return *this = *this - val; } ModInt& operator*=(const ModInt& val){ return *this = *this * val; } ModInt& operator/=(const ModInt& val){ return *this = *this / val; } ModInt operator+(const long long val) const { return ModInt{v + val}; } ModInt operator-(const long long val) const { return ModInt{v - val}; } ModInt operator*(const long long val) const { return ModInt{(long long)v * (val % mod)}; } ModInt operator/(const long long val) const { return ModInt{(long long)v * inv(val)}; } ModInt& operator+=(const long long val){ return *this = *this + val; } ModInt& operator-=(const long long val){ return *this = *this - val; } ModInt& operator*=(const long long val){ return *this = *this * val; } ModInt& operator/=(const long long val){ return *this = *this / val; } bool operator==(const ModInt& val) const { return v == val.v; } bool operator!=(const ModInt& val) const { return !(*this == val); } bool operator==(const long long val) const { return v == norm(val % mod + mod); } bool operator!=(const long long val) const { return !(*this == val); } unsigned int operator()() const { return v; } friend ModInt operator+(const long long val, const ModInt& n) { return n + val; } friend ModInt operator-(const long long val, const ModInt& n) { return ModInt{val - n()}; } friend ModInt operator*(const long long val, const ModInt& n) { return n * val; } friend ModInt operator/(const long long val, const ModInt& n) { return ModInt{val} / n; } friend bool operator==(const long long val, const ModInt& n) { return n == val; } friend bool operator!=(const long long val, const ModInt& n) { return !(val == n); } friend istream& operator>>(istream& is, ModInt& n){ unsigned int v; return is >> v, n = v, is; } friend ostream& operator<<(ostream& os, const ModInt& n){ return (os << n()); } friend ModInt mod_pow(ModInt x, long long n){ ModInt ans = 1; while(n){ if(n & 1) ans *= x; x *= x, n >>= 1; } return ans; } }; #define MOD 1000000007 using mod = ModInt<MOD>; #define MAX_N 200000 mod inv[MAX_N],fac[MAX_N],finv[MAX_N]; void make() { fac[0] = fac[1] = 1; finv[0] = finv[1] = 1; inv[1] = 1; for(int i=2;i<MAX_N;i++){ inv[i] = MOD - inv[MOD % i] * (MOD / i); fac[i] = fac[i-1] * i; finv[i] = finv[i-1] * inv[i]; } } mod comb(int a, int b) { if(a<b) return 0; return fac[a] * finv[b] * finv[a-b]; } class UnionFind { private: int sz; vector<int> par, size_; public: UnionFind(){} UnionFind(int node_size) : sz(node_size), par(sz), size_(sz, 1){ iota(par.begin(), par.end(), 0); } int find(int x){ if(par[x] == x) return x; else return par[x] = find(par[x]); } void unite(int x,int y){ x = find(x), y = find(y); if(x == y) return; if(size_[x] < size_[y]) swap(x,y); par[y] = x; size_[x] += size_[y]; } int size(int x){ x = find(x); return size_[x]; } bool same(int x,int y){ return find(x) == find(y); } }; #define int long long signed main(){ cin.tie(0); ios::sync_with_stdio(false); int n; cin >> n; vector<ll> a(n); rep(i,n) cin >> a[i]; vector<char> res(n); vector<vector<pair<int,int> > > p; vector<pair<int,int> > tmp; int id = 0; while(id!=n){ int k = 0; if(a[id]==1){ while(id!=n&&a[id]==1){ id++; k++; } tmp.push_back(MP(0,k)); }else{ if(a[id]==0){ p.push_back(tmp); tmp.clear(); tmp.push_back(MP(3,0)); p.push_back(tmp); tmp.clear(); id++; }else{ tmp.push_back(MP(1,a[id])); id++; } } } p.push_back(tmp); id = 0; for(auto& x:p){ if(x.size()==0)continue; if(x[0].first==3){ res[id] = '+'; id++; continue; } int m = x.size(); int sm = 0; ll K = 1; bool fflag = 0; rep(i,m){ if(x[i].first==0){ if(i!=0&&i!=m-1){ sm += x[i].second; } }else{ K *= x[i].second; if(K >= 10000000){ fflag = 1; break; } } } if(fflag || K >= 20*sm){ rep(i,m){ if(x[i].first==0){ if(i==0||i==m-1){ rep(j,x[i].second){ res[id] = '+'; id++; } }else{ rep(j,x[i].second){ res[id] = '*'; id++; } } }else{ res[id] = '*'; id++; if(i==m-2&&x[i+1].first==0){ res[id-1] = '+'; } } } res[id-1] = '+'; }else{ ll mx = 0; int mxbit = 0; ll XX = 1; bool fffff = 0; rep(i,m){ if(x[i].first!=0){ XX *= x[i].second; } if(XX >= 1000000000LL){ fffff = 1; break; } } if(fffff){ mxbit = (1<<m-1)-1; }else{ rep(bit,1<<(m-1)){ UnionFind uf(m); rep(i,m-1){ if((bit>>i)&1){ uf.unite(i,i+1); } } ll S = 0; ll tmp = 1; bool flag = 1; rep(i,m){ if(i==m-1||!uf.same(i,i+1)){ if(flag){ S += x[i].second; }else{ if(x[i].first==0){ }else{ tmp *= x[i].second; } S += tmp; } flag = 1; tmp = 1; }else{ flag = 0; if(x[i].first==0){ }else{ tmp *= x[i].second; } } } if(chmax(mx,S)){ mxbit = bit; } } } UnionFind uf(m); rep(i,m-1){ if((mxbit>>i)&1){ uf.unite(i,i+1); } } bool flag = 0; rep(i,m){ if(i==m-1||!uf.same(i,i+1)){ if(x[i].first==0){ rep(j,x[i].second){ res[id] = '+'; id++; } }else{ res[id] = '+'; id++; } flag = 1; }else{ if(x[i].first==0){ rep(j,x[i].second){ res[id] = '*'; id++; } }else{ res[id] = '*'; id++; } flag = 0; } } } } mod ans = 0; mod PP = 1; rep(i,n){ PP *= a[i]; if(i!=n-1){ if(res[i]=='+'){ ans += PP; PP = 1; } } } ans += PP; cout << ans << endl; return 0; }