[NOI 2010]能量采集

Description

题库链接

给你一个 \(n\times m\) 的坐标轴。对于坐标轴的每一个正整数整点 \((x,y)\) 其对答案产生的贡献为 \(2k+1\) ,其中 \(k\) 表示这个点与坐标原点连线,线段穿过了除端点外的 \(k\) 个点。求所有点的贡献和。

\(1\leq n,m \leq 100000\)

Solution

容易发现 \(k=\gcd(x,y)-1\) ,故原式等于求 \[\begin{aligned}&\sum_{i=1}^n\sum_{j=1}^m(2(\gcd(i,j)-1)+1)\\=&2\sum_{i=1}^n\sum_{j=1}^m\gcd(i,j)-nm\end{aligned}\]

记 \(f(n,m)=\sum\limits_{i=1}^n\sum\limits_{j=1}^m\gcd(i,j)\) ,考虑如何求 \(f(n,m)\) \[\begin{aligned}\Rightarrow&\sum_{d=1}^{min\{n,m\}}d\sum_{i=1}^{\left\lfloor\frac{n}{d}\right\rfloor}\sum_{j=1}^{\left\lfloor\frac{m}{d}\right\rfloor}[\gcd(i,j)=1]\\=&\sum_{d=1}^{min\{n,m\}}d\sum_{i=1}^{\left\lfloor\frac{n}{d}\right\rfloor}\sum_{j=1}^{\left\lfloor\frac{m}{d}\right\rfloor}\sum_{k\mid \gcd(i,j)}\mu(k)\\=&\sum_{d=1}^{min\{n,m\}}d\sum_{k=1}^{min\left\{\left\lfloor\frac{n}{d}\right\rfloor,\left\lfloor\frac{m}{d}\right\rfloor\right\}}\mu(k)\left\lfloor\frac{n}{kd}\right\rfloor\left\lfloor\frac{m}{kd}\right\rfloor\end{aligned}\]

令 \(T=kd\) ,枚举 \(T\) \[\begin{aligned}\sum_{T=1}^{min\{n,m\}}\left\lfloor\frac{n}{T}\right\rfloor\left\lfloor\frac{m}{T}\right\rfloor\sum_{d\mid T}d\cdot\mu\left(\frac{T}{d}\right)\end{aligned}\]

记后面那个狄利克雷卷积形式的式子为 \(F(T)\) ,显然这个是可以枚举因子在近似于 \(O(n\ln n)\) 的时限内预处理出来的。然后数论分块的复杂度为 \(O(\sqrt n)\) ,对于 \(t\) 组询问...哦...没有 \(t\) 组询问...那我最后一步还搞个屁啊...

Code

//It is made by Awson on 2018.2.22
#include <bits/stdc++.h>
#define LL long long
#define dob complex<double>
#define Abs(a) ((a) < 0 ? (-(a)) : (a))
#define Max(a, b) ((a) > (b) ? (a) : (b))
#define Min(a, b) ((a) < (b) ? (a) : (b))
#define Swap(a, b) ((a) ^= (b), (b) ^= (a), (a) ^= (b))
#define writeln(x) (write(x), putchar('\n'))
#define lowbit(x) ((x)&(-(x)))
using namespace std;
const int N = 100000;
void read(int &x) {
char ch; bool flag = 0;
for (ch = getchar(); !isdigit(ch) && ((flag |= (ch == '-')) || 1); ch = getchar());
for (x = 0; isdigit(ch); x = (x<<1)+(x<<3)+ch-48, ch = getchar());
x *= 1-2*flag;
}
void print(LL x) {if (x > 9) print(x/10); putchar(x%10+48); }
void write(LL x) {if (x < 0) putchar('-'); print(Abs(x)); } int n, m, mu[N+5];
LL F[N+5]; void get_F() {
int isprime[N+5], prime[N+5], tot = 0;
memset(isprime, 1, sizeof(isprime)); isprime[1] = 0, mu[1] = 1;
for (int i = 2; i <= N; i++) {
if (isprime[i]) mu[i] = -1, prime[++tot] = i;
for (int j = 1; j <= tot && i*prime[j] <= N; j++)
if (i%prime[j] != 0) isprime[i*prime[j]] = 0, mu[i*prime[j]] = -mu[i];
else {isprime[i*prime[j]] = 0, mu[i*prime[j]] = 0; break; }
}
for (int i = 1; i <= N; i++) for (int j = 1; j*i <= N; j++) F[i*j] += i*mu[j];
for (int i = 1; i <= N; i++) F[i] += F[i-1];
}
LL cal(int n, int m) {
if (n > m) Swap(n, m); LL ans = 0;
for (int i = 1, last; i <= n; i = last+1) {
last = Min(n/(n/i), m/(m/i));
ans += 1ll*(n/i)*(m/i)*(F[last]-F[i-1]);
}
return ans;
}
void work() {
read(n), read(m); get_F(); writeln(2ll*cal(n, m)-1ll*n*m);
}
int main() {
work(); return 0;
}
上一篇:TX2上安装spi和uart驱动


下一篇:NRF24L01模块配置