BZOJ 3853: GCD Array

修改操作相当于对所有 \(x\) 进行 \(a_x=a_x+v[(x,n)=d]\)
\(v[(x,n)=d] = \sum\limits_{k|\frac{n}{d},kd|x}\mu(k)v\)
枚举 \(\dfrac{n}{d}\) 的因子 \(k\),那么就是对 \(kd\) 的所有倍数进行修改
那么直接维护一个数组 \(f\)\(a_i=\sum\limits_{j|i}f_j\)
那么修改操作就是在 \(f\) 上进行单点修改
查询则为 \(\sum\limits_{i=1}^xa_i=\sum\limits_{i=1}^x\sum\limits_{k|i}f_k=\sum\limits_{k=1}^x f_k\lfloor \dfrac{x}{k} \rfloor\)

#include <bits/stdc++.h>
#define pb push_back
#define fi first
#define se second
#define pii pair<int, int>
#define pli pair<ll, int>
#define lp p << 1
#define rp p << 1 | 1
#define mid ((l + r) >> 1)
#define lowbit(i) ((i) & (-i))
#define ll long long
#define ull unsigned long long
#define db double
#define rep(i,a,b) for(int i=a;i<b;i++)
#define per(i,a,b) for(int i=b-1;i>=a;i--)
#define Edg int ccnt=1,head[N],to[E],ne[E];void addd(int u,int v){to[++ccnt]=v;ne[ccnt]=head[u];head[u]=ccnt;}void add(int u,int v){addd(u,v);addd(v,u);}
#define Edgc int ccnt=1,head[N],to[E],ne[E],c[E];void addd(int u,int v,int w){to[++ccnt]=v;ne[ccnt]=head[u];c[ccnt]=w;head[u]=ccnt;}void add(int u,int v,int w){addd(u,v,w);addd(v,u,w);}
#define es(u,i,v) for(int i=head[u],v=to[i];i;i=ne[i],v=to[i])
const int MOD = 1e9 + 7;
void M(int &x) {if (x >= MOD)x -= MOD; if (x < 0)x += MOD;}
int qp(int a, int b = MOD - 2) {int ans = 1; for (; b; a = 1LL * a * a % MOD, b >>= 1)if (b & 1)ans = 1LL * ans * a % MOD; return ans % MOD;}
template<class T>T gcd(T a, T b) { while (b) { a %= b; std::swap(a, b); } return a; }
template<class T>bool chkmin(T &a, T b) { return a > b ? a = b, 1 : 0; }
template<class T>bool chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; }
char buf[1 << 21], *p1 = buf, *p2 = buf;
inline char getc() {
	return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++;
}
inline int _() {
	int x = 0, f = 1; char ch = getc();
	while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getc(); }
	while (ch >= '0' && ch <= '9') { x = x * 10ll + ch - 48; ch = getc(); }
	return x * f;
}

const int N = 2e5 + 7, E = 2e6 + 7;
Edg
int prime[N / 10], prin, n, q;
short mu[N];
void init() {
	static bool vis[N];
	mu[1] = 1;
	rep (i, 2, N) {
		if (!vis[i]) prime[++prin] = i, mu[i] = -1;
		rep (j, 1, prin + 1) {
			if (1LL * i * prime[j] >= N) break;
			vis[i * prime[j]] = 1;
			if (i % prime[j] == 0) break;
			mu[i * prime[j]] = -mu[i];
		}
	}
	rep (i, 1, N) {
		if (mu[i])
			for (int j = i; j < N; j += i) addd(j, i);
	}
}

ll bit[50007];
void modify(int x, int v) {
	for (int i = x; i <= n; i += lowbit(i))
		bit[i] += v;
}
ll query(int x) {
	ll ans = 0;
	for (int i = x; i; i -= lowbit(i))
		ans += bit[i];
	return ans;
}

void solve() {
	rep (i, 1, n + 3) bit[i] = 0;
	while (q--) {
		int opt = _();
		if (opt == 1) {
			int nn = _(), d = _(), v = _();
			if (nn % d) continue;
			nn /= d;
			es (nn, i, k) {
				if (k * d <= n) modify(k * d, mu[k] * v);
			}
		} else {
			int x = _();
			ll ans = 0;
			for (int i = 1, j; i <= x; i = j + 1) {
				j = x / (x / i);
				ans += (ll)(query(j) - query(i - 1)) * (x / i);
			}
			printf("%lld\n", ans);
		}
	}
}

int main() {
#ifdef LOCAL
	freopen("ans.out", "w", stdout);
#endif
	init();
	int kase = 0;
	while (1) {
		n = _(), q = _();
		if (!n) break;
		printf("Case #%d:\n", ++kase);
		solve();
	}
	return 0;
}
posted @ 2020-03-02 21:23  Mrzdtz220  阅读(89)  评论(0编辑  收藏  举报