結果

問題 No.1334 Multiply or Add
ユーザー kotatsugamekotatsugame
提出日時 2021-01-08 23:03:41
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 30 ms / 2,000 ms
コード長 3,493 bytes
コンパイル時間 860 ms
コンパイル使用メモリ 74,364 KB
実行使用メモリ 4,456 KB
最終ジャッジ日時 2023-08-10 11:30:21
合計ジャッジ時間 4,578 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,384 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 17 ms
4,424 KB
testcase_04 AC 17 ms
4,380 KB
testcase_05 AC 17 ms
4,384 KB
testcase_06 AC 17 ms
4,384 KB
testcase_07 AC 17 ms
4,384 KB
testcase_08 AC 16 ms
4,384 KB
testcase_09 AC 17 ms
4,384 KB
testcase_10 AC 17 ms
4,380 KB
testcase_11 AC 17 ms
4,384 KB
testcase_12 AC 16 ms
4,380 KB
testcase_13 AC 16 ms
4,380 KB
testcase_14 AC 17 ms
4,384 KB
testcase_15 AC 17 ms
4,384 KB
testcase_16 AC 17 ms
4,380 KB
testcase_17 AC 17 ms
4,388 KB
testcase_18 AC 17 ms
4,384 KB
testcase_19 AC 24 ms
4,384 KB
testcase_20 AC 24 ms
4,380 KB
testcase_21 AC 24 ms
4,380 KB
testcase_22 AC 16 ms
4,384 KB
testcase_23 AC 17 ms
4,384 KB
testcase_24 AC 17 ms
4,384 KB
testcase_25 AC 16 ms
4,456 KB
testcase_26 AC 16 ms
4,384 KB
testcase_27 AC 16 ms
4,384 KB
testcase_28 AC 17 ms
4,380 KB
testcase_29 AC 17 ms
4,380 KB
testcase_30 AC 17 ms
4,384 KB
testcase_31 AC 17 ms
4,380 KB
testcase_32 AC 16 ms
4,436 KB
testcase_33 AC 16 ms
4,384 KB
testcase_34 AC 17 ms
4,380 KB
testcase_35 AC 17 ms
4,384 KB
testcase_36 AC 17 ms
4,428 KB
testcase_37 AC 17 ms
4,384 KB
testcase_38 AC 16 ms
4,384 KB
testcase_39 AC 1 ms
4,380 KB
testcase_40 AC 8 ms
4,380 KB
testcase_41 AC 4 ms
4,380 KB
testcase_42 AC 4 ms
4,380 KB
testcase_43 AC 12 ms
4,384 KB
testcase_44 AC 8 ms
4,380 KB
testcase_45 AC 4 ms
4,384 KB
testcase_46 AC 8 ms
4,380 KB
testcase_47 AC 11 ms
4,384 KB
testcase_48 AC 9 ms
4,380 KB
testcase_49 AC 14 ms
4,384 KB
testcase_50 AC 29 ms
4,380 KB
testcase_51 AC 29 ms
4,380 KB
testcase_52 AC 29 ms
4,444 KB
testcase_53 AC 29 ms
4,384 KB
testcase_54 AC 30 ms
4,388 KB
testcase_55 AC 30 ms
4,380 KB
testcase_56 AC 1 ms
4,384 KB
testcase_57 AC 2 ms
4,384 KB
testcase_58 AC 2 ms
4,384 KB
testcase_59 AC 1 ms
4,380 KB
testcase_60 AC 2 ms
4,380 KB
testcase_61 AC 2 ms
4,380 KB
testcase_62 AC 2 ms
4,384 KB
testcase_63 AC 2 ms
4,380 KB
testcase_64 AC 1 ms
4,384 KB
testcase_65 AC 2 ms
4,380 KB
testcase_66 AC 24 ms
4,380 KB
testcase_67 AC 16 ms
4,380 KB
testcase_68 AC 2 ms
4,384 KB
testcase_69 AC 27 ms
4,380 KB
testcase_70 AC 25 ms
4,380 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
a.cpp:110:1: 警告: ISO C++ では型の無い ‘main’ の宣言を禁止しています [-Wreturn-type]
a.cpp: 関数 ‘mint rec(int, int)’ 内:
a.cpp:78:12: 警告: ‘ansbit’ may be used uninitialized [-Wmaybe-uninitialized]
a.cpp:49:13: 備考: ‘ansbit’ はここで定義されています

ソースコード

diff #

#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[2<<17];
mint rec(int L,int R)
{
	int sum=(R-L)*2;
	long 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<int>B;
	vector<int>C;
	for(int i=L;i<R;)
	{
		if(A[i]==1)
		{
			int j=i;
			while(j<R&&A[j]==1)j++;
			C.push_back(j-i);
			i=j;
		}
		else
		{
			int j=i;
			int prod=1;
			while(j<R&&A[j]!=1)prod*=A[j++];
			B.push_back(prod);
			i=j;
		}
	}
	int n=B.size()-1;
	int ans=-1,ansbit;
	for(int bit=0;bit<1<<n;bit++)
	{
		int now=B[0];
		int sum=0;
		for(int i=0;i<n;i++)
		{
			if(bit>>i&1)
			{
				now*=B[i+1];
			}
			else
			{
				sum+=now;
				sum+=C[i];
				now=B[i+1];
			}
		}
		sum+=now;
		if(ans<sum)
		{
			ans=sum;
			ansbit=bit;
		}
	}
	mint ret=0;
	int now=B[0];
	for(int i=0;i<n;i++)
	{
		if(ansbit>>i&1)
		{
			now*=B[i+1];
		}
		else
		{
			ret+=now;
			ret+=C[i];
			now=B[i+1];
		}
	}
	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());
}
0