JZOJ 5701.「GDOI2018」谈笑风生

吐槽一下数据,我式子推错了也能 50pts(
先不考虑边权,二分并跑最短路判定。
出题人居然没卡 SPFA(

对于一条边 \((u,v)\),它的边权显然就是 \(\sum\limits_{i = 1}^{a_u} \sum\limits_{j = 1}^{a_v} [\gcd(i,j) = 1](i + j)\)
显然可以考虑莫比乌斯反演。

根据套路,设 \(f(x) = \sum\limits_{i = 1}^{a_u} \sum\limits_{j = 1}^{a_v} [\gcd(i,j) = x](i + j),F(x) = \sum\limits_{x | d} f(d) = \sum\limits_{i = 1}^{a_u} \sum\limits_{j = 1}^{a_v} [x | \gcd(i,j)](i + j)\)
对于 \(F(x)\),我们来变换一下。

\[\begin{align*} & \sum\limits_{i = 1}^{a_u} \sum\limits_{j = 1}^{a_v} [x | \gcd(i,j)](i + j) \\ = & x \sum\limits_{xi \le a_u} \sum\limits_{xj \le a_v} (i + j) \\ = & x \left( \lfloor \dfrac {a_v} x \rfloor \sum\limits_{xi \le a_u} i + \lfloor \dfrac {a_u} x \rfloor \sum\limits_{xj \le a_v} j \right) \\ = & x \cdot \dfrac {\lfloor \frac {a_u} x \rfloor \lfloor \frac {a_v} x \rfloor (\lfloor \frac {a_u} x \rfloor + \lfloor \frac {a_v} x \rfloor + 2)} 2 \end{align*}\]

那么我们要求的是 \(f(1)\)

\[\begin{align*} f(1) = & \sum\limits_{i = 1}^{\min(a_u,a_v)} \mu(i)F(i) \\ = & \sum\limits_{i = 1}^{\min(a_u,a_v)} \mu(i) i \cdot \dfrac {\lfloor \frac {a_u} i \rfloor \lfloor \frac {a_v} i \rfloor (\lfloor \frac {a_u} i \rfloor + \lfloor \frac {a_v} i \rfloor + 2)} 2 \end{align*}\]

于是预处理 \(\mu(i) i\) 的前缀和即可。

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 1e4;
const int M = 2e4;
const int A = 1e5;
int n,m,a[N + 5];
long long lim;
int to[(M << 1) + 5],pre[(M << 1) + 5],first[N + 5];
long long val[(M << 1) + 5];
inline void add(int u,int v,long long w)
{
static int tot = 0;
to[++tot] = v;
val[tot] = w;
pre[tot] = first[u];
first[u] = tot;
}
int cnt,prime[A + 5],vis[A + 5],mu[A + 5];
long long calc(int n,int m)
{
long long ret = 0;
if(n > m)
swap(n,m);
for(register int l = 1,r;l <= n;l = r + 1)
{
r = min(n / (n / l),m / (m / l));
ret += (long long)(mu[r] - mu[l - 1]) * (2 + n / l + m / l) * (n / l) * (m / l) / 2;
}
return ret;
}
long long l,r,mid,ans;
int head,tail,q[N * 100 + 5];
long long dis[N + 5];
int check()
{
memset(vis,0,sizeof vis);
memset(dis,0x3f,sizeof dis);
head = tail = 0;
dis[vis[1] = q[++tail] = 1] = 0;
while(head < tail)
{
int p = q[++head];
vis[p] = 0;
for(register int i = first[p];i;i = pre[i])
if(dis[to[i]] > dis[p] + max(val[i] - mid,0LL))
{
dis[to[i]] = dis[p] + max(val[i] - mid,0LL);
if(!vis[to[i]])
vis[to[i]] = 1,q[++tail] = to[i];
}
}
return dis[n] <= lim;
}
int main()
{
freopen("magic.in","r",stdin);
freopen("magic.out","w",stdout);
mu[1] = 1;
for(register int i = 2;i <= A;++i)
{
if(!vis[i])
prime[++cnt] = i,mu[i] = -1;
for(register int j = 1;j <= cnt && i * prime[j] <= A;++j)
{
vis[i * prime[j]] = 1;
if(!(i % prime[j]))
break;
mu[i * prime[j]] = -mu[i];
}
}
for(register int i = 1;i <= A;++i)
mu[i] = mu[i - 1] + mu[i] * i;
scanf("%d%d%lld",&n,&m,&lim);
for(register int i = 1;i <= n;++i)
scanf("%d",a + i);
int u,v;
long long w;
for(register int i = 1;i <= m;++i)
{
scanf("%d%d",&u,&v);
w = calc(a[u],a[v]);
add(u,v,w),add(v,u,w);
}
r = 1e18;
while(l <= r)
{
mid = l + r >> 1;
if(check())
r = mid - 1,ans = mid;
else
l = mid + 1;
}
mid = ans,check();
printf("%lld %lld\n",ans,dis[n]);
}