ABC206--E - Divide Both(容斥&&数学分析)

题意:

给你一个区间 [ 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 cntdcntd

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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值