hdu2841 容斥

分析:

整数平面坐标系在原点连线上的点,第一对互质,其它都是倍数。
所以就是 [1,n] [1,m] 间互质的数的个数。
枚举 [1,n] 的数,分解质因数,容斥不互质的求反即可。复杂度 O(n2phi(n))

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define pr(x) cout << #x << ": " << x << "  " 
#define pl(x) cout << #x << ": " << x << endl;

struct jibancanyang
{
    static const int maxn = int(1e5) + 13;
    int n, m, primes[maxn], primeCnt, factors[maxn], factorCnt;
    bool isprime[maxn];

    void getPrime() {
        primeCnt = 0;
        memset(isprime, false, sizeof(isprime));
        for (int i = 2; i < maxn; ++i) {
            if (!isprime[i]) {
                primes[primeCnt++] = i;
                for (int j = i; j < maxn; j += i) {
                    isprime[j] = true;
                }
            }
        }
    }

    void getFactors(int x) {
        factorCnt = 0;
        for (int i = 0; (long long)primes[i] * primes[i] <= x; ++i) {
            if (x % primes[i] == 0) {
                factors[factorCnt++] = primes[i];
                while (x % primes[i] == 0) x /= primes[i];
            }
        }
        if (x > 1) factors[factorCnt++] = x;
    }

    long long rC() {
        long long ans = (long long)n * m;
        for (int K = 1; K <= n; ++K) {
            getFactors(K);
            for (int s = 1; s < 1 << factorCnt; ++s) {
                int cur = 1, cnt = 0;
                for (int i = 0; i < factorCnt; ++i) {
                    if (s >> i & 1) ++cnt, cur *= factors[i];
                }
                if (cnt & 1) ans -= m / cur;
                else ans += m / cur;
            }
        }
        return ans;
    }

    void fun() {
        int T; 
        scanf("%d", &T);
        getPrime();
        while (T--) {
            scanf("%d%d", &n, &m);
            printf("%lld\n", rC());
        }
    }


}ac;

int main()
{
#ifdef LOCAL
    freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
#endif
    ac.fun();
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值