算法 :搜索。
搜索树的形态,可过 Subtask1 期望得分 。
接下来需要一个结论:
虽然以 为根的子树答案不确定,但节点个数是确定的,因为其中包含多少个点只和质因子个数有关。
算法 :DP。
设 表示以 为根的最小值,则 ,如果用 的检验质因子可以获得 分。
算法 :Subtask4
贪心,发现每次把最大质因子分出去一定最优,这样能使得当前节点的贡献最小且其他子树贡献最小,结合之前算法可获得 分。
算法 :
继续沿用算法 的方法,考虑优化转移。
发现每一个数的因数一定是 的因数,所以检验因数时只用判断 的因数即可。
的因数至多有一万多个,再加上每次只用枚举到根号,所以经测验是可过的,标程最大的点 能出解。
常数优化: 很慢,建议离散化用数组维护。
算法 :
用 Pollard-Rho 优化分解,可能会被卡,但由于这是 CSP 难度的模拟赛,所以不做延伸。
#include<cstdio>
#include<algorithm>
#include<map>
#include<cmath>
using namespace std;
#define re register
long long n,pri[1000002],num[1000002],dp[1000002];
map<long long,bool>vis;
map<long long,int>pos;
int cntt,cnttt,m,cnt;
inline int dfs(re long long x){
if(pos.count(x))return pos[x];
re int xx=sqrt(x);pos[x]=++cntt;re int y=cntt;
dp[y]=4e18;
for(re int i=1;i<=cnt&&pri[i]<=xx;++i){++cnttt;
if(x%pri[i]==0){
re int ls=dfs(pri[i]),rs=dfs(x/pri[i]);
num[y]=num[ls]+num[rs]+1;
dp[y]=min(dp[y],(num[ls]+1)*(num[rs]+1)*x+dp[ls]+dp[rs]);
}
}
if(!num[y])num[y]=1,dp[y]=x;
// printf("%lld %d\n",x,y);
if(vis.count(x))dp[y]=4e18;
return y;
}
signed main(){
// freopen("a30.in","r",stdin);
// freopen("a30.out","w",stdout);
scanf("%lld%d",&n,&m);
re long long x;
for(re int i=1;i<=m;++i)scanf("%lld",&x),vis[x]=1;
if(vis.count(n))return puts("-1"),0;
re int xx=sqrt(n);
for(re int i=2;i<=xx;++i)if(n%i==0)pri[++cnt]=i;
dfs(n);
printf("%lld",dp[pos[n]]==4e18?-1:dp[pos[n]]);
}