結果
| 問題 |
No.1334 Multiply or Add
|
| コンテスト | |
| ユーザー |
Nachia
|
| 提出日時 | 2021-01-28 17:35:40 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 1,168 bytes |
| コンパイル時間 | 2,022 ms |
| コンパイル使用メモリ | 195,628 KB |
| 最終ジャッジ日時 | 2025-01-18 08:30:30 |
|
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 66 WA * 5 |
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:13:8: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
13 | scanf("%d",&N);
| ~~~~~^~~~~~~~~
main.cpp:14:17: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
14 | rep(i,N) scanf("%d",&A[i]);
| ~~~~~^~~~~~~~~~~~
ソースコード
#include<bits/stdc++.h>
using namespace std;
using LL=long long;
using ULL=unsigned long long;
#define rep(i,n) for(int i=0; i<(n); i++)
const ULL M=1000000007;
int N;
int A[200000];
int main(){
scanf("%d",&N);
rep(i,N) scanf("%d",&A[i]);
ULL m=1;
bool large=false;
rep(i,N){ m*=A[i]; if(m>N*2) large=true; m%=M; }
if(!large){
int ans_offset=0;
rep(t,2){
while((N==0)?false:(A[N-1]==1)){ ans_offset++; N--; }
reverse(A,A+N);
}
vector<int> V={0};
rep(i,N){
if(V.size()%2==1){
if(A[i]==1) V.back()++;
else V.push_back(A[i]);
}
else{
if(A[i]==1) V.push_back(1);
else V.back()*=A[i];
}
}
int ans=0;
struct Node{ int factor,term; } dp[32]={};
int n=V.size()/2; int p=n-1;
rep(i,1<<n){
int p=n-1; while(p>=0&&(1<<(n-1-p))&~i) p--;
if(p!=-1) dp[p+1]={dp[p].factor*V[p*2+1],dp[p].term}; p++;
for(; p<n; p++) dp[p+1]={V[p*2+1],dp[p].term+dp[p].factor+V[p*2]};
ans=max(ans,dp[n].factor+dp[n].term);
}
if(V.size()%2==1) ans+=V.back();
printf("%d\n",ans+ans_offset);
}
else{
printf("%llu\n",m);
}
return 0;
}
Nachia