题意:
给你一个区间
[
L
,
R
]
[L,R]
[L,R],求有多少个
p
a
i
r
(
x
,
y
)
pair(x,y)
pair(x,y)满足条件。
condition:
g
c
d
(
x
,
y
)
!
=
1
gcd(x,y) != 1
gcd(x,y)!=1,
x
g
!
=
1
\frac{x}{g} != 1
gx!=1,
y
g
!
=
1
\frac{y}{g} != 1
gy!=1
思路:
- 枚举gcd
之后在区间内查找满足条件 g c d ( x , y ) ! = 1 gcd(x,y) != 1 gcd(x,y)!=1的个数。 c n t d ∗ c n t d cnt_d*cnt_d cntd∗cntd
ans += cnt_d*cnt_d
//这里亥要处理一下前面计算过的结果,跑个筛法,
- 之后减去不符合条件的。
对于x不满足的情况只有 x = g c d ( x , y ) x = gcd(x,y) x=gcd(x,y),之后y取满。
相反的y也有。所以乘2
ans -= cnt_d*2-1;
AC
#include <iostream>
#include <cstdio>
#include <vector>
#include <map>
#include <unordered_map>
#include <set>
#include <algorithm>
#include <queue>
#include <stack>
#include <string>
#include <cmath>
#include <cstring>
#define For(i,x,y) for(int i = (x); i <= (y); i ++ )
#define fori(i,x,y) for(int i = (x); i < (y); i ++ )
#define sz(a) (int)a.size()
#define ALL(a) a.begin(), a.end()
#define mst(x,a) memset(x,a,sizeof(x))
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
#define db double
#define endl '\n'
#define debug(a) cout << #a << ": " << a << endl
using namespace std;
typedef long long LL;
typedef long long ll;
typedef unsigned long long ULL;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int inf = 0x3f3f3f3f;
const int mod = 1e9+7;
typedef pair<int,int>pa;
typedef pair<ll,ll>pai;
typedef pair<db,db> pdd;
const db eps = 1e-6;
const db pi = acos(-1.0);
template<typename T1, typename T2> void ckmin(T1 &a, T2 b) { if (a > b) a = b; }
template<typename T1, typename T2> void ckmax(T1 &a, T2 b) { if (a < b) a = b; }
int read() {
int x = 0, f = 0; char ch = getchar();
while (!isdigit(ch)) f |= ch == '-', ch = getchar();
while (isdigit(ch)) x = 10 * x + ch - '0', ch = getchar();
return f ? -x : x;
}
template<typename T> void print(T x) {
if (x < 0) putchar('-'), x = -x;
if (x >= 10) print(x / 10);
putchar(x % 10 + '0');
}
template<typename T> void print(T x, char let) {
print(x), putchar(let);
}
template<class T> bool uin(T &a, T b) { return a > b ? (a = b, true) : false; }
template<class T> bool uax(T &a, T b) { return a < b ? (a = b, true) : false; }
const int maxn = 1e6 + 10;
LL cnt[maxn+1], cnt2[maxn+1];
void sieve(int l, int r){
For(i,2,maxn){
for(int j = i; j <= r; j += i){
if(j < l) continue;
cnt[i]++;
}
}
}
int main() {
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
LL L, R;
cin>>L>>R;
sieve(L,R);
LL ans = 0;
for(int i = R; i >= 2; i --){
cnt2[i] = cnt[i]*cnt[i];
for(int j = i*2; j <= R; j += i){
cnt2[i] -= cnt2[j];
}
ans += cnt2[i];
if(i >= L) ans -= cnt[i]*2-1;
}
cout<<ans<<endl;
return 0;
}