求助卡常或求复杂度
查看原帖
求助卡常或求复杂度
593299
Qerucy楼主2024/9/1 18:21

rt,multiset做法,最后两个点慢了0.08s。

代码:

#include<bits/stdc++.h>

using namespace std;

#define int long long

const int mod=317847191;

int n,m; 
int x;
char c;
int phimod;
int ans=1;

multiset<int>s;

void euler(){
	int n=mod;
	phimod=mod;
	for(int i=2;i*i<=n;i++){
		if(n%i==0){
			phimod=phimod/i*(i-1);
			while(n%i==0) n/=i;
		}
	}
	if(n>1) phimod=phimod/n*(n-1);
}

int qpow(int a,int b){
	if(b==0) return 1;
	if(b%2==0){
		int qwq=qpow(a,b/2);
		return qwq*qwq%mod;
	}
	return a*qpow(a,b-1)%mod;
}

int inv(int x){
	return qpow(x,phimod-1);
}

signed main(){
	scanf("%lld%lld",&n,&m);
	euler();
	for(int i=1;i<=n;i++){
		scanf("%lld",&x);
		ans*=x;
		ans%=mod;
		s.insert(x);
	}
	while(m--){
		scanf(" %c",&c);
		if(c=='M'){
			multiset<int>::iterator it=s.end();
			it--;
			printf("%lld\n",qpow(*it,*s.begin()));
		}
		if(c=='D'){
			scanf("%lld",&x);
			ans=ans*inv(x)%mod;
			s.erase(x);
		}
		if(c=='S'){
			printf("%lld\n",*s.begin());
		}
		if(c=='B'){
			multiset<int>::iterator it=s.end();
			it--;
			printf("%lld\n",*it);
		}
		if(c=='T'){
			printf("%lld\n",ans);
		}
	}
	return 0;
}

2024/9/1 18:21
加载中...