adad
  • 板块灌水区
  • 楼主shufeng2007
  • 当前回复0
  • 已保存回复0
  • 发布时间2024/8/31 21:58
  • 上次更新2024/8/31 21:58:10
查看原帖
adad
1124817
shufeng2007楼主2024/8/31 21:58
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N=1010;
LL n,x[N],c[N];
unordered_map<LL,LL> dp;//map是对 
unordered_map<LL,LL> vis;//判断有没有存在于vector里面 
vector<LL> s;//存最大公约数 
LL gcd(LL a,LL b){
	if(b==0) return a;
	else return gcd(b,a%b);
}
int main(){
	cin>>n;
	LL tmp=0;
	for(LL i=1;i<=n;i++) {
		cin>>x[i];
		tmp=gcd(tmp,x[i]);
	}
	if(tmp!=1) {
		cout<<"imposib1e"; 
		return 0;
	}
	for(LL i=1;i<=n;i++) cin>>c[i];
		s.push_back(0);
		dp[0]=0;
		vis[0]=1;
		for(LL i=1;i<=n;i++){
			vector<LL> tp;
			LL lens=s.size();
			for(LL j=0;j<lens;j++){
				int g=gcd(x[i],s[j]);
				if(dp[g]==0) dp[g]=dp[s[j]]+c[i];
				else dp[g]=min(dp[g],dp[s[j]]+c[i]);
				if(vis[g]!=1) tp.push_back(g);
			}
			LL lentp=tp.size();
			for(LL k=0;k<lentp;k++){
				if(!vis[tp[k]]){
				s.push_back(tp[k]);
				vis[tp[k]]=1;
			}
		}
	}
	cout<<dp[1];
	return 0;
}
2024/8/31 21:58
加载中...