整除相关

gcd 相关

首先要熟悉几个转化:
\(\gcd (a,b) = \gcd(a, a + qb)\)
\(\gcd (a, b) = \gcd(a, a - b)\)
\(\gcd (a, b) = \gcd(a, a \% b)\)

这几个转化很常见。
还有:
\(\operatorname{lcm}(a, \gcd(b, c)) = \gcd(\operatorname{lcm}(a, b), \operatorname{lcm}(a, c))\)
\(\gcd(a, \operatorname{lcm}(b, c)) = \operatorname{lcm}(\gcd(a, b), \gcd(a, c))\)

CF1717E

\(\sum \limits_{a+b+c=n} \operatorname{lcm}(c, \gcd(a, b))\)

暴力枚举 \(c\),然后 \(a+b=n-c\)。利用第一个转化发现原式 \(= \sum \limits _{a \in [1, n - c - 1]} \operatorname{lcm}(c, \gcd(a, n - c))\)

这时候要用到一个经典的例题(本题重中之重):计算 \(a \in [1, n-c-1], \gcd(a, n-c)\) 为各个数的 \(a\) 的方案数。

首先,\(\gcd(a, n-c)\) 肯定是 \(n-c\) 的因子。那么我们枚举 \(\gcd\),假设为 \(g\),那么如果 \(\gcd(a, n-c) = g\)\(a\) 要满足:\(a | g, (a / g) ⊥ ((n-c) / g)\)

刚好,我们把 \(1 \sim n-c-1\) 中所有整除 \(g\) 的数除了 \(g\) 之后的取值映射到 \(1 \sim (n-c) / g\) 中,并且需要与 \((n-c) / g\) 互质。方案数显然是 \(\phi((n-c)/g)\)

需要注意,当 \(g = n-c\) 时算出来的 \(\phi\)\(1\),但这个 \(1\) 其实是 \(a = n - c\) 贡献的,不符合要求,不能计算。

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define f(i, a, b) for (int i = (a); i <= (b); i++)
#define cl(i, n) i.clear(), i.resize(n);
#define endl '\n'
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
const int inf = 1e9;
const int mod = 1e9 + 7;
int qpow(int x, int k)
{
    int ans = 1;
    while (k)
    {
        if (k & 1)
            ans = ans * x % mod;
        x = x * x % mod;
        k >>= 1;
    }
    return ans;
}
int lcm(int x, int y)
{
    return x * y / __gcd(x, y);
}
int n;
int phi[100010];
int calc(int x)
{
    int ans = x;
    for (int i = 2; i * i <= x; i++)
    {
        if (x % i == 0)
        {
            ans = ans * (i - 1) / i;
            while (x % i == 0)
            {
                x /= i;
            }
        }
    }
    if (x > 1)
        ans = ans * (x - 1) / x;
    return ans;
}
signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(NULL);
    cout.tie(NULL);
    time_t start = clock();
    // think twice,code once.
    // think once,debug forever.
    int ans = 0;
    cin >> n;
    f(i, 1, n) phi[i] = calc(i);
    f(c, 1, n - 2)
    {
        int k = n - c;
        for (int i = 1; i * i <= k; i++)
        {
            if (k % i == 0)
            {
                ans += phi[k / i] * lcm(c, i);
                ans %= mod;

                if (i * i != k && i != 1)
                {
                    ans += phi[i] * lcm(c, k / i);
                    ans %= mod;
                }
            }
        }
    }
    cout << ans << endl;
    time_t finish = clock();
    // cout << "time used:" << (finish-start) * 1.0 / CLOCKS_PER_SEC <<"s"<< endl;
    return 0;
}

因子个数

https://codeforces.com/contest/1744/problem/E2

CF1744E.Divisible Numbers

一般我们估计复杂度的时候认为因子数的上界是 \(O(\sqrt n)\),但是这个上界是比较松的。

\(10^{18}\) 以内的数最多有 \(103680\) 个因子,\(10^9\) 以内的数最多有 \(1344\) 个因子。

为什么呢?我们不妨考虑估计一个更紧的上界。
对于 \(x = p_1^{\alpha_1}...p_k^{\alpha_k}\),其因子个数为 \(\prod \alpha_i\)。如果我们给 \(x\) 乘以一个它已经拥有的质因子 \(p_j\),那么因子个数会加上 \(\cfrac{\prod \alpha_{i}}{\alpha_{j}}\);如果乘上一个它没有的质因子 \(p_{k+1}\),那么因子个数会乘以 \(2\)
因此由于 \(10^{18} < 2 \times 3 \times ... \times 53\),而 \(2^6 > 53\),故只有很少几次会选择乘上已经拥有的因子。因此因子数上界为 \(O(2^{15})\)\(47\) 是第 \(15\) 个质数),常数一般在 \(10\) 以下。故可以估计约为 \(3 \times 10^5\)。故可以暴力枚举并 \(O(1)\) 判断答案。

posted @ 2022-09-03 12:13  OIer某罗  阅读(21)  评论(0编辑  收藏  举报