[ICPC2019西安M] value
Description
给出N个A,B寻找一个集合使价值最大价值即为集合内A的和如果 i,j属于集合内并且 (i>1, j>1, i^k=j(k>1)) 则价值需要减去j的B值
Solution
对于每个非幂数,我们枚举它的所有幂,单独拎出来,因为不同部分的贡献是相互独立的
对于每个非幂数的所有次幂,暴力枚举一遍选谁不选谁
VP 时一开始居然想到 MST 上去了
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 1e5 + 5;
int n, a[N], b[N], v[N];
int solve(vector<pair<int, int>> p)
{
int n = p.size();
int ans = 0;
for (int i = 0; i < (1 << n); i++)
{
bitset<20> s(i);
int tmp = 0;
for (int j = 0; j < n; j++)
if (s[j])
tmp += p[j].first;
for (int j = 0; j < n; j++)
for (int k = 0; k < n; k++)
if (j > k && s[j] && s[k] && (j + 1) % (k + 1) == 0)
tmp -= p[j].second;
ans = max(ans, tmp);
}
return ans;
}
signed main()
{
ios::sync_with_stdio(false);
cin >> n;
for (int i = 1; i <= n; i++)
cin >> a[i];
for (int i = 1; i <= n; i++)
cin >> b[i];
int ans = a[1];
for (int i = 2; i <= n; i++)
{
if (v[i])
continue;
vector<pair<int, int>> vec;
for (int j = i; j <= n; j *= i)
{
v[j] = 1;
vec.push_back({a[j], b[j]});
}
ans += solve(vec);
}
cout << ans << endl;
}