洛谷P2158- SDOI2008]仪仗队 - 莫比乌斯反演 & 欧拉函数
Posted Chivas_/Regal
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了洛谷P2158- SDOI2008]仪仗队 - 莫比乌斯反演 & 欧拉函数相关的知识,希望对你有一定的参考价值。
题目:
思路1 - 欧拉函数:
每个能被看到的点的gcd(x, y) = 1
将正方形沿对角线分成两个三角形
对于每个三角形
寻找每一层有几个互质数即可
所以
r
e
s
=
2
∗
∑
i
=
1
n
−
1
p
h
i
[
i
]
+
1
res =2 * \\sum_{i = 1}^{n - 1} phi[i] + 1
res=2∗∑i=1n−1phi[i]+1
思路2 - 莫比乌斯反演:
我们要求得:
∑
i
=
1
n
∑
j
=
1
n
[
g
c
d
(
i
,
j
)
=
1
]
\\sum_{i = 1}^n\\sum_{j=1}^n[gcd(i, j) = 1]
∑i=1n∑j=1n[gcd(i,j)=1]
所以设:
f
(
x
)
=
∑
i
=
1
n
∑
j
=
1
n
[
g
c
d
(
i
,
j
)
=
x
]
f(x) =\\sum_{i=1}^n\\sum_{j=1}^n[gcd(i,j)=x]
f(x)=∑i=1n∑j=1n[gcd(i,j)=x]
为使:
F
(
x
)
=
∑
x
∣
d
f
(
d
)
F(x) = \\sum_{x|d}f(d)
F(x)=∑x∣df(d)
设:
F
(
x
)
=
∑
i
=
1
n
∑
j
=
1
n
[
x
∣
g
c
d
(
i
,
j
)
]
F(x)=\\sum_{i=1}^n\\sum_{j=1}^n[x|gcd(i,j)]
F(x)=∑i=1n∑j=1n[x∣gcd(i,j)]
根据莫比乌斯定理得:
f
(
x
)
=
∑
x
∣
d
μ
(
d
x
)
F
(
d
)
f(x)=\\sum_{x|d}\\mu(\\frac dx)F(d)
f(x)=∑x∣dμ(xd)F(d)
可发现:
F
(
d
)
=
∑
i
=
1
n
[
d
∣
i
]
∗
∑
j
=
1
n
[
d
∣
j
]
=
⌊
n
d
⌋
∗
⌊
n
d
⌋
F(d)=\\sum_{i=1}^n[d|i] * \\sum_{j=1}^n[d|j] = \\left \\lfloor \\frac nd \\right \\rfloor * \\left \\lfloor \\frac nd \\right \\rfloor
F(d)=∑i=1n[d∣i]∗∑j=1n[d∣j]=⌊dn⌋∗⌊dn⌋
所以化简为:
f
(
x
)
=
∑
x
∣
d
μ
(
d
x
)
⌊
n
d
⌋
⌊
n
d
⌋
f(x)=\\sum_{x|d}\\mu(\\frac dx)\\left \\lfloor \\frac nd \\right \\rfloor \\left \\lfloor \\frac nd \\right \\rfloor
f(x)=∑x∣dμ(xd)⌊dn⌋⌊dn⌋
我们要求的是 f ( 1 ) f(1) f(1)
所以:
f
(
1
)
=
∑
d
=
1
n
μ
(
d
)
⌊
n
d
⌋
⌊
n
d
⌋
f(1)=\\sum_{d=1}^{n}\\mu(d)\\left \\lfloor \\frac nd \\right \\rfloor \\left \\lfloor \\frac nd \\right \\rfloor
f(1)=∑d=1nμ(d)⌊dn⌋⌊dn⌋
代码:
/*
________ _ ________ _
/ ______| | | | __ | | |
/ / | | | |__| | | |
| | | |___ _ _ _ ___ _ _____ | ___| ______ _____ ___ _ | |
| | | __ \\ |_| | | | | | _\\| | | ____| | |\\ \\ | __ | | _ | | _\\| | | |
| | | | \\ | _ | | | | | | \\ | | \\___ | | \\ \\ | |_/ _| | |_| | | | \\ | | |
\\ \\______ | | | | | | \\ |_| / | |_/ | ___/ | | | \\ \\ | /_ \\__ | | |_/ | | |
Author : \\________| |_| |_| |_| \\___/ |___/|_| |_____| _________|__| \\__\\ |______| | | |___/|_| |_|
____| |
\\_____/
*/
#include <unordered_map>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <utility>
#include <string>
#include <vector>
#include <cstdio>
#include <stack>
#include <queue>
#include <cmath>
#include <map>
#include <set>
#define G 10.0
#define LNF 1e18
#define EPS 1e-6
#define PI acos(-1.0)
#define INF 0x7FFFFFFF
#define ll long long
#define ull unsigned long long
#define LOWBIT(x) ((x) & (-x))
#define LOWBD(a, x) lower_bound(a.begin(), a.end(), x) - a.begin()
#define UPPBD(a, x) upper_bound(a.begin(), a.end(), x) - a.begin()
#define TEST(a) cout << "---------" << a << "---------" << '\\n'
#define CHIVAS_ int main()
#define _REGAL exit(0)
#define SP system("pause")
#define IOS ios::sync_with_stdio(false)
//#define map unordered_map
#define _int(a) int a; cin >> a
#define _ll(a) ll a; cin >> a
#define _char(a) char a; cin >> a
#define _string(a) string a; cin >> a
#define _vectorInt(a, n) vector<int>a(n); cin >> a
#define _vectorLL(a, b) vector<ll>a(n); cin >> a
#define PB(x) push_back(x)
#define ALL(a) a.begin(),a.end()
#define MEM(a, b) memset(a, b, sizeof(a))
#define EACH_CASE(cass) for (cin >> cass; cass; cass--)
#define LS l, mid, rt << 1
#define RS mid + 1, r, rt << 1 | 1
#define GETMID (l + r) >> 1
using namespace std;
template<typename T> inline void Read(T &x){T f = 1; x = 0;char s = getchar();while(s < '0' || s > '9'){if(s == '-') f = -1; s = getchar();}while('0'<=s&&s<='9'){x=(x<<3)+(x<<1)+(s^48);s=getchar();}x*=f;}
template<typename T> inline T MAX(T a, T b){return a > b? a : b;}
template<typename T> inline T MIN(T a, T b){return a > b? b : a;}
template<typename T> inline void SWAP(T &a, T &b){T tp = a; a = b; b = tp;}
template<typename T> inline T GCD(T a, T b){return b > 0? G以上是关于洛谷P2158- SDOI2008]仪仗队 - 莫比乌斯反演 & 欧拉函数的主要内容,如果未能解决你的问题,请参考以下文章