zoukankan      html  css  js  c++  java
  • BZOJ4659:lcm

    传送门
    题目所给的不合法的条件可以转化为

    [exists p,p^2|gcd(a,b) Leftrightarrow mu(gcd(a,b)) e 0 ]

    那么

    [ans=sum_{a=1}^{A}sum_{b=1}^{B}[mu(gcd(i,j)) e 0]frac{ab}{gcd(a,b)} ]

    不妨假设 (Ale B),枚举 (gcd) 之后经典莫比乌斯反演
    (S(x)=sum_{i=1}^{x}i)
    得到

    [sum_{i=1}^{A}S(lfloorfrac{A}{i} floor)S(lfloorfrac{B}{i} floor)sum_{d|i}[mu(d) e 0](frac{i}{d})^2mu(frac{i}{d})d ]

    后面的狄利克雷卷积是一个积性函数,直接线性筛

    # include <bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    typedef unsigned int uint;
    
    const int maxn(4e6 + 5);
    const int mod(1 << 30);
    
    int test, pr[maxn], tot, n, m, cnt[maxn], pwd[maxn];
    uint ans, s[maxn];
    bitset <maxn> ispr;
    
    inline uint S(uint x) {
    	return (x & 1) ? (x + 1) / 2 * x : x / 2 * (x + 1);
    }
    
    int main() {
    	register uint i, j;
    	ispr[1] = 1, s[1] = 1;
    	for (i = 2; i < maxn; ++i) {
    		if (!ispr[i]) pwd[i] = pr[++tot] = i, s[i] = i - i * i, cnt[i] = 1;
    		for (j = 1; j <= tot && i * pr[j] < maxn; ++j) {
    			ispr[i * pr[j]] = 1;
    			if (i % pr[j]) {
    				s[i * pr[j]] = s[i] * s[pr[j]];
    				pwd[i * pr[j]] = pr[j], cnt[i * pr[j]] = 1;
    			}
    			else {
    				cnt[i * pr[j]] = cnt[i] + 1, pwd[i * pr[j]] = pwd[i] * pr[j];
    				if (cnt[i] == 1) s[i * pr[j]] = -(uint)pr[j] * pr[j] * pr[j] * s[i / pwd[i]];
    				else s[i * pr[j]] = 0;
    				break;
    			}
    		}
    	}
    	for (i = 2; i < maxn; ++i) s[i] += s[i - 1];
    	scanf("%d", &test);
    	while (test) {
    		scanf("%d%d", &n, &m), test--;
    		if (n > m) swap(n, m);
    		for (ans = 0, i = 1; i <= n; i = j + 1) {
    			j = min(n / (n / i), m / (m / i));
    			ans += (s[j] - s[i - 1]) * S(n / i) * S(m / i);
    		}
    		printf("%u
    ", ans % mod);
    	}
        return 0;
    }
    
    
  • 相关阅读:
    eclipse luna maven失效的原因
    利用线性探测法解决hash冲突
    PHP和JavaScript将字符串转换为数字string2int
    JavaScript 编程易错点整理
    使用phpstudy创建本地虚拟主机
    单例模式
    PHP使用cookie时遇到的坑
    Redis安装与配置
    CI框架2.x的验证码中所遇问题解决
    用delete和trancate删除表记录的区别
  • 原文地址:https://www.cnblogs.com/cjoieryl/p/10178267.html
Copyright © 2011-2022 走看看