QOJ.ac

QOJ

IDSubmission IDProblemHackerOwnerResultSubmit timeJudge time
#1078#682868#9527. A Brand New Geometric Problemucup-team4504skcSuccess!2024-10-28 01:30:092024-10-28 01:30:10

Details

Extra Test:

Time Limit Exceeded

input:

100000 10000 9340531200
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...

output:


result:


IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#682868#9527. A Brand New Geometric ProblemskcTL 29ms4564kbC++141.9kb2024-10-27 17:38:092024-11-06 13:18:57

answer

#include<bits/stdc++.h>
using namespace std;
constexpr int N=100000,M=10000;
long long a[N+10];
vector<long long> facs,pfacs;
map<long long,int> id;
long long minsum[M+10];
int acnt[M+10];
void fact(long long x){
	long long tmp=x;
	int S=sqrt(x),i;
	for(i=1;i<=S;++i){
		if(x%i==0) facs.push_back(i);
		if(i>1&&tmp%i==0){
			pfacs.push_back(i);
			while(tmp%i==0) tmp/=i;
		}
	}
	if(tmp>1) pfacs.push_back(tmp);
	for(i=(int)facs.size()-1;i>=0;--i){
		if(facs[i]*facs[i]!=x) facs.push_back(x/facs[i]);
	}
}
long long getminsum(long long x){
	long long rv=0;
	for(auto i:pfacs){
		while(x%i==0) rv+=i,x/=i;
	}
	return rv;
}
long long ans,anstr;
vector<int> rec;
void dfs(long long val,long long max_,long long rem,int used,int rema){
	if(val==1){
		long long o=min(rem,(long long)acnt[0]);
		long long tans=used+(rem-o)+(rema-o);
		if(tans<ans){
//			cout<<tans<<": ";
//			for(auto i:rec) cout<<i<<' ';
//			cout<<'\n';
			ans=tans;
		}
		return;
	}
	int i;
	for(i=id[max_];i>=1;--i){
		if(val%facs[i]) continue;
		int tid=id[val/facs[i]];
		if(facs[i]+minsum[tid]>rem) continue;
		if(used+(rem-facs[i]-val/facs[i]-rema)>ans) continue;
//		if(used+(rem-facs[i]*(2+(int)ceil(log(val/facs[i])/log(facs[i])))-rema)>ans) continue;
		rec.push_back(facs[i]);
		if(acnt[i]){
			--acnt[i];
			dfs(val/facs[i],facs[i],rem-facs[i],used,rema-1);
			++acnt[i];
		}
		else{
			dfs(val/facs[i],facs[i],rem-facs[i],used+1,rema);
		}
		rec.pop_back();
	}
}
int main(){
	int n;
	long long S,M;
	cin>>n>>S>>M;
	int i;
	for(i=1;i<=n;++i) cin>>a[i];
	fact(M);
	int m=facs.size();
	for(i=0;i<m;++i){
		id[facs[i]]=i;
	}
	for(i=1;i<=n;++i){
		if(M%a[i]==0) ++acnt[id[a[i]]];
		else ++anstr;
	}
	for(i=0;i<m;++i){
		minsum[i]=getminsum(facs[i]);
	}
	if(minsum[m-1]>S){
		cout<<-1<<'\n';
		return 0;
	}
	ans=S+M+n;
	dfs(M,M,S,0,n-anstr);
	cout<<ans+anstr<<'\n';
	return 0;
}