結果
問題 | No.1334 Multiply or Add |
ユーザー | kotatsugame |
提出日時 | 2021-01-08 22:51:00 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 3,896 bytes |
コンパイル時間 | 974 ms |
コンパイル使用メモリ | 76,544 KB |
実行使用メモリ | 5,248 KB |
最終ジャッジ日時 | 2024-11-16 14:24:39 |
合計ジャッジ時間 | 5,611 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
5,248 KB |
testcase_01 | AC | 1 ms
5,248 KB |
testcase_02 | AC | 1 ms
5,248 KB |
testcase_03 | WA | - |
testcase_04 | WA | - |
testcase_05 | WA | - |
testcase_06 | WA | - |
testcase_07 | WA | - |
testcase_08 | WA | - |
testcase_09 | WA | - |
testcase_10 | WA | - |
testcase_11 | WA | - |
testcase_12 | WA | - |
testcase_13 | WA | - |
testcase_14 | WA | - |
testcase_15 | WA | - |
testcase_16 | WA | - |
testcase_17 | WA | - |
testcase_18 | WA | - |
testcase_19 | WA | - |
testcase_20 | WA | - |
testcase_21 | WA | - |
testcase_22 | WA | - |
testcase_23 | WA | - |
testcase_24 | WA | - |
testcase_25 | WA | - |
testcase_26 | WA | - |
testcase_27 | WA | - |
testcase_28 | WA | - |
testcase_29 | WA | - |
testcase_30 | WA | - |
testcase_31 | WA | - |
testcase_32 | WA | - |
testcase_33 | WA | - |
testcase_34 | WA | - |
testcase_35 | WA | - |
testcase_36 | WA | - |
testcase_37 | WA | - |
testcase_38 | WA | - |
testcase_39 | AC | 2 ms
5,248 KB |
testcase_40 | AC | 9 ms
5,248 KB |
testcase_41 | AC | 4 ms
5,248 KB |
testcase_42 | AC | 4 ms
5,248 KB |
testcase_43 | WA | - |
testcase_44 | AC | 9 ms
5,248 KB |
testcase_45 | AC | 5 ms
5,248 KB |
testcase_46 | AC | 9 ms
5,248 KB |
testcase_47 | AC | 14 ms
5,248 KB |
testcase_48 | AC | 10 ms
5,248 KB |
testcase_49 | WA | - |
testcase_50 | RE | - |
testcase_51 | RE | - |
testcase_52 | RE | - |
testcase_53 | RE | - |
testcase_54 | RE | - |
testcase_55 | RE | - |
testcase_56 | AC | 2 ms
5,248 KB |
testcase_57 | AC | 2 ms
5,248 KB |
testcase_58 | AC | 1 ms
5,248 KB |
testcase_59 | AC | 2 ms
5,248 KB |
testcase_60 | AC | 2 ms
5,248 KB |
testcase_61 | AC | 2 ms
5,248 KB |
testcase_62 | AC | 2 ms
5,248 KB |
testcase_63 | AC | 2 ms
5,248 KB |
testcase_64 | AC | 2 ms
5,248 KB |
testcase_65 | AC | 1 ms
5,248 KB |
testcase_66 | RE | - |
testcase_67 | WA | - |
testcase_68 | AC | 2 ms
5,248 KB |
testcase_69 | RE | - |
testcase_70 | RE | - |
コンパイルメッセージ
a.cpp:121:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type] a.cpp: In function 'mint rec(int, int)': a.cpp:90:18: warning: 'ansbit' may be used uninitialized [-Wmaybe-uninitialized] a.cpp:48:13: note: 'ansbit' was declared here
ソースコード
#line 1 "a.cpp" #include<cstdio> #include<vector> #include<cstring> #include<algorithm> using namespace std; #line 2 "/home/kotatsugame/library/math/modint.cpp" #include<iostream> #include<utility> template<int m> struct modint{ unsigned int x; constexpr modint()noexcept:x(){} template<typename T> constexpr modint(T x_)noexcept:x((x_%=m)<0?x_+m:x_){} constexpr unsigned int val()const noexcept{return x;} constexpr modint&operator++()noexcept{if(++x==m)x=0;return*this;} constexpr modint&operator--()noexcept{if(x==0)x=m;--x;return*this;} constexpr modint operator++(int)noexcept{modint res=*this;++*this;return res;} constexpr modint operator--(int)noexcept{modint res=*this;--*this;return res;} constexpr modint&operator+=(const modint&a)noexcept{x+=a.x;if(x>=m)x-=m;return*this;} constexpr modint&operator-=(const modint&a)noexcept{if(x<a.x)x+=m;x-=a.x;return*this;} constexpr modint&operator*=(const modint&a)noexcept{x=(unsigned long long)x*a.x%m;return*this;} constexpr modint&operator/=(const modint&a)noexcept{return*this*=a.inv();} constexpr modint operator+()const noexcept{return*this;} constexpr modint operator-()const noexcept{return modint()-*this;} constexpr modint pow(long long n)const noexcept { if(n<0)return pow(-n).inv(); modint x=*this,r=1; for(;n;x*=x,n>>=1)if(n&1)r*=x; return r; } constexpr modint inv()const noexcept { int s=x,t=m,x=1,u=0; while(t) { int k=s/t; s-=k*t; swap(s,t); x-=k*u; swap(x,u); } return modint(x); } friend constexpr modint operator+(const modint&a,const modint&b){return modint(a)+=b;} friend constexpr modint operator-(const modint&a,const modint&b){return modint(a)-=b;} friend constexpr modint operator*(const modint&a,const modint&b){return modint(a)*=b;} friend constexpr modint operator/(const modint&a,const modint&b){return modint(a)/=b;} friend constexpr bool operator==(const modint&a,const modint&b){return a.x==b.x;} friend constexpr bool operator!=(const modint&a,const modint&b){return a.x!=b.x;} friend ostream&operator<<(ostream&os,const modint&a){return os<<a.x;} friend istream&operator>>(istream&is,modint&a){long long v;is>>v;a=modint(v);return is;} }; #line 7 "a.cpp" using mint=modint<(int)1e9+7>; int N; int A[1<<17]; mint rec(int L,int R) { int sum=(R-L)*2; int prod=1; for(int i=L;i<R;i++) { prod*=A[i]; if(prod>=sum)prod=sum; } if(prod>=sum) { mint ret=1; for(int i=L;i<R;i++) { ret*=A[i]; } return ret; } vector<pair<int,pair<int,int> > >B; for(int i=L;i<R;) { if(A[i]==1) { int j=i; while(j<R&&A[j]==1)j++; B.push_back(make_pair(-(j-i),make_pair(i,j))); i=j; } else { int j=i; int prod=1; while(j<R&&A[j]!=1)prod*=A[j++]; B.push_back(make_pair(prod,make_pair(i,j))); i=j; } } int n=B.size()-1; int ans=-1,ansbit; for(int bit=0;bit<1<<n;bit++) { int now=B[0].first; int sum=0; for(int i=0;i<n;i++) { if(bit>>i&1) { if(now<0)now=1; if(B[i+1].first>0)now*=B[i+1].first; } else { sum+=abs(now); now=B[i+1].first; } } sum+=abs(now); if(ans<sum) { ans=sum; ansbit=bit; } } mint ret=0; for(int i=0;i<=n;i++) { if(B[i].first>0) { int l=B[i].second.first,r=B[i].second.second; mint now=1; for(int j=l;j<r;j++) { now*=A[j]; } ret+=now; } else { bool add=true; if(i>0&&ansbit>>i-1&1)add=false; if(i<n&&ansbit>>i&1)add=false; int l=B[i].second.first,r=B[i].second.second; mint now=add?0:1; for(int j=l;j<r;j++) { if(add)now+=A[j]; else now*=A[j]; } ret+=now; } } return ret; } mint func(int L,int R) { mint ret=0; while(L<R&&A[L]==1) { ret++; L++; } if(L==R)return ret; int r=R; while(A[r-1]==1) { ret++; r--; } ret+=rec(L,r); return ret; } main() { scanf("%d",&N); for(int i=0;i<N;i++)scanf("%d",&A[i]); printf("%u\n",func(0,N).val()); }