結果
問題 | No.1334 Multiply or Add |
ユーザー |
👑 ![]() |
提出日時 | 2021-01-28 17:44:15 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 32 ms / 2,000 ms |
コード長 | 1,177 bytes |
コンパイル時間 | 2,187 ms |
コンパイル使用メモリ | 195,776 KB |
最終ジャッジ日時 | 2025-01-18 08:30:53 |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 71 |
コンパイルメッセージ
main.cpp: In function ‘int main()’: main.cpp:45:14: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘long long unsigned int’ [-Wformat=] 45 | printf("%d\n",(ans+ans_offset)%M); | ~^ ~~~~~~~~~~~~~~~~~~ | | | | int long long unsigned int | %lld 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; }int ans_offset=0;rep(t,2){while((N==0)?false:(A[N-1]==1)){ ans_offset++; N--; }reverse(A,A+N);}if(!large){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)%M);}else{printf("%llu\n",(m+ans_offset)%M);}return 0;}