cf510 D. Fox And Jumping(dp)
作者:互联网
题意:
给定数组 \(a_i\),选每个数有代价 \(c_i\)。在数组中选若干个互质的数,求最小代价。
最小代价互质组
\(1\le n \le 300\)
思路:
\(mp[g]\) 记录使得公因子为 \(g\) 的最小代价。数值比较离散所以开map记录。然后直接dp即可。
const int N = 310;
int n, a[N], c[N];
map<int, int> mp;
signed main()
{
scanf("%d", &n);
for(int i = 1; i <= n; i++) scanf("%d", &a[i]);
for(int i = 1; i <= n; i++) scanf("%d", &c[i]);
for(int i = 1; i <= n; i++)
{
if(!mp[a[i]]) mp[a[i]] = c[i];
else mp[a[i]] = min(mp[a[i]], c[i]);
for(auto &[G,C] : mp)
{
int g = __gcd(G, a[i]);
if(!mp[g]) mp[g] = c[i] + C;
else mp[g] = min(mp[g], c[i] + C);
}
}
if(!mp[1]) mp[1] = -1;
printf("%d", mp[1]);
}
标签:map,le,int,互质,Fox,Jumping,代价,dp 来源: https://www.cnblogs.com/wushansinger/p/15862492.html