QOJ.ac

QOJ

IDSubmission IDProblemHackerOwnerResultSubmit timeJudge time
#1136#713663#9545. Magical Sethos_lyricucup-team5052Failed.2024-11-05 20:42:572024-11-05 20:42:57

Details

Extra Test:

Accepted
time: 430ms
memory: 26900kb

input:

300
921080160 964323360 879278400 882882000 754593840 949188240 797837040 836035200 871350480 714954240 673152480 868467600 815134320 896575680 998197200 834593760 697656960 67108864 819458640 740900160 772611840 833152320 962161200 707747040 931890960 886485600 797116320 830269440 701981280 8021613...

output:

3277

result:

ok single line: '3277'

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#713663#9545. Magical Setucup-team5052AC ✓37ms16192kbC++142.9kb2024-11-05 20:13:252024-11-05 20:20:43

answer

# include <bits/stdc++.h>
using namespace std;
constexpr int INF=1e9;
int a[310],w[4100010],X[410010],h[410010],dis[410010],cur[410010],head[410010],nxt[1000010],tot=1;
bool vis[410010];
struct Edge
{
	int v,w,c,f;
	Edge()=default;
	Edge(int v,int w,int c):v(v),w(w),c(c),f(0){}
}edge[1000010];
struct Node
{
	int v,w;
	Node(int v,int w):v(v),w(w){}
	bool operator<(Node t)const{return w>t.w;}
};
void chkmin(int &x,int y){x=min(x,y);}
void addedge(int u,int v,int w,int c)
{
	nxt[++tot]=head[u];
	edge[tot]=Edge(v,w,c);
	head[u]=tot;
}
void addE(int u,int v,int w,int c)
{
	addedge(u,v,w,c);
	addedge(v,u,-w,0);
}
void spfa(int s,int t,int n)
{
	fill(dis+1,dis+n+1,INF);
	dis[s]=0;
	for(int u=n;u>=1;u--)
		for(int i=head[u];i;i=nxt[i])
		{
			int v=edge[i].v,w=edge[i].w,c=edge[i].c,f=edge[i].f;
			if(c>f) chkmin(dis[v],dis[u]+w);
		}
	for(int i=1;i<=n;i++)
		if(dis[i]<INF) h[i]+=dis[i];
}
bool Dij(int s,int t,int n)
{
	fill(dis+1,dis+n+1,INF);
	dis[s]=0;
	priority_queue<Node> pq;
	pq.emplace(s,0);
	while(!pq.empty())
	{
		int u=pq.top().v;pq.pop();
		if(vis[u]) continue;
		vis[u]=1;
		for(int i=head[u];i;i=nxt[i])
		{
			int v=edge[i].v,w=edge[i].w+h[u]-h[v],c=edge[i].c,f=edge[i].f;
			if(c>f) assert(w>=0);
			if(c>f && dis[u]+w<dis[v]) pq.emplace(v,dis[v]=dis[u]+w);
		}
	}
	return dis[t]<INF;
}
int dfs(int u,int t,int minn)
{
	if(u==t || minn==0) return minn;
	vis[u]=1;
	int ans=0,x;
	for(int &i=cur[u];i;i=nxt[i])
	{
		int v=edge[i].v,w=edge[i].w+h[u]-h[v],c=edge[i].c,f=edge[i].f;
		if(c>f && !vis[v] && dis[u]+w==dis[v] && (x=dfs(v,t,min(minn,c-f)))>0)
		{
			edge[i].f+=x;edge[i^1].f-=x;
			ans+=x;minn-=x;
		}
		if(minn==0) break;
	}
	vis[u]=0;
	return ans;
}
pair<int,int> MCMF(int s,int t,int n)
{
	int flow=0,cost=0;
	spfa(s,t,n);
	while(Dij(s,t,n))
	{
		fill(vis+1,vis+n+1,0);
		copy(head+1,head+n+1,cur+1);
		int f=dfs(s,t,INF);
		flow+=f;cost+=f*(dis[t]+h[t]);
		for(int i=1;i<=n;i++)
			if(dis[i]<INF) h[i]+=dis[i];
	}
	return {flow,cost};
}
int main()
{
	ios::sync_with_stdio(false);cin.tie(nullptr);
	int n,N=0;
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		cin>>a[i];
		for(int j=1;j*j<=a[i];j++)
			if(a[i]%j==0)
			{
				X[++N]=j;
				if(j*j<a[i]) X[++N]=a[i]/j;
			}
	}
	sort(X+1,X+N+1);
	N=unique(X+1,X+N+1)-X-1;
	auto id=[N](int x){return lower_bound(X+1,X+N+1,x)-X;};
	int s=N+1,t=s+1;
	for(int i=1;i<=N;i++)
	{
		int x=X[i];
		for(int j=2;j*j<=x;j++)
			for(;x%j==0;x/=j) w[i]++;
		if(x>1) w[i]++;
	}
	for(int i=1;i<=n;i++) addE(s,id(a[i]),0,1);
	for(int i=1;i<=N;i++) addE(i,t,0,1);
	for(int i=1;i<=n;i++)
	{
		int u=id(a[i]);
		for(int j=1;j*j<=a[i];j++)
			if(a[i]%j==0)
			{
				int v=id(j);
				addE(u,v,w[v]-w[u],n);
				if(j*j<a[i])
				{
					v=id(a[i]/j);
					addE(u,v,w[v]-w[u],n);
				}
			}
	}
	cout<<-MCMF(s,t,t).second<<"\n";
	return 0;
}