Array Equalizer(莫比乌斯反演)
发布时间:2024年01月18日
1605E - Array Equalizer
思路:
?
代码:
?#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
using namespace std;
const int N = 2e5+100;
#define LL long long
int pre[N],su[N],mu[N],cn,f[N],a[N],b[N],c[N],t1[N],t2[N],c1,c2;
LL ans,n, tt1[N], tt2[N];
void into()
{
?? ?mu[1] = 1;
?? ?su[0] = su[1] = 1;
?? ?for (int i = 2; i < N; i++)?
?? ?{
?? ??? ?if (!su[i]) pre[++cn] = i,mu[i]=-1;
?? ??? ?for (int j = 1; j <= cn && pre[j] * i < N; j++)
?? ??? ?{
?? ??? ??? ?su[pre[j] * i] = 1;
?? ??? ??? ?if (i % pre[j] == 0) break;
?? ??? ??? ?mu[i * pre[j]] = mu[i] * -1;
?? ??? ?}
?? ?}
}
int main() {
?? ?into();
?? ?cin >> n;
?? ?for (int i = 1; i <= n; i++)
?? ??? ?cin >> a[i];
?? ?for (int i = 1; i <= n; i++)
?? ??? ?cin >> b[i];
?? ?int T;
?? ?cin >>T;
?? ?for (int i = 2; i <= n; i++)
?? ??? ?c[i] = b[i] - a[i];
?? ?for (int d = 1; d <= n; d++)
?? ?{
?? ??? ?if (!mu[d]) continue;
?? ??? ?for (int x = 2 * d; x <= n; x += d)
?? ??? ??? ?f[x] += mu[d] * c[x / d];
?? ?}
?? ?for (int i = 1; i <=n; i++)
?? ?{
?? ??? ?if (!mu[i])
?? ??? ??? ?ans += abs(f[i]);
?? ??? ?else if (mu[i] == 1)
?? ??? ??? ?t1[++c1] = f[i];
?? ??? ?else
?? ??? ??? ?t2[++c2] = f[i];
?? ?}
?? ?sort(t1 + 1, t1 + 1 + c1);
?? ?sort(t2 + 1, t2 + 1 + c2);
?? ?for (int i = 1; i <= c1; i++)
?? ??? ?tt1[i] = tt1[i - 1] + t1[i];
?? ?for (int i = 1; i <= c2; i++)
?? ??? ?tt2[i] = tt2[i - 1] + t2[i];
?? ?LL ass = ans;
?? ??? ?while (T--)
?? ??? ?{
?? ??? ??? ?ass = ans;
?? ??? ?cin >> b[1];
?? ??? ?c[1] = b[1] - a[1];
?? ??? ?int l = 1, r = c1, mid, an=0;
?? ??? ?while (l <= r)
?? ??? ?{
?? ??? ??? ?mid = (l + r) >> 1;
?? ??? ??? ?if (t1[mid] + c[1] < 0)
?? ??? ??? ??? ?an = mid, l = mid + 1;
?? ??? ??? ?else r = mid - 1;?
?? ??? ?}
?? ??? ?ass += abs((LL)tt1[an] + (LL)an * c[1]) + abs((LL)tt1[c1] - tt1[an] + (LL)(c1-an) * c[1]);
?? ??? ?l = 1, r = c2;
?? ??? ?an = 0;
?? ??? ?while (l <= r)
?? ??? ?{
?? ??? ??? ?mid = (l + r) >> 1;
?? ??? ??? ?if (t2[mid] -c[1] <0)
?? ??? ??? ??? ?an = mid, l = mid + 1;
?? ??? ??? ?else r = mid - 1;
?? ??? ?}
?? ??? ?ass += abs((LL)tt2[an] - (LL)an * c[1]) + abs((LL)tt2[c2] - tt2[an] - (LL)(c2 - an) * c[1]);
?? ??? ?cout << ass << endl;
?? ?}
?? ?return 0;
}
文章来源:https://blog.csdn.net/yusen_123/article/details/135661365
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:chenni525@qq.com进行投诉反馈,一经查实,立即删除!