結果

問題 No.1084 積の積
ユーザー 沙耶花沙耶花
提出日時 2020-06-19 21:48:02
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 180 ms / 2,000 ms
コード長 2,277 bytes
コンパイル時間 2,227 ms
コンパイル使用メモリ 205,772 KB
実行使用メモリ 7,664 KB
最終ジャッジ日時 2023-09-16 13:54:15
合計ジャッジ時間 5,291 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,384 KB
testcase_04 AC 180 ms
7,572 KB
testcase_05 AC 9 ms
4,380 KB
testcase_06 AC 180 ms
7,664 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 134 ms
7,568 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 4 ms
4,376 KB
testcase_12 AC 39 ms
5,132 KB
testcase_13 AC 88 ms
6,988 KB
testcase_14 AC 28 ms
4,496 KB
testcase_15 AC 25 ms
4,384 KB
testcase_16 AC 101 ms
7,604 KB
testcase_17 AC 35 ms
5,016 KB
testcase_18 AC 62 ms
5,728 KB
testcase_19 AC 89 ms
7,360 KB
testcase_20 AC 17 ms
4,380 KB
testcase_21 AC 28 ms
4,492 KB
testcase_22 AC 21 ms
4,404 KB
testcase_23 AC 49 ms
5,548 KB
testcase_24 AC 44 ms
5,228 KB
testcase_25 AC 85 ms
7,116 KB
testcase_26 AC 100 ms
7,624 KB
testcase_27 AC 99 ms
7,620 KB
testcase_28 AC 99 ms
7,572 KB
testcase_29 AC 99 ms
7,576 KB
testcase_30 AC 100 ms
7,624 KB
testcase_31 AC 100 ms
7,624 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define modulo 1000000007
#define mod(mod_x) ((((long long)mod_x+modulo))%modulo)
#define Inf 1000000000LL

int beki(int a,long long b,int M = modulo){
	int x = 1;
	while(b!=0){
		if(b&1){
			x=((long long)x*a)%M;
		}
		a=((long long)a*a)%M;
		b>>=1;
	}
	return x;
}


int gyakugen(int a){
	return beki(a,modulo-2);
}

template <typename T,typename F>
struct segtree{
	F func;
	vector<T> v;
	int n;
	
	T init_value;
	
	segtree(int sz,F f,T iv):func(f){
		init_value = iv;
		n=1;
		while(true){
			if(n>=sz)break;
			n*=2;
		}
		v.resize(2*n,init_value);

		for(int i=n-1;i>=0;i--){
			v[i]=func(v[i<<1],v[(i<<1)+1]);
		}
	}
	
	segtree(vector<T> &x,F f,T iv):func(f){
		init_value = iv;
		n=1;
		while(true){
			if(n>=x.size())break;
			n*=2;
		}
		v.resize(2*n,init_value);
		
		for(int i=0;i<x.size();i++){
			v[n+i]=x[i];
		}
		for(int i=n-1;i>=0;i--){
			v[i]=func(v[i<<1],v[(i<<1)+1]);
		}
	}

	void update(int x,T val){
		x+=n;
		v[x]=val;
		while(x>0){
			x>>=1;
			v[x]=func(v[x<<1],v[(x<<1)+1]);
		}
	}
	
	T query(int l,int r){
		if(l>=r)return init_value;
		l+=n;
		r+=n;
		T res1 = init_value;
		T res2 = init_value;
		while(true){
			if(l&1){
				res1=func(res1,v[l++]);
			}
			if(r&1){
				res2=func(v[--r],res2);
			}
			if(l>=r)break;
			l>>=1;r>>=1;
		}
		return func(res1,res2);
	}
	
	void show(){
		int n = 1;
		for(int i=1;i<v.size();i++){
			for(int j=0;j<n;j++){
				if(j!=0)cout<<' ';
				cout<<v[i+j];
			}
			cout<<endl;
			i+=n-1;
			n*=2;
		}
	}
	
};

int main(){
	
	int N;
	cin>>N;
	
	vector<long long> A(N);
	for(int i=0;i<N;i++)scanf("%lld",&A[i]);

	if(A[0]==0){
		cout<<0<<endl;
		return 0;
	}
	
	auto f = [](long long a,long long b){
		return min(a*b,Inf);
	};
	
	segtree<long long,decltype(f)> seg(A,f,1LL);
	
	vector<long long> S(N,0),D(N,0);
	for(int i=0;i<N;i++){
		int ok = i,ng = N;
		while(ng-ok>1){
			int mid = (ok+ng)/2;
			if(seg.query(i,mid+1)<Inf){
				ok = mid;
			}
			else{
				ng = mid;
			}
		}
		S[i] = ok-i+1;
		D[i] --;
		if(i+(ok-i+1)<N)D[i+(ok-i+1)]++;
	}
	
	long long d = 0;
	for(int i=0;i<N;i++){
		if(i!=0){
			S[i] += S[i-1];
			S[i] += d;
		}
		d += D[i];
	}
	
	int ans = 1;
	for(int i=0;i<N;i++){
		ans = mod(ans * beki(A[i],S[i]));
	}
	
	cout<<ans<<endl;
	
	return 0;
}
0