【样例输入1】
5 4
【样例输入2】
3 4

【样例输出1】
36
【样例输出2】
20

## Solution

$\sum_{i=1}^n \sum_{j=1}^mgcd(i,j)$

$=\sum_{p=1}^{n} p \sum_{i=1}^n \sum_{j=1}^m[gcd(i,j)=p]$

$=\sum_{p=1}^np\sum_{i=1}^{\left \lfloor \frac{n}{p} \right \rfloor}\sum_{j=1}^{\left \lfloor \frac{m}{p} \right \rfloor}[gcd(i,j)=1]$

$=\sum_{p=1}^np\sum_{i=1}^{\left \lfloor \frac{n}{p} \right \rfloor}\sum_{j=1}^{\left \lfloor \frac{m}{p} \right \rfloor}\sum_{d|gcd(a,b)}\mu(d)$

$=\sum_{p=1}^np\sum_{d=1}^{\left \lfloor \frac{n}{p} \right \rfloor}\mu(d){\left \lfloor \frac{n}{pd} \right \rfloor}{\left \lfloor \frac{m}{pd} \right \rfloor}$

$=\sum_{T=1}^{n}{\left \lfloor \frac{n}{T} \right \rfloor}{\left \lfloor \frac{m}{T} \right \rfloor}\sum_{p|T}p*\mu(\frac{T}{p})$

$=\sum_{T=1}^{n}{\left \lfloor \frac{n}{T} \right \rfloor}{\left \lfloor \frac{m}{T} \right \rfloor}φ(T)$

$\sum_{p|T}p*\mu(\frac{T}{p})=φ(T)$好像是因为用到了求欧拉函数的时候容斥的思想QAQ……

## Code

 1 #include<iostream>
2 #include<cstdio>
3 #define N (100000)
4 using namespace std;
5
6 long long ans,n,m,sum[N+5],phi[N+5];
7
8 void Get_phi()
9 {
10     phi[1]=1;
11     for (int i=2; i<=N; ++i)
12         if (!phi[i])
13             for (int j=i; j<=N; j+=i)
14             {
15                 if (!phi[j]) phi[j]=j;
16                 phi[j]=phi[j]/i*(i-1);
17             }
18     for (int i=1; i<=N; ++i) sum[i]=sum[i-1]+phi[i];
19 }
20
21 int main()
22 {
23     scanf("%lld%lld",&n,&m);
24     if (n>m) swap(n,m);
25     Get_phi();
26     for (int l=1,r; l<=n; l=r+1)
27     {
28         r=min(n/(n/l),m/(m/l));
29         ans+=(sum[r]-sum[l-1])*(n/l)*(m/l);
30     }
31     printf("%lld\n",2*ans-n*m);
32 }

posted on 2018-08-30 11:05  Refun  阅读(133)  评论(2编辑  收藏  举报